Java Parse Date Pattern YYYY parse(String string)

Here you can find the source of parse(String string)

Description

parse

License

Open Source License

Declaration

public static long parse(String string) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

public class Main {
    public static final String ISO_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss", ISO_DATE_HOUR_FORMAT = "yyyy-MM-dd HH",
            ISO_DATE_FORMAT = "yyyy-MM-dd", ISO_HOUR_FORMAT = "HH";
    private static ThreadLocal<Map<String, DateFormat>> dateFormats = new ThreadLocal<Map<String, DateFormat>>() {
        public Map<String, DateFormat> get() {
            return super.get();
        }/*from  w  w  w.  j av  a  2 s.c o m*/

        protected Map<String, DateFormat> initialValue() {
            return new HashMap<String, DateFormat>();
        }

        public void remove() {
            super.remove();
        }

        public void set(Map<String, DateFormat> value) {
            super.set(value);
        }
    };
    public final static long NO_TIME_AVAILABLE = -1L;

    public static long parse(String string, String format) {
        if (null == string) {
            return NO_TIME_AVAILABLE;
        }
        try {
            return getDateFormat(format).parse(string).getTime();
        } catch (ParseException e) {
            return NO_TIME_AVAILABLE;
        }
    }

    public static long parse(String string) {
        return parse(string, ISO_DATETIME_FORMAT);
    }

    private static DateFormat getDateFormat(String format) {
        Map<String, DateFormat> threadDateFormats = dateFormats.get();
        if (!threadDateFormats.containsKey(format)) {
            DateFormat dateFormat = new SimpleDateFormat(format);
            dateFormat.setLenient(false);
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            threadDateFormats.put(format, dateFormat);
        }
        return threadDateFormats.get(format);
    }
}

Related

  1. parse(String str)
  2. parse(String str)
  3. parse(String str)
  4. parse(String strDate)
  5. parse(String strDate)
  6. parse(String string)
  7. parse(String stringData)
  8. parse(String text)
  9. parse(String text)