How to convert String to Date with TimeZone?

后端 未结 3 1395
借酒劲吻你
借酒劲吻你 2021-01-29 06:42

I\'m trying to convert my String in Date + Timezone. I get my String from a DateTime Variable (here: xyz). My code:

Strin         


        
3条回答
  •  遥遥无期
    2021-01-29 07:24

    "2017-01-03+01:00"

    I thought it a similar ISO 8601 format date string, but actually not ISO 8601. Thanks @Meno Hochschild and @Basil Bourque's indication.

    It is so luck that this method works for such format's string: javax.xml.bind.DatatypeConverter.parseDateTime, it will return a Calendar:

    System.out.println(DatatypeConverter.parseDate("2017-01-03+01:00").getTime());
    

    Output:

    Tue Jan 03 07:00:00 CST 2017

    From the method javadoc:

    public static Calendar parseDate(String lexicalXSDDate)
    Converts the string argument into a Calendar value.

    Parameters: lexicalXSDDate - A string containing lexical representation of xsd:Date.

    Returns: A Calendar value represented by the string argument.

    Throws: IllegalArgumentException - if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:Date.

提交回复
热议问题