Reputation: 43953
In java, I download a time from the UK and I am in toronto, I want to get the timezone and change the converted date object to it, but it's not working.
public static Date getDateFromSQLDate(String sqldate) {
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = (Date) formatter.parse(sqldate);
TimeZone tz = TimeZone.getDefault();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.setTimeZone(tz);
return calendar.getTime();
//return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
Does anyone know whats wrong?
Thanks
Upvotes: 1
Views: 1027
Reputation: 41143
DateFormat uses the system's default timezone, which most likely to be Toronto. What you need to do is:
DateFormat formatter = ...
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date date = (Date) formatter.parse(sqldate);
return date;
Or whichever time zone identifier applicable to your "UK" time. You can see the list of supported identifier names here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Keep in mind that the Date object (or java.sql.Date) never has any timezone information stored in it. Saying you want to convert a timezone of a Date object just doesn't make any sense because you can't.
What you can do is to set the timezone from which you parse, or format the Date object.
Upvotes: 2