0

from web, the date is in Asia/Hong_Kong format (UTC+8), but when pass to WildFly server using JSON format, in server debug mode, the date string become UTC format. e.g.

web:Mon Oct 12 2015 00:00:00 GMT+0800 (Hong Kong Standard Time)
WildFly server:2015-10-11T16:00:00.000Z

I want to use java SimpleDateFormat to parse it back to Asia/Hong_Kong format, I try

    String dateUTC="2015-10-11T16:00:00.000Z";

    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println("sdf1.parse(dateUTC)="+sdf1.parse(dateUTC));

    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    sdf2.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println("sdf2.parse(dateUTC)="+sdf2.parse(dateUTC));
    SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
    sdf3.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
    System.out.println("sdf3.parse(dateUTC)="+sdf3.parse(dateUTC));

I want the output to be "2015-10-12" but fail, the output is

sdf1.parse(dateUTC)=Sun Oct 11 00:00:00 HKT 2015
sdf2.parse(dateUTC)=Sun Oct 11 08:00:00 HKT 2015
sdf3.parse(dateUTC)=Sun Oct 11 00:00:00 HKT 2015

1) So how to make the output to be "2015-10-12"? (As Hong Kong is UTC+8)?

2) any method to make the server can receive the web datetime in Asia/Hong_Kong format, but not in UTC datetime format? I try to set WildFly java_opts by setting VM arguments

-Duser.timezone=Asia/Hong_Kong

but no use, the server still receive the datetime as UTC format.

user1169587
  • 1,104
  • 2
  • 17
  • 34
  • 1
    I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead use `DateTimeFormatter` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 12 '19 at 11:25
  • 1
    I’m unsure what your confusion is and what you had expected to happen. A `Date` hasn’t got a format, so you cannot get output `2015-10-12` from printing it, see [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format). A `Date` hasn’t got a time zone, so `HKT` is not in your `Date` object, only in the printed output. See [Get GMT Time in Java](https://stackoverflow.com/questions/5236052/get-gmt-time-in-java). (to be continued) – Ole V.V. Jul 12 '19 at 11:32
  • 1
    (continued) The formatter you are using for parsing should specify the format of the string you are trying to parse, not that of the intended result, see [Convert String Date to String date different format](https://stackoverflow.com/questions/14999506/convert-string-date-to-string-date-different-format). See also [All about java.util.Date](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/). – Ole V.V. Jul 12 '19 at 11:33
  • so when the datetime pass from web to server, it will change from hong kong time to UTC, this is normal? – user1169587 Jul 13 '19 at 04:45
  • How and where that change happens I have not understood (handling times in UTC in the server is considered good practice, so I also wouldn’t be worried). – Ole V.V. Jul 13 '19 at 05:11

1 Answers1

2

Stop using the obsolete Date and SimpleDateFormat classes. use the java.time package.

And realize that if you have a complete instant, precise to the millisecond, and specifying a timezone (Z means UTC), in the standard ISO format, then you shoul not ignore all the time part and the timezone when parsing it. Parse the whole thing:

    String dateUTC = "2015-10-11T16:00:00.000Z";
    Instant instant = Instant.parse(dateUTC);
    ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Hong_Kong"));
    System.out.println(zonedDateTime.toLocalDate());
    // prints 2015-10-12

The equivalent, using the old, obsolete Date and SimpleDateDateFormat classes, that you shouldn't use anymore, would be

    String dateUTC = "2015-10-11T16:00:00.000Z";

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
    Date parsed = sdf.parse(dateUTC);
    SimpleDateFormat sdfHongKong = new SimpleDateFormat("yyyy-MM-dd");
    sdfHongKong.setTimeZone(TimeZone.getTimeZone("Asia/Hong_Kong"));
    System.out.println(sdfHongKong.format(parsed));
    // prints 2015-10-12
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255