Java Parse Date Pattern YYYY parseXsdDateTime(String date)

Here you can find the source of parseXsdDateTime(String date)

Description

Parses a String that is formatted according to the xsd:dateTime data type and returns it as a Date

License

Open Source License

Parameter

Parameter Description
date the String to format.

Return

the parse date.

Declaration

public static Date parseXsdDateTime(String date) throws ParseException 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

public class Main {
    private static SimpleDateFormat XSD_DATE_TIME_FORMAT = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ssZ");

    /**/*from  w w  w  . jav a 2s .c  om*/
     * Parses a String that is formatted according to the xsd:dateTime data type
     * and returns it as a Date
     * @param date the String to format.
     * @return the parse date.
     */
    public static Date parseXsdDateTime(String date) throws ParseException {
        // Trim any whitespace (e.g. a newline at the end of the string)
        String newDate = date.trim();
        if (newDate.endsWith("Z")) {
            // Remove the Z and replace with "+0000"
            newDate = newDate.substring(0, newDate.length() - 1) + "+0000";
        } else {
            // Remove the last colon from the string (i.e. the time offset)
            int colonPos = newDate.lastIndexOf(":");
            newDate = newDate.substring(0, colonPos)
                    + newDate.substring(colonPos + 1, newDate.length());
        }
        return XSD_DATE_TIME_FORMAT.parse(newDate);
    }
}

Related

  1. parseW3CDateTime(String date)
  2. parseW3CDateTime(String sDate, Locale locale)
  3. parseWsDate(String date)
  4. parseXEP0082Date(String dateString)
  5. parseXsdDate(final Date date)
  6. parseYmd(String date)
  7. parseYmd(String s)
  8. parseYmdDate(String dateString)
  9. parseYMDToDate(String dateString)