如何从字符串在JAVA获得时区

问题描述 投票:4回答:1

我有一个输入“+04:00”

什么API我可以用得到从这个时区?


到目前为止,1)我看着乔达 - https://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html

   static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) 

不知道这是最理想的。

2)我检查时区类。

http://docs.oracle.com/javase/6/docs/api/java/util/TimeZone.html#getTimeZone%28java.lang.String%29

 getTimeZone

public static TimeZone getTimeZone(String ID)

    Gets the TimeZone for the given ID.

    Parameters:
        ID - the ID for a TimeZone, either an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00". Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used. 
    Returns:
        the specified TimeZone, or the GMT zone if the given ID cannot be understood.

有人可以请解释如何给自定义的ID作为输入到这个功能呢?

java timezone jodatime
1个回答
3
投票

http://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html

有一个在TimeZone类的方法称为getTimeZone(String id),你在传递一个字符串,它可以像"GMT-04:00"。您应该检查了这一点。

只要确保你尝试了几个例子,并确保你正确使用它。其中的一些默认方法需要特定的格式。

© www.soinside.com 2019 - 2024. All rights reserved.