Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

jodatime - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...