SimpleDateFormat对象更改时区,但未添加/减去正确数量的小时。

huangapple go评论83阅读模式
英文:

SimpleDateFormat object changes timezone without adding/subtracting the correct number of hours

问题

我有一个字符串表示的是UTC时区的日期(因为我的数据库使用UTC时间)。我想使用SimpleDateFormat将这个字符串转换为日期。问题是它将其转换为CEST时区的日期,而没有添加UTC和CEST之间相隔的2小时。以下是代码部分:

//这是一个UTC时间
String text = "2020-09-24T09:45:22.806Z";
//在这里我定义了正确的格式(最后的Z表示它是UTC时间)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//然后我进行解析
Date date = sdf.parse(text);
//然后我将其打印出来
System.out.println(date);

打印的结果是:

Thu Sep 24 09:45:22 CEST 2020
为什么是CEST?我希望它保持为UTC时间,但如果必须变为CEST时间,至少要添加2小时。
英文:

I have a String which represents a Date in the UTC timezone (because my database uses UTC). I want to convert this String into a date with SimpleDateFormat. The problem is that converts it into a Date in the CEST timezone without adding the 2 hour separating UTC and CEST. Here is the code:

//This is a date in UTC
String text = "2020-09-24T09:45:22.806Z";
//Here I define the correct format (The final Z means that it's UTC)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 
//Then I parse it
Date date = sdf.parse(text);
//Then I print it
System.out.println(date);  

The result of the print is

Thu Sep 24 09:45:22 CEST 2020

Why CEST? I would like it to remain UTC, but if it has to become CEST at least add the 2 hours

答案1

得分: 0

你应该在你的 DateFormat 中使用 setTimeZone(),如下所示:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        // 这是一个在 UTC 时间的日期
        String text = "2020-09-24T09:45:22.806Z";
        // 这里我定义了正确的格式(最后的 Z 表示它是 UTC 时间)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        // 然后我进行解析
        Date date = sdf.parse(text);
        // 然后我打印它
        System.out.println(date);
    }
}

我还根据 文档'Z' 替换为 X

英文:

You should setTimeZone() to your DateFormat like

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        //This is a date in UTC
        String text = "2020-09-24T09:45:22.806Z";
        //Here I define the correct format (The final Z means that it's UTC)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        //Then I parse it
        Date date = sdf.parse(text);
        //Then I print it
        System.out.println(date);
    }
}

I also replaced 'Z' to X following the documentation

huangapple
  • 本文由 发表于 2020年9月24日 19:03:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64045124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定