For certain input values though format and all things are correct, I am getting ParseException from SimpleDateFormat.parse(String input) method.
I'm in a scenario where I need to convert time from PST timezone to local timezone; it works almost all of the time for acceptable input except for 2013-03-10T02:00:00Z to 2013-03-10T02:59:59Z. It seems strange; I tried several JDKs and machines, but the result is the same, it throws
Method threw 'java.text.ParseException' exception.
My expectation is to parse this date properly and return non null date object instead of throwing exception.
Snippet to test this:
public static void main(String[] args) throws Exception {
System.out.println(getDateFromString("2013-03-10T02:59:59Z", "yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH));
}
public static Date getDateFromString(String dateText, String format, Locale locale) {
Date date = null;
SimpleDateFormat simpleDateFormat = null;
try {
simpleDateFormat = new SimpleDateFormat(format, locale);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("PST"));
//Supporting strict validation of date format
simpleDateFormat.setLenient(false);
date = simpleDateFormat.parse(dateText);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}