gpt4 book ai didi

Java日期解析: distinguish between 28/10/2012 2:00 CET and 28/10/2012 2:00 CEST

转载 作者:行者123 更新时间:2023-11-30 07:25:38 26 4
gpt4 key购买 nike

我有一个日期列表作为字符串,即:

28/10/2012 00:00
28/10/2012 01:00
28/10/2012 02:00
28/10/2012 02:00
28/10/2012 03:00

(同一小时两次是因为夏令时)

而且我需要确保每个日期之间恰好有一个小时。

这里的问题是第一个 28/10/2012 02:00 是 CEST,而第二个是 CET。区分它们很容易,因为第一个是 CEST,第二个是 CET。困难的部分是如何向 SimpleDateFormat(或另一个日期解析类)指定该字符串表示 CEST/CET 时间,以便我可以获得正确的 Date 对象?

提前致谢!

最佳答案

考虑下面的例子我想它有你的答案

convert String to Date with SimpleDateFormat considering CET CEST

public class DatesAndTimesStackOverflow {

final static SimpleDateFormat sdf;
final static TimeZone tz;
static {
tz = TimeZone.getTimeZone( "Europe/Paris" );
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
sdf.setTimeZone(tz);
}

public static void main(String[] args) {

// october clock change should be the following:
outputDateInfo("2012-10-28 02:00:00 CEST");
outputDateInfo("2012-10-28 02:30:00 CEST");
outputDateInfo("2012-10-28 02:00:00 CET");
outputDateInfo("2012-10-28 02:30:00 CET");
outputDateInfo("2012-10-28 03:00:00 CET");
outputDateInfo("2012-10-28 03:30:00 CET");
outputDateInfo("2012-10-28 04:00:00 CET");
}


private static void outputDateInfo(String theDate) {
try {
output("------------------------------------------------------------------------------");
Date d = sdf.parse(theDate);
Calendar c = GregorianCalendar.getInstance(tz);
c.setTimeInMillis(d.getTime());
TimeZone tzCal = c.getTimeZone();

output("String: " + theDate);
output("");
output("Date: " + d); // toString uses current system TimeZone
output("Date Millis: " + d.getTime());
output("Cal Millis: " + c.getTimeInMillis());
output("Cal To Date Millis: " + c.getTime().getTime());
output("Cal TimeZone Name: " + tzCal.getDisplayName());
output("Cal TimeZone ID: " + tzCal.getID());
output("Cal TimeZone DST Name: " + tzCal.getDisplayName(true, TimeZone.SHORT));
output("Cal TimeZone Standard Name: " + tzCal.getDisplayName(false, TimeZone.SHORT));
output("In DayLight: " + tzCal.inDaylightTime(d));

output("");
output("Day Of Month: " + c.get(Calendar.DAY_OF_MONTH));
output("Month Of Year: " + c.get(Calendar.MONTH));
output("Year: " + c.get(Calendar.YEAR));

output("Hour Of Day: " + c.get(Calendar.HOUR_OF_DAY));
output("Minute: " + c.get(Calendar.MINUTE));
output("Second: " + c.get(Calendar.SECOND));

// check to see if this converts back to correct string
String reformat = sdf.format(c.getTime());
if( reformat.equals(theDate) ) {
output("ReConvert: " + reformat + " OK");
} else {
output("ReConvert: " + reformat + " <-------- Error. The converted date is different");
}

} catch (ParseException ex) {
output("Cannot parse this date");
}
}

private static void output(String message) {
System.out.println(message);
}
}

关于Java日期解析: distinguish between 28/10/2012 2:00 CET and 28/10/2012 2:00 CEST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10829820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com