Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final String date_format_simple = "yyyy-MM-dd";
    private static final ThreadLocal<DateFormat> simpleDateFormat = new ThreadLocal<DateFormat>() {
        @Override
        public DateFormat get() {
            return super.get();
        }

        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat(date_format_simple);
        }

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

        @Override
        public void set(DateFormat value) {
            super.set(value);
        }
    };

    /**
     * @param dateStr
     *            "yyyy-MM-dd" like
     * @return parsed Date
     * @throws ParseException
     */
    public static Date parseSimple(String dateStr) {
        DateFormat dateFormat = getSimpleDateFormat();
        Date date = null;
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    /**
     * @return "yyyy-MM-dd" like formatter
     */
    public static DateFormat getSimpleDateFormat() {
        DateFormat df = simpleDateFormat.get();
        if (df == null) {
            df = new SimpleDateFormat(date_format_simple);
            simpleDateFormat.set(df);
        }
        return df;
    }
}