My requirment is like this:
I am saving time in millisecond in database and the timezone.For example the time in milisecond is 1223123123232 in long and timezone is Asia/Calcutta. I have to convert it to Africa/Asmara timezone.
long l = 1223123123232l;
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
long tzOff = tz.getOffset(l);
java.util.Date d = new Date(l-tzOff); // WHY THIS??
DateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
df.setTimeZone(TimeZone.getTimeZone("Africa/Asmara"));// required timezone
String s = df.format(d);
System.out.println(s);
To check i am refering this: link
My question is:
- If the timezone is just the representation of time in different formats(geographical areas offset from GMT),why do i need to subtract the offset time form the actual time
(l-tzOff)? - Why can’t i ignore the timezone which is saved in Database, and only consider the timezome in which i want to convert the date?
Something like:
long l = 1223123123232l;
java.util.Date d = new Date(l);
DateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
df.setTimeZone(TimeZone.getTimeZone("Africa/Asmara"));// required timezone
String s = df.format(d);
System.out.println(s);
My system timezone is Asia/Calcutta, i want to convert a Date in Africa/Bujumbura timezone to Europe/Vatican timezone.The above code is not working in this case? Why this is so?
Java dates do not know about time zones. Therefore, if I save 14H00 in New-York, it will not be the same 14H00 as in Paris, although the millisecond value is the same. You need to use a unique reference to save dates. People often chose GMT+0.
If you need to check local time across timezones, you can use a tool I developed here.
To answer your questions: