I'm using the below function to convert a microsecond format time string to ZoneDateTime
so I can do comparisons later.
public static ZonedDateTime createTimeFromString(String inputTime) {
ZonedDateTime time;
try {
System.out.printf("Input time %s%n", inputTime);
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyyMMdd-HH:mm:ss.SSSSSS");
LocalDate date = LocalDate.parse(inputTime, formatter);
time = date.atStartOfDay(ZoneId.systemDefault());
System.out.printf("Formated time %s%n", time);
return time;
}
catch (DateTimeParseException exc) {
System.out.printf("%s is not parsable!%n", inputTime);
throw exc; // Rethrow the exception.
}
}
However, whatever time string I pass into the function I get the same output.
eg:
Input time 20171025-10:58:24.062151
Formated time 2017-10-25T00:00+05:30[Asia/Colombo]
Input time 20171025-10:58:25.446862
Formated time 2017-10-25T00:00+05:30[Asia/Colombo]
I'm using Java 8.
Can you please clarify what I'm doing wrong?
When you call LocalDate.parse
, you're getting just the date part (day, month and year) and discarding the rest. A LocalDate
doesn't have the time fields (hour, minute, seconds and fraction of second), so they are simply discarded and lost.
Then you call atStartOfDay(ZoneId.systemDefault())
, which sets the time to the first moment of the day per the JVM default timezone. (Usually the first moment is the time 00:00, though some dates in some zones may start at another time, such as 01:00.)
If you want to keep everything (date and time), parse it to a LocalDateTime
, which is a class that contains all the date and time fields. Then you call the atZone
method to convert it to a ZonedDateTime
:
String inputTime = "20171025-10:58:24.062151";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HH:mm:ss.SSSSSS");
// parse to a LocalDateTime (keeping all date and time fields)
LocalDateTime date = LocalDateTime.parse(inputTime, formatter);
// convert to ZonedDateTime
ZonedDateTime z = date.atZone(ZoneId.systemDefault());
PS: ZoneId.systemDefault()
returns the JVM default timezone, but keep in mind that this can be changed without notice, even at runtime, so it's better to always make it explicit which one you're using.
The API uses IANA timezones names (always in the format Region/City
, like Asia/Colombo
or Europe/Berlin
).
Avoid using the 3-letter abbreviations (like IST
or CET
) because they are ambiguous and not standard.
You can get a list of available timezones (and choose the one that fits best your system) by calling ZoneId.getAvailableZoneIds()
. Then you call the ZoneId.of()
method with the zone name, like this:
// using specific timezone instead of JVM's default
ZonedDateTime z = date.atZone(ZoneId.of("Asia/Colombo"));