2012-12-06 24 views
0

我在使用Joda时间库获取本地时间时遇到问题。这是我想要做的一个简单的例子。从DateTimeObject获取LocalDateTime

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日期时间。

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

我在这里错过了什么?

回答

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?

+0

嗯,这确实是问题,我试图让我的默认时区是UTC。另一方面,我的电脑时钟设置为UTC-3,夏令时。这怎么可能? (usign windows xp,如果有的话) –

+0

@ThiagoMoraes:也许你的其他代码中的一些将默认时区设置为UTC?不知道更多,很难说。我建议你尝试一个简单的控制台应用程序,它只是*打印出默认的时区。如果这显示了你的“真实”时区,那么你必须在其他代码中进行冒充。 –

+0

我已经完成了这个测试,只需插入“DateTimeZone d = DateTimeZone.getDefault();”在你的解决方案的开始,然后进入调试。如果我找到有用的东西,我会搜索一下,然后回来。谢谢! –

1

你可以试试下面的(我在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 
+0

+1,指出在很多情况下,DateTime类可能比LocalDateTime更好地实现“本地”日期/时间。这完全取决于“本地”的含义 - 本地PC或设备,并基于当前时区,或完全独立于所有时区。 – RenniePet