Java Date Parse convertStringToDate(String dateTime)

Here you can find the source of convertStringToDate(String dateTime)

Description

Converts a datetime string into and instance of java.util.Date using the date format: yyyy-MM-ddTHH:mm:ss.SSSZ.

License

fedora commons license

Parameter

Parameter Description
dateTime A datetime string

Return

Corresponding instance of java.util.Date (returns null if dateTime string argument is empty string or null)

Declaration

public static Date convertStringToDate(String dateTime) 

Method Source Code

//package com.java2s;
/* The contents of this file are subject to the license and copyright terms
 * detailed in the license directory at the root of the source tree (also
 * available online at http://fedora-commons.org/license/).
 *///w  w  w  . ja  v a 2  s .  co  m

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

public class Main {
    /**
     * Converts a datetime string into and instance of java.util.Date using the
     * date format: yyyy-MM-ddTHH:mm:ss.SSSZ.
     *
     * @param dateTime
     *        A datetime string
     * @return Corresponding instance of java.util.Date (returns null if
     *         dateTime string argument is empty string or null)
     */
    public static Date convertStringToDate(String dateTime) {
        return parseDateAsUTC(dateTime);
    }

    /**
     * Attempt to parse the given string of form: yyyy-MM-dd[THH:mm:ss[.SSS][Z]]
     * as a Date. If the string is not of that form, return null.
     *
     * @param dateString
     *        the date string to parse
     * @return Date the date, if parse was successful; null otherwise
     */
    public static Date parseDateAsUTC(String dateString) {
        if (dateString == null || dateString.length() == 0) {
            return null;
        }

        SimpleDateFormat formatter = new SimpleDateFormat();
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        int length = dateString.length();
        if (dateString.startsWith("-")) {
            length--;
        }
        if (dateString.endsWith("Z")) {
            if (length == 11) {
                formatter.applyPattern("yyyy-MM-dd'Z'");
            } else if (length == 20) {
                formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
            } else if (length == 22) {
                formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
            } else if (length == 23) {
                formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
            } else if (length == 24) {
                formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            }
        } else {
            if (length == 10) {
                formatter.applyPattern("yyyy-MM-dd");
            } else if (length == 19) {
                formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss");
            } else if (length == 21) {
                formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.S");
            } else if (length == 22) {
                formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SS");
            } else if (length == 23) {
                formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
            } else if (dateString.endsWith("GMT") || dateString.endsWith("UTC")) {
                formatter.applyPattern("EEE, dd MMMM yyyyy HH:mm:ss z");
            }
        }
        try {
            return formatter.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. convertStringToDate(String dateString, String dateFormat)
  2. convertStringToDate(String dateString, String dateFormat)
  3. convertStringToDate(String dateString, String pattern)
  4. convertStringToDate(String dateText)
  5. convertStringToDate(String dateTime)
  6. convertStringToDate(String dt)
  7. convertStringToDate(String input)
  8. convertStringToDate(String s)
  9. convertStringToDate(String str)