Hi,
I have a scenario where I take the time zone selected by the user from a drop down in the web page, and the form also takes the timings for all week days in the format as shown in this Ex - ‘09:30:00’ to ‘17:00:00’ . It is considered that the timings entered are w.r.t the timezone selected.
Let us assume that the user selected GMT+5:30(IST), as the time zone and enters start time to be 09:00:00 and end time to be 16:30:00, how do i convert these two timings to GMT so that i get 03:30:00 as start time and 11:00:00 as end time using java.
Thanks in advance
Time is an interesting thing. On Earth, no mater where you are, it is always the exact same time, people just perceive it differently.
The goal with time is to get it down to the milliseconds and store the time as milliseconds (most databases do this automajically) so conversion is easy.
Get the user to enter the time and the timezone and then use the GregorianCalendar class:
TimeZone timeZone = TimeZone.getTimeZone(<your time zone the user entered>);
GregorianCalendar gregorianCalendar = new GregorianCalendar(timeZone);
// CAREFUL WITH SOME OF THIS SET DATES IN GREGORIANCALENDAR.
// Some start with 0 instead of 1
gregorianCalendar.set(GregorianCalendar.MONTH, <your month>);
gregorianCalendar.set(GregorianCalendar.DAY_OF_MONTH, <your day of month>);
gregorianCalendar.set(GregorianCalendar.HOUR, <your hour>);
gregorianCalendar.set(GregorianCalendar.MINUTE, <your minute>);
gregorianCalendar.set(GregorianCalendar.SECOND, 0);
gregorianCalendar.set(GregorianCalendar.MILLISECOND, 0);
Date date = gregorianCalendar.getTime();
There you go. Simple as cake.
For more information get a book called “Java Internationalization” or perform a search on google of the same title.