I have the time in milliseconds which I need to convert to 2009-01-31T06:34:45Z
using Joda library. I have written the below program but the date parser is adding milliseconds by default.
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateTime dateTime = new DateTime(System.currentTimeMillis());
String str = fmt.print(dateTime);
System.out.println("Date as string - " + str);
dateTime = fmt.parseDateTime(str);
System.out.println("Date as Joda dataTime - " + dateTime);
Following is the output:
Date as string - 2007-03-24T23:03:44Z
Date as Joda dataTime - 2007-03-24T23:03:44.000+05:30
.000+05:30
is getting added to the dateTime
object. The print
method print the dateTime in correct format, but the parse
method adds milliseconds and timezone unnecessarily. Please let me know the mistake in the program.
.000+05:30
is getting added to the dateTime object
No, it's included in the result of dateTime.toString()
, that's all. A DateTime
value doesn't have any concept of its own format - it's just a date/time/calendar/zone. If you want to convert a DateTime
value to a specific format, you need to go through the formatter again - which you clearly know how to do, as you're already doing it in your code.
It's very important to distinguish between "the data in the object" and "the text representation you want". A DateTime
is always stored with a precision of milliseconds, regardless of what it happened to be parsed from.
See more on this question at Stackoverflow