This question already has an answer here:
-
Converting ISO 8601-compliant String to java.util.Date
25 answers
How do I can parse this date 2018-01-09T11:11:02.0+03:00
to dd.MM.yyyy hh:mm
format in Android?
And what does T
between 09
and 11
mean?
Thanks.
I don't know how the back-end developer got this format.
I am using Java.
If you are using java, you can use SimpeDateFormat
with patterns:
String date = "2018-01-09T11:11:02.0+03:00";
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
SimpleDateFormat output = new SimpleDateFormat("dd.MM.yyyy hh:mm");
Date d = null;
try {
d = dateformat.parse(date /*your date as String*/);
} catch (ParseException e) {
e.printStackTrace();
}
String formattedDate = output.format(d);
Log.d("Date format", "output date :" + formattedDate);
The output is :
D/Date format: output date :09.01.2018 09:11
EDIT : Thanks to @OleV.V., for API > 26, or using ThreeTenABP we can use
DateTimeFormatter, we can do something like that
String date = "2018-01-09T11:11:02.0+03:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern("dd.MM.yyyy hh:mm");
LocalDate parsedDate = LocalDate.parse(date, formatter);
String formattedDate = formatterOut.format(parsedDate);
Log.d("Date format", "output date :" + formattedDate);
DateTimeFormatter desiredFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
String backendDateTimeString = "2018-01-09T11:11:02.0+03:00";
OffsetDateTime dateTime = OffsetDateTime.parse(backendDateTimeString);
String presentationDateTimeString = dateTime.format(desiredFormatter);
System.out.println(presentationDateTimeString);
This prints:
09.01.2018 11:11
Please note: I am using uppercase HH
in the format pattern string. This indicates hour of day from 00 through 23. In the question you used lowercase hh
, which in a format pattern string means hour with AM or PM from 01 through 12, so 00:33 would come out as 12:33 and 15:47 as 03:47. I didn’t think you intended this.
The format that your backend developer got, 2018-01-09T11:11:02.0+03:00
, is ISO 8601. It’s widespread, and it’s the international standard, so it’s good that s/he got that. The funny T
in the middle indicates the start of the time part to separate it from the date part. The one-arg OffsetDateTime.parse
method parses ISO 8601, which is why we didn’t need any formatter for parsing. OffsetDateTime
and DateTimeFormatter
are classes from java.time
, the modern Java date and time API.
Question: Can I use java.time on Android?
Yes, java.time
works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (API level 26 and up) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages (my code was tested with these imports).
Links
- Oracle tutorial: Date Time explaining how to use
java.time
.
- Java Specification Request (JSR) 310, where
java.time
was first described.
- ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310).
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Wikipedia article: ISO 8601
You can do this with SimpleDateFormat.
Here is a tested example in Java:
String dateString = "2018-01-09T11:11:02.0+03:00";
String inPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
String outPattern = "dd.MM.yyyy hh:mm";
SimpleDateFormat inFormat = new SimpleDateFormat(inPattern, Locale.getDefault());
SimpleDateFormat outFormat = new SimpleDateFormat(outPattern, Locale.getDefault());
try {
Date inDate = inFormat.parse(dateString);
String outDate = outFormat.format(inDate);
Log.e("TEST", outDate);
} catch (ParseException e) {
e.printStackTrace();
}
Here is a tested example in Kotlin:
val dateString = "2018-01-09T11:11:02.0+03:00"
val inPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
val outPattern = "dd.MM.yyyy hh:mm"
val inFormat = SimpleDateFormat(inPattern, Locale.getDefault())
val outFormat = SimpleDateFormat(outPattern, Locale.getDefault())
val inDate = inFormat.parse(dateString)
val outDate = outFormat.format(inDate)
Log.e("TEST", outDate)