Java Parse Date Pattern YYYY parse(String str)

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

Description

parse

License

Apache License

Declaration

public static long parse(String str) 

Method Source Code


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

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

import java.util.Map;
import java.util.TimeZone;

import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static final Map<String, Long> map = new ConcurrentHashMap<String, Long>();

    public static long parse(String str) {
        int len = str.length();
        String date = str.substring(0, 10);
        Long baseline = map.get(date);

        if (baseline == null) {
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

                format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                baseline = format.parse(date).getTime();
                map.put(date, baseline);
            } catch (ParseException e) {
                return -1;
            }/*ww  w.  j av a  2  s . c o m*/
        }

        long time = baseline.longValue();
        long metric = 1;
        boolean millisecond = true;

        for (int i = len - 1; i > 10; i--) {
            char ch = str.charAt(i);

            if (ch >= '0' && ch <= '9') {
                time += (ch - '0') * metric;
                metric *= 10;
            } else if (millisecond) {
                millisecond = false;
            } else {
                metric = metric / 100 * 60;
            }
        }
        return time;
    }
}

Related

  1. parse(String source)
  2. parse(String source)
  3. parse(String source, Date defaultValue)
  4. parse(String str)
  5. parse(String str)
  6. parse(String str)
  7. parse(String str)
  8. parse(String strDate)
  9. parse(String strDate)