Java SimpleDateFormat将解析字符串解释为UTC [英] Java SimpleDateFormat interpret parse-string as UTC

查看:800
本文介绍了Java SimpleDateFormat将解析字符串解释为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的时区是GMT + 1。

my timezone is GMT+1.

所以日期 - 22.09.1985 00:00UTC打印Sun Sep 22 01:00: 00 CEST 1985onstring函数。

so a "Date"-object with "22.09.1985 00:00UTC" prints "Sun Sep 22 01:00:00 CEST 1985" on the tostring function.

现在我试图通过解析22/09/1985with simpleDateFormat

Now i'm trying to create this date by parsing "22/09/1985" with 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

如何配置simpleedateformat创建一个Date打印Sun Sep 22 01:00:00 CEST 1985,输入字符串22/09/1985?

how can i configure simpledateformat that it creates an Date which prints "Sun Sep 22 01:00:00 CEST 1985" with input string "22/09/1985"?

推荐答案

我的假设是错误的,

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

so

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

正是我想要的,我比较的日期是错误的。

is exactly what i wanted, the date i compared it with was wrong.

这篇关于Java SimpleDateFormat将解析字符串解释为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆