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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With