Γεια σου. I believe that vguzzi’s answer is correct. I would like to add two messages:
- Avoid the outdated classes
GregorianCalendar
, Date
, TimeZone
and the notoriously troublesome SimpleDateFormat
. Instead use java.time
, the modern Java date and time API.
- Use the built-in localized formats rather than rolling your own to match the expectations of a local audience much better.
Code example:
DateTimeFormatter originalFormatter
= DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
DateTimeFormatter userFormatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String originalDateTimeString = "Thu Jun 28 14:25:00 GMT+03:00 2018";
ZonedDateTime dateTime = ZonedDateTime.parse(originalDateTimeString, originalFormatter);
String formattedDateTime = dateTime.format(userFormatter);
System.out.println(formattedDateTime);
When run in US locale this prints a result that resembles yours:
Jun 28, 2018, 2:25:00 PM
Already British locale is a bit different:
28 Jun 2018, 14:25:00
There’s English and English. And Java knows that Spanish doesn’t use AM and PM either:
28 jun. 2018 14:25:00
It was a surprise to me to learn that Greek does:
28 Ιουν 2018, 2:25:00 μ.μ.
The output examples are from the code running on my Java 9. The locale data on Android may differ giving slightly different output in some cases, but still well suited to the different local audiences.
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 (from API level 26, I’m told) 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.
Links