Java simpledateformat interprets the parsed string as UTC

My time zone is GMT 1

Therefore, the "date" object and "22.09.1985 00:00 UTC" print "Sun SEP 22 01:00:00 CEST 1985" on the toString function

Now I try to create this date by parsing "22 / 09 / 1985" using simpledateformat

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getDefault());
Date d = sdf.parse("22/09/1985");
    => Sun Sep 22 00:00:00 CEST 1985

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = sdf.parse("22/09/1985");
    => Sun Sep 22 02:00:00 CEST 1985

How do I configure simpledateformat to create a date printout string "Sun SEP 22 01:00:00 CEST 1985" of "22 / 09 / 1985"?

Solution

My hypothesis is wrong,

22.09.1985 00:00UTC is actually 22.09.1985 02:00CET

therefore

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = sdf.parse("22/09/1985");

It's exactly what I want. I'm wrong to compare its date

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>