Java Parse Date parseDate(String value)

Here you can find the source of parseDate(String value)

Description

Loops over all the possible date formats and tries to find the right one.

License

Apache License

Parameter

Parameter Description
value ISO date string

Return

Null if there is a parsing failure

Declaration

public static Date parseDate(String value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Arrays;

import java.util.Date;

import java.util.List;
import java.util.Locale;

import java.util.TimeZone;

public class Main {
    /**//from  www  .  jav  a  2s  .  co m
     * Date formats using for Date parsing.
     */
    @SuppressWarnings("unchecked")
    private static final List<ThreadLocal<SimpleDateFormat>> DATETIME_FORMATS = Arrays
            .asList(new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
                    format.setTimeZone(TimeZone.getTimeZone("UTC"));
                    return format;
                }
            }, new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
                    format.setTimeZone(TimeZone.getTimeZone("UTC"));
                    return format;
                }
            }, new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'", Locale.US);
                    format.setTimeZone(TimeZone.getTimeZone("UTC"));
                    return format;
                }
            }, new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
                    format.setTimeZone(TimeZone.getTimeZone("UTC"));
                    return format;
                }
            }, new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
                    format.setTimeZone(TimeZone.getTimeZone("UTC"));
                    return format;
                }
            }, new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat format = new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US);
                    format.setTimeZone(TimeZone.getTimeZone("UTC"));
                    return format;
                }
            }, new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat format = new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US);
                    format.setTimeZone(TimeZone.getTimeZone("UTC"));
                    return format;
                }
            });

    /**
     * Loops over all the possible date formats and tries to find the right one.
     *
     * @param value ISO date string
     * @return Null if there is a parsing failure
     */
    public static Date parseDate(String value) {
        if (value == null) {
            return null;
        }
        Date date = null;
        for (ThreadLocal<SimpleDateFormat> format : DATETIME_FORMATS) {
            try {
                date = format.get().parse(value);
                break;
            } catch (ParseException e) {
                // We loop through this until we found a valid one.
            }
        }
        return date;
    }
}

Related

  1. parseDate(String time)
  2. parseDate(String time)
  3. parseDate(String timeString)
  4. parseDate(String token)
  5. parseDate(String value)
  6. parseDate(String value)
  7. parseDate(String value)
  8. parseDate(String value)
  9. parseDate(String value)