gpt4 book ai didi

java - Java日期强制BST“时区”

转载 作者:行者123 更新时间:2023-12-01 11:54:28 35 4
gpt4 key购买 nike

在JAVA中,如何确定所有日期都作为GMT日期返回?

例如,即使我尝试使用GMT语言环境强制使用DateFormat,它也会应用某种逻辑来检索BST日期。

public static void main(String[] args) throws ParseException {
DateFormat dd = new SimpleDateFormat("MMM dd HH:mm:ss zzz yyyy");
dd.setTimeZone(TimeZone.getTimeZone("GMT"));
Date parse = dd.parse("Out 29 23:00:00 GMT 2011");
Date parse2 = dd.parse("Out 30 23:00:00 GMT 2011");
System.out.println(parse); // Prints "Sun Oct 30 00:00:00 BST 2011"
System.out.println(parse2); // Prints "Sun Oct 30 23:00:00 GMT 2011"
System.out.println(Locale.getDefault()); // Prints "en_US"
System.out.println(TimeZone.getDefault().getID()); // Prints "Europe/London"
}


BST来自​​哪里?它与夏时制有关吗? TimeZone类相反。

    System.out.println(TimeZone.getTimeZone("GMT").inDaylightTime(parse)); // Prints "false"
System.out.println(TimeZone.getTimeZone("GMT").inDaylightTime(parse2)); // Prints "false"


默认系统区域设置为en_US。

编辑:如果我将默认时区更改为格林尼治标准时间,则根据Basil Bourque的回复,我可以将两个打印都显示为格林尼治标准时间:

    TimeZone.setDefault(TimeZone.getTimeZone("GMT"));

最佳答案

神秘

您确定在两个System.out.println行上的注释都正确吗?我希望这两行的输出具有相同的时区,即BSTGMT

如果确定这些正确,请发布完整的工作代码示例。同时记录您的默认语言环境和时区。

充分发挥作用的例子

这是我的代码版本,转换为可以正常工作的示例。我从BSTout推断出这是葡萄牙语巴西语言环境。

java.util.Locale.setDefault( new Locale.Builder().setLanguage( "pt" ).setRegion( "BR" ).build() );  // **HACK* Think twice before ever setting the default of your JVM’s locale or time zone. Generally a bad idea.
java.text.DateFormat dd = new java.text.SimpleDateFormat( "MMM dd HH:mm:ss zzz yyyy" );
dd.setTimeZone( java.util.TimeZone.getTimeZone( "GMT" ) );
Date parse = null;
Date parse2 = null;
try {
parse = dd.parse( "Out 29 23:00:00 GMT 2011" );
parse2 = dd.parse( "Out 30 23:00:00 GMT 2011" );
} catch ( ParseException ex ) {
Logger.getLogger( JodaTimeWork.class.getName() ).log( Level.SEVERE , null , ex );
}
System.out.println( parse );
System.out.println( parse2 );


US区域设置和 America/Los_Angeles时区(即 PDT时区)上运行时的输出。

Sat Oct 29 16:00:00 PDT 2011
Sun Oct 30 16:00:00 PDT 2011


Date对象上没有时区

请注意,没有为java.util.Date对象分配时区。令人困惑的是,该类上的 toString方法实现应用了JVM当前的默认时区。因此,似乎Date对象具有时区,但没有。

正如 GriffeyDog的正确注释所说, DateFormat对象有一个时区,而Date对象没有。

因此,我希望您的 System.out.println这两行都可以在相同的时区发出文本,就像我上面所说的那样。

Joda-Time | java.time

时区的这种混乱处理是避免使用java.util.Date/.Calendar&SimpleTextFormat的众多原因之一。那些知道的人要么使用 Joda-Time库,要么使用Java 8中内置的新的 java.time package。每个都有其优点和缺点。

乔达时代的例子

这是Joda-Time 2.7中的示例。

DateTime上的时区

与java.util.Date对象不同,Joda-Time中的 DateTime对象知道其自己分配的时区。

错误的本地化

您输入的数据对 O使用大写的 Out似乎与葡萄牙语约定不正确。我的示例将其更正为小写。 Joda-Time拒绝将大写字母视为无效。



String input1 = "out 29 23:00:00 GMT 2011";
String input2 = "out 30 23:00:00 GMT 2011";

Locale locale_pt_BR = new Locale.Builder().setLanguage( "pt" ).setRegion( "BR" ).build(); //
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MMM dd HH:mm:ss 'GMT' yyyy" ).withLocale( locale_pt_BR ).withZone( DateTimeZone.UTC );

DateTime dateTime1 = null;
DateTime dateTime2 = null;
DateTime dateTime1_Sao_Paulo = null;
DateTime dateTime2_Sao_Paulo = null;
try {
dateTime1 = formatter.parseDateTime( input1 );
dateTime2 = formatter.parseDateTime( input2 );
// Adjust to "America/Sao_Paulo" time zone.
DateTimeZone zone_Sao_Paulo = DateTimeZone.forID( "America/Sao_Paulo" );
dateTime1_Sao_Paulo = dateTime1.withZone( zone_Sao_Paulo );
dateTime2_Sao_Paulo = dateTime2.withZone( zone_Sao_Paulo );
} catch ( IllegalArgumentException e ) {
// … Handle exception.
System.out.println( "ERROR - Unexpected input for parsing into a date-time object." );
}


转储到控制台。

System.out.println( "dateTime1 : " + dateTime1 );
System.out.println( "dateTime2 : " + dateTime2 );
System.out.println( "Adjusted to America/Sao_Paulo: " + dateTime1_Sao_Paulo + " & " + dateTime2_Sao_Paulo );


运行时。

dateTime1 : 2011-10-29T23:00:00.000Z
dateTime2 : 2011-10-30T23:00:00.000Z
Adjusted to America/Sao_Paulo: 2011-10-29T21:00:00.000-02:00 & 2011-10-30T21:00:00.000-02:00


ISO 8601

如果您对输入数据的格式有任何控制或影响,强烈建议更改为标准 ISO 8601格式。

示例: 2015-02-15T19:39:11Z

时区

避免在时区使用3或4个字母代码。它们既不是标准化的也不是唯一的。例如, BST可以是:


British Summer Time(已于1971年过时,但仍在Google热门歌曲中名列前茅)
Brazil Standard Time
Bangladesh Standard Time


使用 proper time zone names。示例: America/Sao_Paulo

3-4个字母代码被Joda-Time拒绝

由于频繁出现重复值,因此无法负责地解析这些值。因此Joda-Time拒绝尝试。

请注意,在上面的示例代码中,我是如何对期望的 GMT值进行硬编码的。请参阅“ GMT”字母周围的单引号(APOSTROPHE)。这告诉Joda-Time在解析时期望并忽略该字符串。

这有一个关键的结果:由于没有确定的时区或UTC偏移,Joda-Time在解析字符串时不知道如何解释日期时间。我们将格式化程序设置为一个时区,通过该时区可以解释没有时区或偏移量的字符串。如果字符串确实有偏移量,则在格式化程序上设置时区会有不同的行为:解析后,格式化程序会将值调整为该时区。



†更令人困惑的是,java.util.Date实际上确实有一个时区,但深深地嵌入了其实现中。出于大多数实际目的,将忽略该时区。因此,简而言之,我们说一个j.u.Date没有时区(实际上就像在UTC中一样)。

关于java - Java日期强制BST“时区”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28548750/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com