Question

I have created a web service which returns the date of an event which is initially captured by the getDate() function. I want the date returned by this function (something along this format : 2013-05-17 14:52:00.943) to be parsed and shown to the user in the DD-MM-YYYY format.

Any suggestions? I haven't found any solution along this direction yet.

Was it helpful?

Solution

I have tried this code and it's work fine for me,Please Try my code below: Please upvote to Tarun also coz he gave almost right answer.just he did mistake that he passes cal.getTime() method instead of pDate

String formatDate = p.format(pDate);

and second mistake in format like"DD-MM-YYYY" but actual format is:

"dd-MM-yyyy" not "DD-MM-YYYY"

I have done changes in it and modify it.

String dateStr = "2013-05-16 14:52:00.943"; 

SimpleDateFormat c = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S"); // your web service format
Date pDate = c.parse(dateStr); 
SimpleDateFormat p = new SimpleDateFormat("dd-MM-yyyy"); // your required format

String formatDate = p.format(pDate); // convert it in your required format
SimpleDateFormat formatter = new SimpleDateFormat("EEEE"); // Day format as you want EEE for like "Sat" and EEEE for like "Saturday"
String Day = formatter.format(pDate); // This will give you a day as your selected format

System.out.println("Date & Day>>>"+formatDate+" "+Day);

// For GMT format your format should be like this: "2013-05-16 14:52:00.943 GMT+05:30"

// Give it to me in GMT time.
c.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
System.out.println("GMT time: " + c.format(pDate));

Output:

Date & Day>>>16-05-2013 Thursday
GMT time: 2013-05-16 02:52:00.943 Greenwich Mean Time

Joda time:

you can download 2.0 jar file of joda time from here:

DateTimeFormatter jodaFormatter = ISODateTimeFormat.dateTime();
DateTime jodaParsed = jodaFormatter
                .parseDateTime("2013-05-17T16:27:34.9+05:30");
Date date2 = jodaParsed.toDate();
System.out.println("Date & Day:" + jodaParsed.getDayOfMonth() + "-" + jodaParsed.getMonthOfYear() + "-" + jodaParsed.getYear() + " " + jodaParsed.getHourOfDay() + ":" + jodaParsed.getMinuteOfHour()+" "+jodaParsed.dayOfWeek().getAsText());

output:

Date & Day:17-5-2013 16:27 Friday

Hope it will work for you.

OTHER TIPS

String dateStr = "2013-05-17 14:52:00.943"; 

SimpleDateFormat c = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S"); 
Date pDate = c.parse(dateStr); 
SimpleDateFormat p = new SimpleDateFormat("dd-MM-yyyy"); 

String formatDate = p.format(pDate); 

You can use Joda Time if you have colon in time offset.

DateTimeFormatter jodaFormatter = ISODateTimeFormat.dateTime();
DateTime jodaParsed = jodaFormatter.parseDateTime("2013-05-17T16:27:34.9+05:30"); 
Date date = jodaParsed.toDate();
Calendar c = Calendar.getInstance(); 
c.setTime(date); 
c.get(Calendar.DATE));

More info about joda can be found here.

Use a SimpleDateFormat to parse the date and then print it out with a SimpleDateFormat withe the desired format.

Example:

SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("05/18/2013");
System.out.println(format2.format(date));

Output:

11-05-2013

Edit:

Calendar cal = Calendar.getInstance();
cal.setTime(specific_date);
int dayOfMonth = cal.get(Calendar.DAY_OF_WEEK);

String dayOfMonthStr = String.valueOf(dayOfMonth);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top