Hi,
I just wrote a small program using joda to understand time conversion across
timezones.
I had set the current system time zone as IST(GMT + 5.30).
The current time is around 2009-03-05 10:12:41 AM IST
I executed following program which converted local time(IST) to UTC.
import java.sql.Timestamp;
import org.joda.time.DateTimeZone;
public class TestLocalToUTC {
public static void main(String[] args) {
long currentTime = System.currentTimeMillis();
System.out.println("IST Timestamp:"+new Timestamp(currentTime));
System.out.println("UTC
Timestamp:"+getUTCTimeFromLocal(currentTime));
}
public static Timestamp getUTCTimeFromLocal(long localTimeInMillis) {
DateTimeZone dateTimeZone = DateTimeZone.getDefault();
long utcTime =
dateTimeZone.convertLocalToUTC(localTimeInMillis, false);
System.out.println("UTC Time:"+utcTime);
Timestamp utcTimestamp = new Timestamp(utcTime);
return utcTimestamp;
}
}
Result:
IST Timestamp:2009-03-05 10:12:41.415
UTC Time:1236208361415
UTC Timestamp:2009-03-05 04:42:41.415
Now I had set my system timezone as PST(GMT – 8.00). I used the UTC
milliseconds got from above program and executed below code snippet which
converts UTC to local(PST).
import java.sql.Timestamp;
import org.joda.time.DateTimeZone;
public class TestUTCToLocal {
public static void main(String[] args) {
System.out.println("PST
Timestamp:"+getLocalTimeFromUTC(1236208361415L));
}
public static Timestamp getLocalTimeFromUTC(long utcTimeInMillis) {
//UTC to Local
DateTimeZone dateTimeZone = DateTimeZone.getDefault();
System.out.println("DateTimeZone : " + dateTimeZone);
long localTime =
dateTimeZone.convertUTCToLocal(utcTimeInMillis);
System.out.println("Converted Local Time : " + localTime);
Timestamp localTimestamp = new Timestamp(localTime);
return localTimestamp;
}
}
Result:
DateTimeZone : America/Los_Angeles
Converted Local Time : 1236179561415
PST Timestamp:2009-03-04 07:12:41.415
The PST timestamp that I got is 2009-03-04 07:12:41 A.M. PST
Actually 2009-03-05 10:12:41 IST should give around 2009-03-04 8.45 PM PST.
Can anyone help if my observation is right or Am I doing something wrong? Will
Joda not support across timezone conversion?
Thanks,
Suganthi.
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Joda-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/joda-interest