Java Parse Date parseDateTimeFromString(String datetime)

Here you can find the source of parseDateTimeFromString(String datetime)

Description

parse Date Time From String

License

Apache License

Declaration

public static Date parseDateTimeFromString(String datetime) 

Method Source Code

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

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

public class Main {
    private static final ThreadLocal<SimpleDateFormat> RFC_822_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
        @Override/*w w w  .j a va2 s.c  o  m*/
        protected SimpleDateFormat initialValue() {
            SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            return format;
        }
    };
    private static final ThreadLocal<SimpleDateFormat> RFC_850_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            SimpleDateFormat format = new SimpleDateFormat("EEEE, dd-MMM-yy HH:mm:ss z", Locale.US);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            return format;
        }
    };
    private static final ThreadLocal<SimpleDateFormat> ANSI_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy", Locale.US);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            return format;
        }
    };

    public static Date parseDateTimeFromString(String datetime) {
        Date date = tryToParse(datetime, RFC_822_DATE_FORMAT.get());
        if (date == null) {
            date = tryToParse(datetime, RFC_850_DATE_FORMAT.get());
        }
        if (date == null) {
            date = tryToParse(datetime, ANSI_DATE_FORMAT.get());
        }
        return date;
    }

    private static Date tryToParse(String datetime, SimpleDateFormat format) {
        try {
            return format.parse(datetime);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. parseDateTime(String s)
  2. parseDateTime(String s)
  3. parseDateTime(String s)
  4. parseDateTime(String s)
  5. parseDatetime(String value)
  6. parseDateTimeHuman(String dateString)
  7. parseDateTimeString(final String date)
  8. parseDateTimeString(String dateTime)
  9. parseDateTimeString(String dateTimeString)