I would like to convert: 2018-06-04T21:00:00.000-07:00 this to 2018-06-05 which is the date for the correct time zone (I don't know know what the correct time zone is only have this -07:00).
-
Add your source code – Rafiq Apr 25 '18 at 14:31
-
`the date for the correct time zone` which timezone is that? (your target timezone, not the -07:00 one) – Aaron Apr 25 '18 at 14:34
-
nothing to add, have not idea to approach this – pshemek Apr 25 '18 at 14:34
-
Are you working with Date class or just strings ? – sharon182 Apr 25 '18 at 14:38
-
I get this 2018-06-04T21:00:00.000-07:00 as String – pshemek Apr 25 '18 at 14:40
-
How should it be possible to convert to the date in the correct time zone when you don’t know the correct time zone? Your string will correspond to two, sometimes three different dates in different time zones. – Ole V.V. Apr 25 '18 at 14:53
3 Answers
Try this.
String date = "2018-06-04T21:00:00.000-07:00";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
System.out.println(dateFormat.parseObject(date));

- 2,516
- 4
- 18
- 20
-
Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Apr 25 '18 at 14:49
If you're using Java8, you can use the ZonedDateTime
class, like this:
String d = "2018-06-04T21:00:00.000-07:00";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
ZonedDateTime zonedDateTime = ZonedDateTime.parse(d, parser);
// Convert here to any other timezone you want...
System.out.println(zonedDateTime.withZoneSameInstant(ZoneOffset.UTC));

- 3,229
- 2
- 21
- 25
-
-
that is indeed very unfortunate :). There's Joda, which can be used in Java 7 – Magd Kudama Apr 25 '18 at 14:44
-
No big problem, @pshemek. [ThreeTen Backport](http://www.threeten.org/threetenbp/) backports the `java.time` functionality (originally introduced in Java 8) to Java 6 and 7. So get that library and add it to your project, and you’re all fine. I warmly recommend it. – Ole V.V. Apr 25 '18 at 14:50
-
Good one, didn't know about that project. I always used Joda in the past... But that's long time ago now! :) – Magd Kudama Apr 25 '18 at 14:52
When you don’t know the correct time zone, this is not possible.
A string like yours conforms to the ISO 8601 format for an offset date time, that is a date and time with a UTC offset (but without time zone). So the only natural thing to do is parse it into an OffsetDateTime
:
String dateTimeString = "2018-03-04T03:45:00.000-07:00";
OffsetDateTime dateTime = OffsetDateTime.parse(dateTimeString);
System.out.println(dateTime);
Output:
2018-03-04T03:45-07:00
Parsing works without any explicit formatter because OffsetDateTime
and the other date-time classes of java.time
parse ISO 8601 format as their default (and also print ISO 8601 back in their toString
methods). java.time
is the modern Java date and time API. (I on purpose picked a different date and time from yours in order to make my next point.)
To convert it to a date, time zone is crucial. Let’s try with three different time zones:
System.out.println(dateTime.atZoneSameInstant(ZoneId.of("Pacific/Apia")).toLocalDate());
System.out.println(dateTime.atZoneSameInstant(ZoneId.of("Pacific/Pago_Pago")).toLocalDate());
System.out.println(dateTime.atZoneSameInstant(ZoneId.of("Asia/Kamchatka")).toLocalDate());
Output:
2018-03-05
2018-03-03
2018-03-04
We got three different dates, March 3rd, 4th and 5th. Because it is never the same date everywhere on earth. Make your pick. :-)
Possibly it will be OK to use your JVM’s time zone setting. If so, use ZoneId.systemDefault()
as argument to atZoneSameInstant()
. The time zone setting can be changed at any time from another part of your program or another program running in the same JVM, though.
Question: Can I use java.time with Java 7?
Yes, java.time
just requires at least Java 6.
- In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
- On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package
org.threeten.bp
and subpackages.
Links
- Oracle tutorial: Date Time, explaining how to use
java.time
. - ThreeTen Backport project
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Java Specification Request (JSR) 310.
- Wikipedia article: ISO 8601

- 81,772
- 15
- 137
- 161