Using Joda time to get current wall time for a given Time Zone

前端 未结 3 737
深忆病人
深忆病人 2021-02-02 16:24

The requirement is to simply to get the current wall time (including the correct DST adjustment) for a given Time Zone.

There seems to be a few questi

相关标签:
3条回答
  • 2021-02-02 17:05

    Cleanest Method

    I find the following to be the cleanest method.

    DateTime currentTime = DateTime.now( DateTimeZone.UTC );
    

    This gets you the current time in UTC but this value can be converted to another DateTimeZone or you can replace DateTimeZone.UTC with a different DateTimeZone.

    Using the System TimeZone

    If you want to set it to the system timezone, you can use this:

    DateTime currentTime = DateTime.now( DateTimeZone.getDefault() );
    
    0 讨论(0)
  • 2021-02-02 17:09

    Have you looked at the DateTime constructor:

    DateTime(DateTimeZone zone) 
    

    This constructs a DateTime representing the current time in the specified timezone.

    0 讨论(0)
  • 2021-02-02 17:14

    How about:

    DateTime utc = new DateTime(DateTimeZone.UTC);
    DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    DateTime losAngelesDateTime = utc.toDateTime(tz);
    
    0 讨论(0)
提交回复
热议问题