Reputation: 2520
i want to get current time (now) from Different time zone .
for example using joda datetime library,
I can get Australian time zone like using JODA datetime
DateTime zoned = new DateTime(DateTimeZone.forID("Australia/Melbourne"));
and its current time Using
DateTime.now(DateTimeZone.forID("Australia/Melbourne"));
if i want to convert this DateTime object into java.sql.Timestamp object
,i have to get its milliseconds using
getMillis method of DateTime class to instantaite new Timestamp Object
Timestamp zonedStamp = new TimeStamp(zoned.getMillis());
so every time the passed milliseconds since the epoch time would be the same logically for each timezone.
My question is how i can get Autralian Time zone's current time to get a zoned Timestamp Object.
Thank You Mihir Parekh
Upvotes: 0
Views: 6210
Reputation: 34367
If you want a Timestamp
object with Australian timezone equivalent time value then try below:
Date currentTime = new Date();
DateFormat ausFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
ausFormat.setTimeZone(TimeZone.getTimeZone("Australia/Melbourne"));
//get the time string in australian timezone
String ausTime = ausFormat.format(currentTime);
//Convert the above time string in local date object
DateFormat currentFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
//optional: set the timezone as Asia/Calcutta
currentFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
Date ausTimeInLocal = currentFormat.parse(ausTime);
//get the time stamp object using above date object
Timestamp ausTimeStampInLocal = new Timestamp(ausTimeInLocal.getTime());
Upvotes: 1