简体   繁体   English

Java简单日期格式英国时间

[英]Java simple date format british time

I am using simple date format to allow users to specify which time zone they are sending data in: 我使用简单的日期格式,允许用户指定他们发送数据的时区:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,z");

This works fine: eg 这很好用:例如

df.parse("2009-05-16 11:07:41,GMT");

However, if someone is always sending time in London time (ie taking into account daylight savings), what would be the approriate time zone String to add? 但是,如果有人总是在伦敦时间发送时间(即考虑到夏令时),那么要添加的approriate时区字符串是什么? eg this doesnt work: 例如,这不起作用:

df.parse("2009-05-16 11:07:41,Western European Time");  
System.out.println(date);
Sat May 16 12:07:41 BST 2009

I want to match the time to british time taking into daylight savings. 我希望将英国时间与夏令时相匹配。

Thanks. 谢谢。

In daylight saving time, it's BST . 在夏令时,它是BST In the rest of the year it's GMT . 在今年剩下的时间里,这是GMT

I suggest that you use the generic name (for the whole year), which is Europe/London . 我建议您使用通用名称(全年),即Europe/London You can use something like this: 你可以使用这样的东西:

    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]));

The output in this case is: 这种情况下的输出是:

Sat May 16 11:07:41 GMT+01:00 2009

So what exactly is your question - what ID for the TimeZone you should use? 那么究竟是什么问题 - 你应该使用TimeZone ID? The following will print a list of all the available time zone identifiers: 以下将打印所有可用时区标识符的列表:

for (String id : TimeZone.getAvailableIDs()) {
    System.out.println(id);
}

I don't think it's possible to do exactly what you want to do using SimpleDateFormat on its own. 我认为不可能单独使用SimpleDateFormat做你想做的事情。 TimeZone.parse(String) only accepts time zones, not time zone IDs, for example: TimeZone.parse(String)仅接受时区,而不接受时区ID,例如:

Time Zone ID       Time Zone (Winter)      Time Zone (Summer)
-------------------------------------------------------------
Europe/London      GMT                     BST

If parse(...) accepted Europe/London there would be one hour in spring that would not be a valid Europe/London time and one hour in autumn that would map to two UTC times. 如果解析(...)接受欧洲/伦敦,春季将有一个小时不是有效的欧洲/伦敦时间,而秋天的一小时将映射到两个UTC时间。

I think that the best you can do is follow Bruno Rothgiesser's suggestion, however you could accept the time zone ID as a separate user input, or do an additional string processing step to separate the time zone id from the user input string, and use it to work out whether the user probably means GMT or BST. 我认为您可以做的最好的是遵循Bruno Rothgiesser的建议,但是您可以接受时区ID作为单独的用户输入,或者执行额外的字符串处理步骤以将时区ID与用户输入字符串分开,并使用它弄清楚用户是否可能意味着GMT或BST。 The user's Locale might be a better way of working out what he/she means - although there are some assumptions involved in that idea. 用户的Locale可能是一种更好的方法来计算他/她的意思 - 虽然这个想法涉及一些假设。

The "what the user probably means" algorithm has to deal with two special cases - you can use TimeZone.inDaylightTime(Date) with userTime +/- 1 hour to work out if you might have one of these. “用户可能意味着什么”算法必须处理两种特殊情况 - 如果你可能有其中一种,你可以使用TimeZone.inDaylightTime(Date)和userTime +/- 1小时来计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM