【问题标题】:Get LocalDateTime from a DateTimeObject从 DateTime 对象获取 LocalDateTime
【发布时间】:2012-12-06 13:04:29
【问题描述】:

我在使用 Joda 时间库获取本地时间时遇到问题。这是我正在尝试做的一个简单示例。

package simple;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class TestJodaLocalTime {

    public static void main(String[] args) {
        //time in UTC standard
        DateTime processingDate = DateTime.now();
        //local time
        LocalDateTime localDateTime = processingDate.
                withZone(DateTimeZone.getDefault()).toLocalDateTime();

        DateTimeFormatter fmtDate = DateTimeFormat.forPattern("dd/MM/yyyy");
        DateTimeFormatter fmtHour = DateTimeFormat.forPattern("HH:mm:ss");

        System.out.println("Date: " + fmtDate.print(processingDate));
        System.out.println("Time: " + fmtHour.withZone(DateTimeZone.UTC).
                print(processingDate));
        System.out.println("Local date: " + fmtDate.print(localDateTime));
        System.out.println("Local time: " + fmtHour.print(localDateTime));

    }

}

我期待在打印本地时间时,我会从我的应用程序运行的时区获取时间,但我得到的时间与 UTC DateTime 相同。

Date: 06/12/2012
Time: 12:55:49
Local date: 06/12/2012
Local time: 12:55:49

我在这里错过了什么?

【问题讨论】:

    标签: java timezone jodatime


    【解决方案1】:

    这条评论可能是问题的一部分:

    //time in UTC standard
    DateTime processingDate = DateTime.now();
    

    否,DateTime.now() 返回默认时区的当前时间。因此,当您稍后使用 withZone(DateTimeZone.getDefault()) 时,这是一个空操作。

    但是,我仍然希望 fmtHour.withZone(DateTimeZone.UTC).print(processingDate) 转换为 UTC。

    确实,当我手动将默认时区设置为 UTC 以外的其他时区时,我会得到这样的结果:

    import org.joda.time.*;
    import org.joda.time.format.*;
    
    public class Test {
        public static void main(String[] args) {
            DateTimeZone pacific = DateTimeZone.forID("America/Los_Angeles");
            DateTimeZone.setDefault(pacific);
    
            DateTime processingDate = DateTime.now();
            //local time
            LocalDateTime localDateTime = processingDate.
                withZone(DateTimeZone.getDefault()).toLocalDateTime();
    
            DateTimeFormatter fmtDate = DateTimeFormat.forPattern("dd/MM/yyyy");
            DateTimeFormatter fmtHour = DateTimeFormat.forPattern("HH:mm:ss");
    
            System.out.println("Date: " + fmtDate.print(processingDate));
            System.out.println("Time: " + fmtHour.withZone(DateTimeZone.UTC).
                               print(processingDate));
            System.out.println("Local date: " + fmtDate.print(localDateTime));
            System.out.println("Local time: " + fmtHour.print(localDateTime));
        }
    }
    

    输出:

    Date: 06/12/2012
    Time: 13:19:35
    Local date: 06/12/2012
    Local time: 05:19:35
    

    您确定问题不只是您的默认时区 UTC?

    【讨论】:

    • 嗯,这确实是问题所在,我试图获取我的默认时区,它是 UTC。另一方面,我的计算机时钟设置为 UTC-3 夏令时。这怎么可能? (使用 windows xp,如果这很重要)
    • @ThiagoMoraes:也许您的一些其他代码将默认时区设置为 UTC?不知道更多就很难说。我建议您尝试一个简单的控制台应用程序,它只是 打印出默认时区。如果这显示了您的“真实”时区,那么您一定是在某处捏造了其他代码。
    • 我通过插入“DateTimeZone d = DateTimeZone.getDefault();”完成了这个测试在解决方案的开头,然后进入调试。如果我发现任何有用的东西,我会搜索一下并回来。谢谢!
    【解决方案2】:

    您可以尝试以下方法(我在 GMT+2:00 时区):

    public class TestJodaLocalTime {
    
        public static void main(String[] args) {
            DateTime utcNow = DateTime.now(DateTimeZone.UTC);
            DateTime localNow = utcNow.withZone(DateTimeZone.getDefault());
    
            DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinuteSecond();
    
            System.out.println("UTC   now: " + fmt.print(utcNow));
            System.out.println("Local now: " + fmt.print(localNow));
        }
    
    }
    

    输出:

    UTC   now: 2012-12-07T17:43:43
    Local now: 2012-12-07T19:43:43
    

    【讨论】:

    • +1 指出在许多情况下 DateTime 类在实现“本地”日期/时间方面可能比 LocalDateTime 更好。这完全取决于您所说的“本地”是什么意思 - 此 PC 或设备的本地并基于其当前时区,或完全独立于所有时区。
    猜你喜欢
    • 2022-01-24
    • 2014-09-17
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2011-05-05
    • 1970-01-01
    相关资源
    最近更新 更多