1

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.

6
  • is this getDate php function? Commented May 18, 2013 at 4:24
  • Nope its an SQL function. Commented May 18, 2013 at 4:38
  • there are SimpleDateFormat to change date formate see Commented May 18, 2013 at 4:41
  • I dont see your link @AnkitMakwana. Commented May 18, 2013 at 5:09
  • developer.android.com/reference/java/text/SimpleDateFormat.html Commented May 18, 2013 at 5:15

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Okay it gives the following error: java.text.ParseException: Unparseable date: "2013-05-17T16:27:34.9+05:30" (at offset 10).
Im guessing it has something to do with the semicolon that seperates my time zone and time. Gah! Help. :|
It should be like "2013-05-17T16:27:34.9+0530"
Using string functions will just make it messier, isn't there some way around this?
@GarimaTiwari check my updated answer I have updated my answer for the GMT time but format should be like that format I have wrote in my answer. And if you don't change then try Tarun updated answer. you can ask another question for GMT time if required.
|
3
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.

9 Comments

But I wont get the day along with the date will I?
Calendar c = Calendar.getInstance(); c.setTime(date); c.get(Calendar.DATE)); link
upvote for your GMT answer.It is right for the new format given by @GarimaTiwari.
I looked up whatever format the getDate() function returns and there is no T in it, i wonder whats causing this.
@GarimaTiwari you just have to call DateTime jodaParsed = jodaFormatter.parseDateTime("your date"); It will give you the parsed date and time which you can use to create date object.
|
2

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);

5 Comments

Hello again, I didn't quite completely understand the above code you gave but is there any way I can display the day along with the date using the date format I've mentioned in my question?
day means in which sense??
Like today, 18-05-2013 along with Saturday.
Hey you should do like this go get your result "MM/dd/yyyy EEEE" please go through docs.oracle.com/javase/1.4.2/docs/api/java/text/…
@Segi, all my chatrooms are blocked in office! Sorry :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.