Skip to content

Instantly share code, notes, and snippets.

@marco-schmidt
Last active August 14, 2021 23:42
Show Gist options
  • Save marco-schmidt/4be2b4a2cfa6772a0124361b3d37f2a2 to your computer and use it in GitHub Desktop.
Save marco-schmidt/4be2b4a2cfa6772a0124361b3d37f2a2 to your computer and use it in GitHub Desktop.
Parse legacy timestamp strings without timezone information in them and adapt them according to known timezone, considering daylight saving time.
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
// parse timestamp strings not containing timezone information (but timezone is known)
// without daylight saving time this would be easy (add fixed string like "+01:00")
// how to use Java 8 time API?
// with Java 11+ simply run as: java TimestampParser.java
public class TimestampParser
{
/** test cases legacy timestamp to be converted, do not contain timezone
in Europe/Berlin in the year 2021 daylight saving time (DST)
is between Mar 28th 3am and Oct 31st 3am */
public static final String[] INPUT_BERLIN =
{//YYYY-MM-DD,hh:mm:ss,millis
"2021-03-28,01:00:00,123", // before DST, switches at 2am to 3am
"2021-03-28,02:00:49,123", // not valid, does not exist
"2021-03-28,03:00:49,123", // first hour of DST, one hour after first timestamp
// at 3am time switches back to 2am
"2021-10-31,02:00:49,123", // ambiguous: first or second time 2:xx am?
"2021-10-31,03:00:49,123", // after DST ended
};
/** expected output when reformatting parsed timestamps with ISO-8601 */
public static final String[] OUTPUT_BERLIN =
{//ISO-8601
"2021-03-28T01:00:00.123+01:00",
null,
"2021-03-28T03:00:49.123+02:00",
"2021-10-31T02:00:49.123+01:00", // arbitrary pick, might have been +02:00
"2021-10-31T03:00:49.123+01:00",
};
public static final String[] INPUT_MEMPHIS =
{//YYYY-MM-DD,hh:mm:ss,millis
"2021-03-14,01:00:00,123", // before DST, switches at 2am to 3am
"2021-03-14,02:00:49,123", // not valid, does not exist
"2021-03-14,03:00:49,123", // first hour of DST, one hour after first timestamp
// at 3am time switches back to 2am
"2021-11-07,01:00:49,123", // ambiguous: first or second time?
"2021-11-07,02:00:49,123", // after DST ended
};
/** expected output when reformatting parsed timestamps with ISO-8601 */
public static final String[] OUTPUT_MEMPHIS =
{//ISO-8601
"2021-03-14T06:00:00.123-06:00",
null,
"2021-03-14T08:00:49.123-05:00",
"2021-11-07T08:00:49.123-05:00", // arbitrary pick, might have been +02:00
"2021-11-07T08:00:49.123-06:00",
};
public static void parse(final String[] INPUT, final String[] OUTPUT, ZoneId zone)
{
System.out.println("Parse for " + zone.getId() + " on a system with default " + ZoneId.systemDefault().getId());
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd,HH:mm:ss,SSS");
int i = 0;
for (String t : INPUT)
{
LocalDateTime ldt = LocalDateTime.parse(t, dtf);
ZonedDateTime withZone = ZonedDateTime.of(ldt, zone);
System.out.println("input =" + t);
System.out.println("parsed local=" + ldt.toString());
System.out.println("parsed zone =" + withZone);
System.out.println("expected =" + OUTPUT[i++]);
System.out.println("parsed utc =" + withZone.toInstant());
System.out.println();
}
}
public static void main(String[] args)
{
parse(INPUT_BERLIN, OUTPUT_BERLIN, ZoneId.of("Europe/Berlin"));
parse(INPUT_MEMPHIS, OUTPUT_MEMPHIS, ZoneId.of("America/Chicago"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment