【发布时间】:2015-09-23 18:35:19
【问题描述】:
我有很多日期想存储在使用 BST 的服务器上运行的数据库中:
2015-09-232024-05-072024-03-13
但是它们在数据库中存储为:
2015-09-23 01:00:002024-05-07 01:00:00-
2024-03-13 00:00:0001:00:00
值在存储到数据库之前转换为Date。我在调试时注意到以下几点:
TimeZone timeZone = Calendar.getInstance().getTimeZone();
System.out.println(timeZone.getDisplayName(false, TimeZone.SHORT));
System.out.println(new SimpleDateFormat("zzz").format(new Date()));
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd").withZone(DateTimeZone.UTC);
System.out.println(formatter.parseDateTime("2015-09-23").toDate());
System.out.println(formatter.parseDateTime("2024-05-07").toDate());
System.out.println(formatter.parseDateTime("2024-03-13").toDate());
前两个日期使用BST,最后一个日期使用GMT。是否可以让它们都使用相同的时区?
GMT
BST
Wed Sep 23 01:00:00 BST 2015
Tue May 07 01:00:00 BST 2024
Wed Mar 13 00:00:00 GMT 2024
【问题讨论】:
-
他们不是使用相同的时区,而是分别使用夏季和冬季时间吗?
-
冬天没有英国夏令时!您可以将它们全部设为 GMT。也许
formatter.parseDateTime("2024-03-13").toDateTime(DateTimeZone.UTC).toLocalDate()#toDate()提供了一个 java.util.Date 类型,它取决于 JVM 设置。但是#toLocalDate()留在jodatime 里面。请注意,在您的代码中,您只在要解析的格式上设置时区,而不是打印出 java.util.Date。