我在使用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
我在这里错过了什么?
嗯,这确实是问题,我试图让我的默认时区是UTC。另一方面,我的电脑时钟设置为UTC-3,夏令时。这怎么可能? (usign windows xp,如果有的话) –
@ThiagoMoraes:也许你的其他代码中的一些将默认时区设置为UTC?不知道更多,很难说。我建议你尝试一个简单的控制台应用程序,它只是*打印出默认的时区。如果这显示了你的“真实”时区,那么你必须在其他代码中进行冒充。 –
我已经完成了这个测试,只需插入“DateTimeZone d = DateTimeZone.getDefault();”在你的解决方案的开始,然后进入调试。如果我找到有用的东西,我会搜索一下,然后回来。谢谢! –