简体   繁体   English

将Jodatime的DateTime的日期时区默认为UTC

[英]Defaulting date time zone to UTC for Jodatime's DateTime

I am currently creating UTC DateTime objects using the current idiom 我目前正在使用当前的习惯用法创建UTC DateTime对象

DateTime now = new DateTime(DateTimeZone.UTC);

Is there any way to default so I can create UTC-based DateTime objects using the default constructor so it is more implicit? 有没有办法默认所以我可以使用默认构造函数创建基于UTC的DateTime对象,所以它更隐含?

DateTime now = new DateTime();

If you only want to set the default timezone for joda time, use DateTimeZone.setDefault . 如果您只想为joda时间设置默认时区,请使用DateTimeZone.setDefault


If you want to change the timezone that the whole jvm uses use TimeZone.setDefault method. 如果要更改整个jvm使用的时区,请使用TimeZone.setDefault方法。 Just be sure to set it early as it can be cached by joda time.. quoted from DateTimeZone.getDefault : 请务必尽早设置它,因为它可以通过joda时间缓存..引自DateTimeZone.getDefault

The default time zone is derived from the system property user.timezone. 默认时区源自系统属性user.timezone。 If that is null or is not a valid identifier, then the value of the JDK TimeZone default is converted. 如果该值为null或不是有效标识符,则转换JDK TimeZone缺省值。 If that fails, UTC is used. 如果失败,则使用UTC。

If you are really concerned about the extra chars, then just create a helper method: 如果你真的关心额外的字符,那么只需创建一个辅助方法:

public static DateTime newUTCDateTime() {
  return new DateTime(DateTimeZone.UTC);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM