java.time
I recommend that you use java.time, the modern Java date and time API, for your date and time work. Among its many advantages is that in many cases it gives you far better validation than the old classes like TimeZone
, SimpleDateFormat
and Date
. Here’s my shot at the modern equivalent of your code:
String str = "2016-06-21-10-19-22";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd-hh-mm-ss");
ZonedDateTime dateTime = LocalDateTime.parse(str, dtf)
.atZone(ZoneId.of("fewfefewf"));
long epoch = dateTime.toInstant().toEpochMilli();
System.out.println(epoch);
Running it produces:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-06-21-10-19-22' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MicroOfSecond=0, MinuteOfHour=19, NanoOfSecond=0, MilliOfSecond=0, SecondOfMinute=22, HourOfAmPm=10},ISO resolved to 2016-06-21 of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
at ovv.misc.MiscTest.<init>(MiscTest.java:72)
at ovv.misc.MiscTest.main(MiscTest.java:61)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {MicroOfSecond=0, MinuteOfHour=19, NanoOfSecond=0, MilliOfSecond=0, SecondOfMinute=22, HourOfAmPm=10},ISO resolved to 2016-06-21 of type java.time.format.Parsed
at java.base/java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.base/java.time.format.Parsed.query(Parsed.java:235)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
... 3 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {MicroOfSecond=0, MinuteOfHour=19, NanoOfSecond=0, MilliOfSecond=0, SecondOfMinute=22, HourOfAmPm=10},ISO resolved to 2016-06-21 of type java.time.format.Parsed
at java.base/java.time.LocalTime.from(LocalTime.java:431)
at java.base/java.time.LocalDateTime.from(LocalDateTime.java:457)
... 5 more
Surprised? java.time has already found a bug that you didn’t ask about: in the format pattern string you need uppercase HH
for hour of day from 00 through 23. Lowercase hh
is for clock hour of AM or PM from 01 through 12. The detail to note in the error message is HourOfAmPm=10
— this is not what you intended. Let’s fix and run again:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
Exception in thread "main" java.time.zone.ZoneRulesException: Unknown time-zone ID: fewfefewf
at java.base/java.time.zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:279)
at java.base/java.time.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:234)
at java.base/java.time.ZoneRegion.ofId(ZoneRegion.java:120)
at java.base/java.time.ZoneId.of(ZoneId.java:408)
at java.base/java.time.ZoneId.of(ZoneId.java:356)
at ovv.misc.MiscTest.<init>(MiscTest.java:73)
at ovv.misc.MiscTest.main(MiscTest.java:61)
I believe that this is what you asked for. If we fix this error too, the code runs flawlessly:
ZonedDateTime dateTime = LocalDateTime.parse(str, dtf)
.atZone(ZoneId.of("America/Eirunepe"));
1466522362000
I put in a random time zone, but I think that you know which one you want.
The other answers have already explained nicely what went wrong in your code. I know of no way to persuade the old and poorly designed TimeZone
class to validate the time zone ID string.
Link: Oracle tutorial: Date Time explaining how to use java.time.