0

This is the code which works correctly in Java 8 and prints the desired output.

import java.util.*;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class Java11 {

    public static void main(String args[]) throws Exception {

        // Hong kong timezone ID.
        String hongkongTimezone = "Asia/Hong_Kong";
        String dateFormat = "MMM dd, yyyy HH:mm:SS";

        //Current date of local time zone.
        Date date = new Date();

        // Current date of HongKong.
        Date currentDateHongkong = null;

        DateFormat hongkongDateFromat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);

        // Set hongkong time zone ID.
        hongkongDateFromat.setTimeZone(TimeZone.getTimeZone(hongkongTimezone));
        String stringFormatDate = hongkongDateFromat.format(date);

        currentDateHongkong = new SimpleDateFormat(dateFormat).parse(stringFormatDate, new ParsePosition(0));

        System.out.println(currentDateHongkong);
    }
}

Output is following when executed on Java 8

Mon Oct 22 06:28:00 IST 2018

But when I execute the same code on Java 11, I get null.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
efex09
  • 417
  • 2
  • 12
  • I am using this api of java11 https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html#parse(java.lang.String,java.text.ParsePosition) – efex09 Oct 22 '18 at 10:36
  • I can't reproduce this, the program prints null on 1.8.0_121, 9 and 11. – Marv Oct 22 '18 at 10:43
  • I am using openjdk @Marv – efex09 Oct 22 '18 at 10:44
  • 3
    Okay, I can't try reproducing this now then. But have you considered using `LocalDateTime` instead of the obsolete `Date` API, or are you using `Date` for legacy reasons? And why are you formatting a date only to try and parse it again? – Marv Oct 22 '18 at 10:49
  • 3
    You should never want to use `Date` nor `DateFormat`, neither on Java 8 nor Java 11. The latter in particular is notoriously troublesome. [java.time, the modern Java date and timeAPI,](https://docs.oracle.com/javase/tutorial/datetime/) is so much nicer to work with. – Ole V.V. Oct 22 '18 at 10:58
  • 1
    Probable variant of [Java - Unparseable date](https://stackoverflow.com/questions/6154772/java-unparseable-date) and/or [Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical](https://stackoverflow.com/questions/46285384/getting-error-java-text-parseexception-unparseable-date-at-offset-0-even-if) – Ole V.V. Oct 22 '18 at 11:03
  • 1
    Also what would be the point in formatting a date using one formatter and trying to parse it back using a different formatter? – Ole V.V. Oct 22 '18 at 11:06
  • ´SS` (milliseconds) should be `ss` (seconds). Quite **important** here. – Joop Eggen Oct 22 '18 at 11:14

1 Answers1

1

You are using different format patterns for formatting and later parsing.

Try

currentDateHongkong = hongkongDateFromat.parse(stringFormatDate, new ParsePosition(0));

instead of

currentDateHongkong = new SimpleDateFormat(dateFormat).parse(stringFormatDate, new ParsePosition(0));
Selaron
  • 6,105
  • 4
  • 31
  • 39