1

I expect the below code to output: 20181104 05
But it outputs: 20181104 06

Why is that? I also double checked on this site and they got it right.

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH");
    dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
    Date date = dateFormat.parse(20181104 + " " + 1);

    dateFormat = new SimpleDateFormat("yyyy MM dd HH");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    System.out.println(dateFormat.format(date));
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Aug 08 '18 at 21:09
  • Tip: Your work would be much easier if you use standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formats for your date-time strings. – Basil Bourque Aug 08 '18 at 21:19

0 Answers0