Java中的日期Date

来源:互联网 发布:linux查询端口命令 编辑:程序博客网 时间:2024/06/06 10:38

在项目开发中,日期处理是一种很常见的操作,在这总结了一下,很简单,留着以后备用。

package com.lsh.utils;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtil {    //SimpleDateFormate 是线程不安全的    public static ThreadLocal<DateFormat> yyyyMMdd = new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue() {            return new SimpleDateFormat("yyyyMMdd");        }    };    public static ThreadLocal<DateFormat> yyyyMMddLine = new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue() {            return new SimpleDateFormat("yyyy/MM/dd");        }    };    public static ThreadLocal<DateFormat> YYMMDD = new ThreadLocal<DateFormat>(){        protected  synchronized DateFormat initialValue() {            return new SimpleDateFormat("yyyyMMdd");        }    };    public static ThreadLocal<DateFormat> HHmmss =new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue(){            return new SimpleDateFormat("HHmmss");        }    };    public static ThreadLocal<DateFormat> yyyyMMdd10 = new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue() {            return new SimpleDateFormat("yyyy-MM-dd");        }    };    public static ThreadLocal<DateFormat> yyyyMMddHHmmss = new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue() {            return new SimpleDateFormat("yyyyMMddHHmmss");        }    };    public static ThreadLocal<DateFormat> formateter17 = new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue() {            return new SimpleDateFormat("yyyyMMddHHmmssSSS");        }    };    public static ThreadLocal<DateFormat> formateter18 = new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue() {            return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");        }    };    public static ThreadLocal<DateFormat> formateter19 = new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue() {            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        }    };    public static ThreadLocal<DateFormat> hhmmssSSS = new ThreadLocal<DateFormat>(){        protected synchronized DateFormat initialValue() {            return new SimpleDateFormat("hhmmssSSS");        }    };    public static SimpleDateFormat getDateFormat6(){        DateFormat df = HHmmss.get();        if(df == null){            df = new SimpleDateFormat("HHmmss");            HHmmss.set(df);        }        return (SimpleDateFormat)df;    }    public static SimpleDateFormat getDateFormat8(){        DateFormat df = yyyyMMdd.get();        if(df == null){            df = new SimpleDateFormat("yyyyMMdd");            yyyyMMdd.set(df);        }        return (SimpleDateFormat)df;    }    public static SimpleDateFormat getDateFormat10(){        DateFormat df = yyyyMMdd10.get();        if(df == null){            df = new SimpleDateFormat("yyyy-MM-dd");            yyyyMMdd10.set(df);        }        return (SimpleDateFormat)df;    }    public static SimpleDateFormat getDateFormat14(){        DateFormat df = yyyyMMddHHmmss.get();        if(df == null){            df = new SimpleDateFormat("yyyyMMddHHmmss");            yyyyMMddHHmmss.set(df);        }        return (SimpleDateFormat)df;    }    public static SimpleDateFormat getDateFormat17(){        DateFormat df = formateter17.get();        if(df == null){            df = new SimpleDateFormat("yyyyMMddHHmmssSSS");            formateter17.set(df);        }        return (SimpleDateFormat)df;    }    public static SimpleDateFormat getDateFormat18(){        DateFormat df = formateter18.get();        if(df == null){            df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");            formateter18.set(df);        }        return (SimpleDateFormat)df;    }    public static SimpleDateFormat getDateFormat19(){        DateFormat df = formateter19.get();        if(df == null){            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSS");            formateter19.set(df);        }        return (SimpleDateFormat)df;    }    /**     * 获取制定对象的format对象     * @param args     */    public static SimpleDateFormat getsimSimpleDateFormatByPattern(final String pattern){        ThreadLocal<DateFormat> formatter = new ThreadLocal<DateFormat>(){            @Override            protected synchronized DateFormat initialValue() {                return new SimpleDateFormat();            }        };        return (SimpleDateFormat)formatter.get();    }    /**     * 获取day天后的日期     * @param args     */    public static Date getDateAfter(Date date,int day){        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)+day);        return calendar.getTime();    }    public static void main(String[] args) {        System.out.println(DateUtil.getDateFormat6().format(new Date()));        System.out.println(DateUtil.getDateFormat8().format(new Date()));        System.out.println(DateUtil.getDateFormat10().format(new Date()));        System.out.println(DateUtil.getDateFormat14().format(new Date()));        System.out.println(DateUtil.getDateFormat17().format(new Date()));        System.out.println(DateUtil.getDateFormat18().format(new Date()));        System.out.println(DateUtil.getDateFormat19().format(new Date()));        System.out.println(DateUtil.getDateFormat18().format(getDateAfter(new Date(), 100)));    }}