Java 日期强制 BST "timezone"
Java Date forcing BST "timezone"
在 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 class 另有说明。
System.out.println(TimeZone.getTimeZone("GMT").inDaylightTime(parse)); // Prints "false"
System.out.println(TimeZone.getTimeZone("GMT").inDaylightTime(parse2)); // Prints "false"
默认系统区域设置为 en_US。
编辑:根据 Basil Bourque 的回复,如果我将默认时区更改为 GMT,我可以获得 GMT 日期的两张图片:
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
神秘
您确定您对 System.out.println
两行的评论都是正确的吗?我希望两条线的输出具有相同的时区,BST
或 GMT
.
如果您确定这些是正确的,请post一个完整的工作代码示例。还要记录您的默认语言环境和时区。
完整的示例
这是我的代码版本,已转换为一个完整的示例。我从 BST
和 out
推断这是葡萄牙语巴西语言环境。
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 对象没有指定时区†。令人困惑的是,class 上的 toString
方法实现应用了 JVM 当前的默认时区。所以它 似乎 Date 对象有时区,但实际上没有。
正如 GriffeyDog 的正确评论所说,DateFormat
对象有时区,但 Date 对象没有。
所以我希望你的两行 System.out.println
发出具有相同时区的文本,正如我在上面所说的那样。
乔达时间 | java.time
这种令人困惑的时区处理是避免使用 java.util.Date/.Calendar & SimpleTextFormat 的众多原因之一。知情人士使用 Java 8 中内置的 Joda-Time library or the new java.time package。java.time 包的灵感来自 Joda-Time,但经过了重新架构;每个都有其优点和缺点。
Joda-Time 中的示例
这是 Joda-Time 2.7 中的示例。
时区 DateTime
Joda-Time 中的 DateTime
对象知道自己指定的时区,这与 java.util.Date 对象不同。
本地化不正确
您的输入数据对 Out
使用大写字母 O
似乎不符合葡萄牙语惯例。我的示例将其更正为小写。 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
.
Joda-Time 拒绝的 3-4 个字母代码
由于经常出现重复值,因此无法负责任地解析此类值。所以 Joda-Time 拒绝尝试。
请注意上面的示例代码中我是如何对预期的 GMT
值进行硬编码的。查看 "GMT" 字母周围的单引号 (撇号)。这告诉 Joda-Time 在解析时期望并忽略该字符串。
这有一个重要的后果:由于没有确定的时区或与 UTC 的偏移量,Joda-Time 在解析字符串时不知道如何解释日期时间。我们将格式化程序设置为一个时区,通过该时区来解释没有时区或偏移量的字符串。如果字符串 did 有偏移量,在格式化程序上设置时区会有不同的行为:after 解析,格式化程序将值调整为时区。
† 更令人困惑的是,java.util.Date 实际上 确实 有一个时区,但隐藏在其实现的深处。出于大多数实际目的,该时区被忽略。因此,对于 shorthand,我们说 j.u.Date 没有时区(实际上就像在 UTC 中一样)。
在 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 class 另有说明。
System.out.println(TimeZone.getTimeZone("GMT").inDaylightTime(parse)); // Prints "false"
System.out.println(TimeZone.getTimeZone("GMT").inDaylightTime(parse2)); // Prints "false"
默认系统区域设置为 en_US。
编辑:根据 Basil Bourque 的回复,如果我将默认时区更改为 GMT,我可以获得 GMT 日期的两张图片:
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
神秘
您确定您对 System.out.println
两行的评论都是正确的吗?我希望两条线的输出具有相同的时区,BST
或 GMT
.
如果您确定这些是正确的,请post一个完整的工作代码示例。还要记录您的默认语言环境和时区。
完整的示例
这是我的代码版本,已转换为一个完整的示例。我从 BST
和 out
推断这是葡萄牙语巴西语言环境。
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 对象没有指定时区†。令人困惑的是,class 上的 toString
方法实现应用了 JVM 当前的默认时区。所以它 似乎 Date 对象有时区,但实际上没有。
正如 GriffeyDog 的正确评论所说,DateFormat
对象有时区,但 Date 对象没有。
所以我希望你的两行 System.out.println
发出具有相同时区的文本,正如我在上面所说的那样。
乔达时间 | java.time
这种令人困惑的时区处理是避免使用 java.util.Date/.Calendar & SimpleTextFormat 的众多原因之一。知情人士使用 Java 8 中内置的 Joda-Time library or the new java.time package。java.time 包的灵感来自 Joda-Time,但经过了重新架构;每个都有其优点和缺点。
Joda-Time 中的示例
这是 Joda-Time 2.7 中的示例。
时区 DateTime
Joda-Time 中的 DateTime
对象知道自己指定的时区,这与 java.util.Date 对象不同。
本地化不正确
您的输入数据对 Out
使用大写字母 O
似乎不符合葡萄牙语惯例。我的示例将其更正为小写。 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
.
Joda-Time 拒绝的 3-4 个字母代码
由于经常出现重复值,因此无法负责任地解析此类值。所以 Joda-Time 拒绝尝试。
请注意上面的示例代码中我是如何对预期的 GMT
值进行硬编码的。查看 "GMT" 字母周围的单引号 (撇号)。这告诉 Joda-Time 在解析时期望并忽略该字符串。
这有一个重要的后果:由于没有确定的时区或与 UTC 的偏移量,Joda-Time 在解析字符串时不知道如何解释日期时间。我们将格式化程序设置为一个时区,通过该时区来解释没有时区或偏移量的字符串。如果字符串 did 有偏移量,在格式化程序上设置时区会有不同的行为:after 解析,格式化程序将值调整为时区。
† 更令人困惑的是,java.util.Date 实际上 确实 有一个时区,但隐藏在其实现的深处。出于大多数实际目的,该时区被忽略。因此,对于 shorthand,我们说 j.u.Date 没有时区(实际上就像在 UTC 中一样)。