SimpleDateFormat和DataFormat具有不同的时区

时间:2018-05-26 06:48:32

标签: java timezone simpledateformat date-format

我对SimpleDateFormatDateFormat有疑问。虽然我在德国设置了本地时区,但会显示不同的时区。 输出是:

SimpleDateFormat:Tue May 01 00:00:00 CEST 2018
DateFromat: 26.05.18 08:46:24 MESZ

我的代码:

public class StringAndDateFormat {
    private static final int  dateStyleLoc = DateFormat.SHORT;
    private static final int timeStyleLoc= DateFormat.LONG;
    private static final Locale localeLoc = Locale.GERMANY;
    private static final String stringDateIni= "2000/01/01 00:00:00:";

    public static void main(String[] args) {
        System.out.println(getStringNow());
        System.out.println(getDateFromStringSimpleDateFormat("01.05.2018 00:00:00"));
    }

    public static String getStringNow() {
        DateFormat dateFormat = DateFormat.getDateTimeInstance(dateStyleLoc, timeStyleLoc, localeLoc);
        return  dateFormat.format(new Date());
    }

    public static Date getDateFromStringSimpleDateFormat(String stringDateIn) {
        SimpleDateFormat simpleDateFormatTmp = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss",localeLoc);
        Date dateTmp= null;

        try {

            dateTmp=  simpleDateFormatTmp.parse(stringDateIn);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return dateTmp;
    }

1 个答案:

答案 0 :(得分:0)

private static final Locale localeLoc = Locale.GERMANY;
private static final ZoneId zoneLoc = ZoneId.of("Europe/Berlin");
private static final DateTimeFormatter formatterLoc = DateTimeFormatter
        .ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.LONG)
        .withLocale(localeLoc);

public static void main(String[] args) {
    System.out.println(getStringNow());
    ZonedDateTime parsedDateTime 
            = getDateFromStringDateTimeFormatter("01.05.2018 00:00:00");
    System.out.println(parsedDateTime);
    System.out.println(parsedDateTime.format(formatterLoc));
}

public static String getStringNow() {
    return ZonedDateTime.now(zoneLoc).format(formatterLoc);
}

public static ZonedDateTime getDateFromStringDateTimeFormatter(String stringDateIn) {
    DateTimeFormatter formatterTmp
            = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss", localeLoc);
    return LocalDateTime.parse(stringDateIn, formatterTmp).atZone(zoneLoc);
}

现在运行此代码打印

26.05.18, 10:12:41 MESZ
2018-05-01T00:00+02:00[Europe/Berlin]
01.05.18, 00:00:00 MESZ

在您的代码中,您已在GERMANYDateFormat上设置了区域设置(SimpleDateFormat)。您没有设置任何时区。即使区域设置和时区可能都与地理区域相关联(它们并非总是如此),但它们并不直接相关,因此设置其中一个区域永远不会影响另一个区域。此外,如果您在SimpleDateFormat上设置了时区和区域设置,则Dateparse方法获得的Date中都不会反映出这一点,因为dateFormat既不能持有时区或地区。这只是一个时间点。

相反,两种格式都使用了JVM的时区设置。 GERMANY使用了MESZ语言环境,因此为中欧夏令时的德语名称MitteleuropäischeSommerzeit制作了simpleDateFormatTmpDate使用相同的默认时区将日期时间字符串解释为某个时间点,但正如我所说,未将时区存储到Date。当您打印toString时,您隐式调用其toString方法。此方法再次使用JVM的默认时区生成字符串。这个事实让很多人困惑。但是,ZonedDateTime总是使用英语,所以当天是“星期二”(星期二),月份是“五月”,半个时区是中欧夏令时的“CEST”,同一时区有一路使用。

顺便说一句,中欧夏令时不是真正的时区。相反,它是在夏季时间生效的一年中大约7个月中用于中欧时间的夏令时名称(夏令时也称为夏令时或夏令时)。中欧时间反过来是大量欧洲时区的共同名称,这些时区目前共享同一时间,但并非总是如此,因此不能被认为是相同的。

在我的代码中,我使用Date来保存日期。与toString相反,正如其名称暗示 持有时区,因此似乎可以为您提供所需内容。其java.time方法将时区渲染为欧洲/柏林。要获得MESZ,只需使用与我们用于其他字符串相同的格式化程序。

我热烈建议您使用现代Java日期和时间API DateFormat,抛弃过时的SimpleDateFormatDatejava.time。现代API可以更好地使用。

链接: Oracle tutorial: Date Time解释如何使用<link rel="stylesheet" type="text/css" href="print.css" media="print">