I need some advice on this java method. The intent of this method is to take a string that represents a date - this string was created from a date in the EST time zone - and convert it to a java Date object in the UTC time zone.
private Date buildValidationDate(String dateString) throws ParseException {
System.out.println("dateString " + dateString);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyy hh:mm a");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dateFormat.setLenient(true);
Date dt = dateFormat.parse(dateString);
System.out.println("dt " + dt);
return dt;
}
the problem I'm seeing is the value of dt seems to be off. For instance, if dateString is '10/16/2012 12:06 PM' - I'm expecting the value of dt (in UTC) to be something like 'Tuesday, October 16, 2012 4:06 PM'. Instead the value of dt is 'Tue Oct 16 07:06:00 CDT 2012'. This does not seem to be the correct UTC time.
I appreciate any advice, I'm sorry if this seems to be an easy question I have a lot of trouble with Java dates. I'm not sure if I'm coding something incorrectly or if there is something wrong with my methodology. Thanks
解决方案
Your date is getting converted right. Its just printing value in your default timezone format as java.util.Date is timezone independent. If you want timezone specific handling, please use java.util.Calendar.