Datetime value in long variable need to convert to this format 2014-04-06T12:04:23.000Z
what is the simplest approach.
Asked
Active
Viewed 1.0k times
1
Naruto
- 9,476
- 37
- 118
- 201
-
1Check SimpleDateFormat. It works on patterns and is really easy to use. See: http://developer.android.com/reference/java/text/SimpleDateFormat.html – markubik Apr 06 '14 at 12:41
-
Here you'll find an answer: http://stackoverflow.com/questions/7953725/how-to-convert-milliseconds-to-date-format-in-android – Phantômaxx Apr 06 '14 at 12:43
-
@markubik, Hi, thanks but the response is in string i.e simpledateformat, how to convert to datetime? – Naruto Apr 06 '14 at 12:46
-
Date type has constructor that takes String as argument. – markubik Apr 06 '14 at 12:48
3 Answers
5
Assuming the long is the usual value (milliseconds since The Epoch), you create a Date instance:
Date theDate = new Date(theLongValue);
...and then use one of the DateFormats (perhaps SimpleDateFormat) or something like JodaTime to format the result however you want.
If you're using Java 8, you might check out the new java.time package, which provides a new and hopefully better API for dates and times; Oracle tutorial here.
T.J. Crowder
- 1,031,962
- 187
- 1,923
- 1,875
3
Try this simply way,
Android
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
System.out.format("%30s %s\n", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", sdf.format(new Date(0))); // 0 - your "LongValue"
Result
yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000
Great Android Doc for SimpleDateFormat
Jaykumar Patel
- 26,836
- 12
- 74
- 76
0
Use SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSSZ");
Date outputDate = format.parse(inputDate);
StarsSky
- 6,721
- 6
- 38
- 63