A history table of an old source system has 2 fields regarding date/time: a date (ex: 12/20/2017) String and a time (14:33:00) String. I have no timezone information I can use. The web service I'm creating is being consumed by a UI team which wants this information as an ISO 8601 formatted String. I'm using Java 8. Is creating an ISO 8601 formatted String version to return to the UI possible without a timezone?
Asked
Active
Viewed 5,209 times
2
-
3How can we convert `14:33:00` to UTC if we don't know what timezone it is? – Joe C Jul 11 '17 at 20:06
-
4*UTC formatted version of this String* - UTC is not a format, it's a time zone. You can't convert to a time zone if you don't know the source. – shmosel Jul 11 '17 at 20:08
-
1Only one thing you can do is to make an assumption in what timezone your old source system ran... as example what is Time Zone of database Server? or if values are string and was provided by some appserver - what is time zone of that appserver. Otherwise there is no way... – Vadim Jul 11 '17 at 20:12
-
1@shmosel, technically it's not a time zone, but it can stand in for one. Very true that UTC is not a format. :) – Matt Johnson-Pint Jul 11 '17 at 22:44
-
1I confused the terminology. I guess I should have asked about ISO 8601 formatting, not UTC since that is a time zone. – JL Gradley Jul 12 '17 at 00:58
-
1Technically [UTC is a standard, not a timezone](https://stackoverflow.com/a/44760188/7605325) - but thinking that it is make things simpler. Anyway, if you want to convert the date to UTC, it's not possible without a timezone (the timezone tells you what's the offset - the difference from UTC - so without this information there's no way to convert). If you just want to print the date and time (without timezone/offset) in ISO format, well, there's an answer below. – Jul 12 '17 at 01:23
-
Thanks everyone for the feedback. Dates/times are my kryptonite. The answer below was what I was looking for. – JL Gradley Jul 12 '17 at 13:23
2 Answers
5
Yes, you can use DateTimeFormatter ISO_LOCAL_DATE_TIME
to format without the time zone:
String input = "12/20/2017 14:33:00";
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(input, inputFormat);
String output = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(date);
System.out.println(output);
Output:
2017-12-20T14:33:00

shmosel
- 49,289
- 6
- 73
- 138
1
shmosel’s answer is correct. I would find it clearer and cleaner to make explicit in the code that the date and the time come from separate fields:
String dateString = "12/20/2017";
String timeString = "14:33:00";
DateTimeFormatter dateInputFormat = DateTimeFormatter.ofPattern("MM/dd/uuuu");
String iso8601DateTimeString = LocalDate.parse(dateString, dateInputFormat)
.atTime(LocalTime.parse(timeString))
.toString();
This produces 2017-12-20T14:33
, which is ISO 8601 compliant.
Instead of toString()
you may use format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
. Funnily it doesn’t always produce the same result: we now get 2017-12-20T14:33:00
, that is, the seconds are given even when they are 0. Both versions are ISO 8601 compliant.

Ole V.V.
- 81,772
- 15
- 137
- 161