问题
I am currently verifying that a generated Excel sheet contains a correctly-rendered "time stamp" whose date and time part are in separate cells, and whose format is adjusted by a locale.
My test case fails, as when I read back the Excel sheet's time from a String, somehow daylight saving seems to be ignored.
Here's my Java code:
Date now = new Date();
// [... other code, creating and reading the Excel sheet etc. ...]
// from an Excel sheet
String dateString = cellB2.getStringCellValue(); // e.g., 3.7.2013
String timeString = cellB3.getStringCellValue(); // e.g., 13:38:09
TimeZone tz = TimeZone.getDefault(); // Berlin, CEST
dateFormat.setTimeZone(tz); // dateFormat is result of DateFormat.getDateInstance(DateFormat.MEDIUM, locale); e.g. with locale "cs_CZ"
timeFormat.setTimeZone(tz); // timeFormat is result of DateFormat.getTimeInstance(DateFormat.MEDIUM, locale); e.g. with locale "cs_CZ"
// try to parse the time / date strings using the expected format
Date actualDate = dateFormat.parse(dateString); // e.g., Wed Jul 03 00:00:00 CEST 2013
Date actualTime = timeFormat.parse(timeString); // e.g. Thu Jan 01 13:38:09 CET 1970
// now: e.g. Wed Jul 03 13:38:09 CEST 2013
// actualDateTime: e.g. Wed Jul 03 12:48:07 CEST 2013
Date actualDateTime = new Date(actualDate.getTime() + actualTime.getTime());
assertFalse(now.after(actualDateTime)); // fails
回答1:
Robert,
Your solution will work, but I think it's like making one workaround around another and it makes your code more complex, thus harder to mantain. Why not use a SimpleDateFormat with this pattern:
"dd.MM.YYYY kk:mm:ss"
Then, just make a Date String like this:
String dateTime = cellB2.getStringCellValue() + " " + cellB3.getStringCellValue();
Then you can parse it... I did not actually test it, but it should give you the idea, maybe you need to check the pattern String, here's the reference:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Regards
回答2:
Following both Martin's and Matt's suggestions, here is my final program snippet:
// from an Excel sheet
String dateString = cellB2.getStringCellValue();
String timeString = cellB3.getStringCellValue();
TimeZone tz = TimeZone.getDefault();
// NEW!
DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l);
dateTimeFormat.setTimeZone(tz);
Date actualDateTime = dateTimeFormat.parse(dateString + " " + timeString);
assertFalse(now.after(actualDateTime));
回答3:
After setting actualTime, I have to make the following distinction:
if (tz.inDaylightTime(actualDate)) {
actualTime = new Date(actualTime.getTime() + 1000 * 60 * 60);
}
As the time-only String will always result in a Date on 1 January 1970, daylight saving will never be considered (at least not for my Berlin case), and I have to make adjustments manually.
回答4:
Date in java does not add daylight savings time automatically. This is because java Date is calculated by epoch (the number of seconds since January 1, 1970), and represents UTC time.
If you want the class you're using to automatically correct for daylight savings time you can either use the built in Calendar class or, Joda Time. I'd really recommend taking a look at Joda Time. Its better than java.util.Date java.sql.Date and java.util.Calendar combined! It so much easier to deal with one really complete time library than deal with 3 different ones and try to convert between them.
Also, just as a funny aside that shows how difficult DST can be, see this video - at about the 4 minute mark it explains how different countries implement DST and what a headache this causes for everyone.
来源:https://stackoverflow.com/questions/17447481/daylight-saving-ignored-when-creating-a-java-date-using-new-dateadate-gettime