BST时间错误

BST Time is wrong

使用下面的代码,除了 BST

之外的每个时区都正确打印值
​import java.text.*;

def format = "yyyy-MM-dd HH:mm:ssXXX"
def dt = new Date();
println dt;

SimpleDateFormat utcFormat = new SimpleDateFormat(format)
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
println utcFormat.format(dt)

SimpleDateFormat istFormat = new SimpleDateFormat(format)
istFormat .setTimeZone(TimeZone.getTimeZone("IST"))
println istFormat.format(dt)

SimpleDateFormat cetFormat = new SimpleDateFormat(format)
cetFormat.setTimeZone(TimeZone.getTimeZone("CET"))
println cetFormat.format(dt)

SimpleDateFormat bstFormat = new SimpleDateFormat(format)
bstFormat.setTimeZone(TimeZone.getTimeZone("BST"))
println bstFormat.format(dt)

输出:

3 月 26 日星期一09:04:14UTC 2018

2018-03-2609:04:14Z

2018-03-26 14:34:14+05:30

2018-03-26 11:04:14+02:00

2018-03-26 15:04:14+06:00

这里BST时间不对。有什么问题吗?

您似乎希望 BST 是英国夏令时,但在本例中它代表 Bangladesh Standard Time。 另见

来自答案:

下面的代码有效

SimpleDateFormat bstFormat = new SimpleDateFormat(format)
bstFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"))
println bstFormat.format(dt)

我运行你的代码,发现BST的时间刚刚好。将 6 小时的偏移量添加到 09:04:14 的 UTC 时间得到 15:04:14。我认为您对时区的首字母缩写感到困惑。

如果你在JVM中的时区数据库不正确,你可以通过TimeZone.getTimeZone("Europe/London");

获取英国夏令时时区

您应该避免使用缩写的时区名称。检查 TimeZone documentation 中的以下注释:

Three-letter time zone IDs

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

在您的情况下,系统可能已将 BST 映射到 比什凯克标准时间 ,其时区偏移量为 +06:00 小时。标准命名约定是 Region/City 例如Europe/London、Europe/Paris、Asia/Kolkata等 其中城市一般指一个国家在该地区最大的城市。如有疑问,执行以下语句获取所有时区ID:

System.out.println(ZoneId.getAvailableZoneIds());

java.time

java.util 日期时间 API 及其格式 API、SimpleDateFormat 已过时且容易出错。建议完全停止使用它们并切换到 modern Date-Time API*.

解决方案使用 java.time,现代日期时间 API:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssXXX", Locale.ENGLISH);
        
        Instant now = Instant.now();
        
        System.out.println(now.atZone(ZoneOffset.UTC).format(dtf));
        System.out.println(now.atZone(ZoneId.of("Asia/Kolkata")).format(dtf));
        System.out.println(now.atZone(ZoneId.of("Europe/Paris")).format(dtf));
        System.out.println(now.atZone(ZoneId.of("Europe/London")).format(dtf));
    }
}

输出 来自样本运行:

2021-10-12 12:24:03Z
2021-10-12 17:54:03+05:30
2021-10-12 14:24:03+02:00
2021-10-12 13:24:03+01:00

ONLINE DEMO

Trail: Date Time.

了解有关现代日期时间 API 的更多信息

* 如果您正在为一个 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time