Java 如何为 joda DateTime 对象指定时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23939576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:14:25  来源:igfitidea点击:

How to specify a timezone for a joda DateTime object?

javajodatime

提问by BarFooBar

I have a function that converts a string into a joda time object and formats it.

我有一个函数可以将字符串转换为 joda 时间对象并对其进行格式化。

public static String getFormattedDatetime(String someDate) {
    DateTime newDate = DateTime.parse(someDate, DateTimeFormat.forPattern("yy-MM-dd HH:mm:ss.SSS")).minusHours(7);
    String formattedDate = newDate.toString("dd-MMM-yy hh:mm a");

    return formattedDate;
}

The function takes the string and returns a formatted DateTime just fine, but I'm having troubles assigning a TimeZone to it. The string that I'm passing in represents a UTC time from the database that I need to convert to PST/PDT. I can't figure out a way to 1) assign the new DateTime object a timezone and 2) convert that object's timezone to PST/PDT. Right now I am handling this with .minusHours. Certainly not ideal.

该函数接受字符串并返回格式化的 DateTime 就好了,但是我在为其分配 TimeZone 时遇到了麻烦。我传入的字符串表示我需要转换为 PST/PDT 的数据库中的 UTC 时间。我想不出一种方法来 1) 为新的 DateTime 对象分配一个时区,以及 2) 将该对象的时区转换为 PST/PDT。现在我正在用 .minusHours 处理这个问题。当然不理想。

采纳答案by durron597

Under no circumstances should you use .minusHours(7)as it will be wrong half the year, and the DateTimeobject will still think it's in UTC.

在任何情况下都不应该使用,.minusHours(7)因为它会在半年中出错,并且DateTime对象仍然会认为它是 UTC。

Use .withZone(DateTimeZone.forID("America/Los_Angeles"));

.withZone(DateTimeZone.forID("America/Los_Angeles"));

Here is a list of all time zones supported by Joda Time with their corresponding IDs

这是 Joda Time 支持的所有时区及其相应 ID 的列表

I recommend refactoring the constant generated by the forID()call into a static finalfield somewhere appropriate in your code, and using that everywhere you need to do the conversion.

我建议将forID()调用生成的常量重构为static final代码中适当位置的字段,并在需要进行转换的任何地方使用它。



You are probably aware, but to be on the safe side DateTimeobjects are IMMUTABLE, which means the withZonecall will return a brand new DateTimeinstance, and will notchange the zone of the original object.

您可能知道,但安全起见,DateTime对象是IMMUTABLE,这意味着withZone调用将返回一个全新的DateTime实例,并且不会更改原始对象的区域。



@anotherdave makes a great point in the comments. You say PSTin your question, but PSTis only in use half the year (because of Daylight savings). I assumed you meant the current time in Los Angeles, Seattle, etc. and not specifically the PSTzone, as those cities use PDTin the other half of the year. However, if you DO want PST100% of the time, you can use forID("PST");

@anotherdave 在评论中提出了一个很好的观点。您在问题中说PST,但PST只使用半年(由于夏令时)。我假设您指的是洛杉矶、西雅图等地的当前时间,而不是具体的PST区域,因为这些城市在一年的另一半使用PDT。但是,如果您确实希望100% 的时间使用PST,则可以使用forID("PST");

回答by BarFooBar

Solved:

解决了:

public static String getFormattedDatetime(String someDate) {

    DateTime newDate = DateTime.parse(someDate, DateTimeFormat.forPattern("yy-MM-dd  HH:mm:ss.SSS"));
        LocalDateTime zonedDate = LocalDateTime.parse(newDate, DateTimeFormat.forPattern("yy-MM-dd HH:mm:ss.SSS"));
        DateTime finalZonedDate = zonedDate.toDateTime(DateTimeZone.UTC);
        String formattedDate = newDate.toString("dd-MMM-yy hh:mm a");
        return formattedDate;
    }

I had to use joda's LocalDateTimeclass to assign a timezone when I was converting the string to a variable. Then I had to convert the zone separately.

LocalDateTime当我将字符串转换为变量时,我不得不使用 joda 的类来分配时区。然后我不得不单独转换区域。