Java中的日期Date

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

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)));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值