Java SimpleDataFormat会导致错误的时间
我有以下代码Java SimpleDataFormat会导致错误的时间,java,simpledateformat,date-format,Java,Simpledateformat,Date Format,我有以下代码 protected void amethod1() { String strDate = "Thu May 18 16:24:59 UTC 2017"; String dateFormatStr = "EEE MMM dd HH:mm:ss zzz yyyy"; DateFormat dateFormat = new SimpleDateFormat(dateFormatStr); Date formattedDate = null;
protected void amethod1() {
String strDate = "Thu May 18 16:24:59 UTC 2017";
String dateFormatStr = "EEE MMM dd HH:mm:ss zzz yyyy";
DateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
Date formattedDate = null;
try {
formattedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
formattedDate
的结果值为“2017年5月18日星期四11:24:59 CDT”
我正在芝加哥测试这段代码,当地时区是CDT。我无法理解为什么时间值从16:24:59更改为11:24:59,即使如此。我是否在定义的日期格式中遗漏了某些内容?类
date
根本不包含任何时区。从1970年1月1日00:00:00 GMT开始只有几毫秒。如果您尝试使用System.out.println
或调试器查看formattedDate
包含的内容,您将获得本地时区的格式化日期11:24:59 CDT
和16:24:59 UTC
是同一时间,因此结果是正确的
为了更好地管理时区,最好使用
jodatime
或java8时间API
。ClassDate
根本不包含任何时区。从1970年1月1日00:00:00 GMT开始只有几毫秒。如果您尝试使用System.out.println
或调试器查看formattedDate
包含的内容,您将获得本地时区的格式化日期11:24:59 CDT
和16:24:59 UTC
是同一时间,因此结果是正确的
为了更好地管理时区,最好使用
jodatime
或Java8时间API
。SimpleDateFormat myFmt=newsimpledateformat(“yyyy-MM-dd HH:MM:ss”);
现在日期=新日期();
System.out.println(myFmt.format(现在));
我希望我能帮助你。如果可以,请采用。谢谢
SimpleDateFormat myFmt=新的SimpleDateFormat(“yyy-MM-dd HH:MM:ss”);
现在日期=新日期();
System.out.println(myFmt.format(现在));
我希望我能帮助你。如果可以,请采用。谢谢formattedDate的结果值为“2017年5月18日星期四11:24:59 CDT”为什么?因为您的时区运行时间为UTC时间的5小时,您可以在下面的链接wiki时区缩写中找到,如果您想要得到相同的时区,您需要在Formatter中指定时区,希望您得到我的关注
formattedDate的结果值为“2017年5月18日星期四11:24:59 CDT”为什么?因为您的时区运行时间为UTC时间的5小时,您可以在下面的链接wiki时区缩写中找到,如果您想要得到相同的时区,您需要在Formatter中指定时区,希望您得到我的关注
您指定了时区,这就是为什么在解析当前时区(您所在的时区)上的时间后,SimpleDataFormat会设置UTC时区。当您尝试输出日期时,它会显示在您指定的时区的当前时区上,这就是为什么在解析当前时区(您所在的位置)上的时间后,SimpleDataFormat会设置UTC时区。当您尝试输出日期时,它会显示在您当前的时区上。当您为例如设置timezone.setDefault(timezone.getTimeZone(“PST”)的日期格式时,似乎还需要指定时区代码>
查看此讨论在格式化例如的日期时,似乎还需要指定时区。
TimeZone.setDefault(TimeZone.getTimeZone(“PST”)代码>
请看一下此讨论首先,您得到了正确的时间。当芝加哥使用夏时制时(5月18日),时间是11:24:59,而UTC是16:24:59。因此,您的Date
值表示相同的时间点。这就是您对日期的所有期望
我知道您不仅需要一个时间点,还需要UTC时区。因为,我只想填写细节:
DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern(dateFormatStr, Locale.US);
ZonedDateTime dateTime = ZonedDateTime.parse(strDate, parseFormatter);
结果是
2017-05-18T16:24:59Z[UTC]
如果您总是想要UTC时区,Instant
类正好适合它,因此您可能需要转换为它:
Instant instant = dateTime.toInstant();
一般来说,瞬间总是以UTC为单位。首先,您得到了正确的时间。当芝加哥使用夏时制时(5月18日),时间是11:24:59,而UTC是16:24:59。因此,您的Date
值表示相同的时间点。这就是您对日期的所有期望
我知道您不仅需要一个时间点,还需要UTC时区。因为,我只想填写细节:
DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern(dateFormatStr, Locale.US);
ZonedDateTime dateTime = ZonedDateTime.parse(strDate, parseFormatter);
结果是
2017-05-18T16:24:59Z[UTC]
如果您总是想要UTC时区,Instant
类正好适合它,因此您可能需要转换为它:
Instant instant = dateTime.toInstant();
通常来说,时间总是以UTC为单位。日期的输出取决于指定的格式,您可以在其中指定时区,如下例所示:
protected void amethod2() {
String strDate = "Thu May 18 16:24:59 UTC 2017";
String dateFormatStr = "EEE MMM dd HH:mm:ss zzz yyyy";
DateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
Date formattedDate = null;
try {
formattedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Date: " + formattedDate);
// Thu May 18 17:24:59 BST 2017, BST is my system default timezone
// Set the time zone to UTC for the calendar of dateFormat
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("Date in timezone UTC: " + dateFormat.format(formattedDate));
// Thu May 18 16:24:59 UTC 2017
// Set the time zone to America/Chicago
dateFormat.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
System.out.println("Date in timezone America/Chicago: " + dateFormat.format(formattedDate));
// Thu May 18 11:24:59 CDT 2017
}
至于ID,例如示例中的“UTC”和“America/Chicago”,您可以通过TimeZone.getAvailableIDs()
获得它们的完整列表。您可以打印出来查看:
Arrays.stream(java.util.TimeZone.getAvailableIDs()).forEach(System.out::println)代码>
您将有:
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
...
日期的输出取决于指定的格式,您可以在其中指定时区,如下例所示:
protected void amethod2() {
String strDate = "Thu May 18 16:24:59 UTC 2017";
String dateFormatStr = "EEE MMM dd HH:mm:ss zzz yyyy";
DateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
Date formattedDate = null;
try {
formattedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Date: " + formattedDate);
// Thu May 18 17:24:59 BST 2017, BST is my system default timezone
// Set the time zone to UTC for the calendar of dateFormat
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("Date in timezone UTC: " + dateFormat.format(formattedDate));
// Thu May 18 16:24:59 UTC 2017
// Set the time zone to America/Chicago
dateFormat.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
System.out.println("Date in timezone America/Chicago: " + dateFormat.format(formattedDate));
// Thu May 18 11:24:59 CDT 2017
}
至于ID,例如示例中的“UTC”和“America/Chicago”,您可以通过TimeZone.getAvailableIDs()
获得它们的完整列表。您可以打印出来查看:
Arrays.stream(java.util.TimeZone.getAvailableIDs()).forEach(System.out::println)代码>
您将有:
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
...
您将日期声明为UTC,并在CDT中获得结果,因此差异是合乎逻辑的。你是想在UTC得到结果吗?@Nathan-是的,没错。我希望结果是UTC。java.util.Date
将时间转换为系统的时区。要在UTC中获得它,您可以将日期转换为。您应该首先将日期声明为UTC,然后在CDT中获得结果,因此差异是合乎逻辑的。你是不是想找我