简体   繁体   中英

how to get a timestamp of another timezone in java or JODA

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

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM