我使用简单的日期格式来允许用户指定他们在哪个时区发送数据:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,z");
这很好用: 例如
df.parse("2009-05-16 11:07:41,GMT");
但是,如果有人总是发送伦敦时间的时间(即考虑到夏令时),要添加的适当时区字符串是什么? 例如这不起作用:
df.parse("2009-05-16 11:07:41,Western European Time");
System.out.println(date);
Sat May 16 12:07:41 BST 2009
我想将时间与考虑夏令时的英国时间相匹配。
谢谢。
最佳答案
在夏令时,它是 BST
。在这一年的剩余时间里,它是 GMT
。
我建议您使用通用名称(全年),即 Europe/London
。你可以使用这样的东西:
String userInput = "2009-05-16 11:07:41,Europe/London";
String[] tokens = userInput.split(",");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone(tokens[1]));
System.out.println(df.parse(tokens[0]));
这种情况下的输出是:
Sat May 16 11:07:41 GMT+01:00 2009
关于Java简单日期格式英国时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2965681/