I tried the following code that should allow me to convert the time pass as parameter to a string with one hour more (GMT+1).
But I always get the same time I have in my object Time
public static String getFormattedTimeLabel(final Time time) {
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(time.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
sdf.setCalendar(cal);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(cal.getTime());
}
Does anybody know how to solve it?
EDIT : Ok so finally I created the following function in my controller.
The timezone I set in the Simple date format is the timezone of the client recover thanks to HttpServletRequest.
You can see the printed result of System.out after the code.
private Time getTimeLocal(Time requestTime) {
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(requestTime.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
sdf.setCalendar(cal);
System.out.println(requestTime);
sdf.setTimeZone(tz);
System.out.println(tz.getID());
String dt = sdf.format(cal.getTime());
Date date = null;
try {
date = sdf.parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(dt);
return new Time(date.getTime());
}
10:00:00 //the Time object
Europe/Paris // The id of the client timezone (GMT+1 for me)
10 : 00 //the date. the time printed is not correct it should be set with the timezone of the client so for me GMT+1 so 11:00:00