日期工具类

package com.funi.demoprojact.utils;

import com.google.common.base.Strings;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/12/2 0002.
 */
public abstract class DateUtils {
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATE_FORMAT_All = "yyyyMMddHHmmss";
    private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>();

    /**
     * 年月日
     * @return
     */
    public static DateFormat getDateFormat() {
        DateFormat df = threadLocal.get();
        if(df==null){
            df = new SimpleDateFormat(DATE_FORMAT);
            threadLocal.set(df);
        }
        return df;
    }

    /**
     * date转string
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        if(null==date){
            return "";
        }
        return getDateFormat().format(date);
    }

    /**
     * string转date
     * @param strDate
     * @return
     */
    public static Date parse(String strDate) {
        if(Strings.isNullOrEmpty(strDate)){
            return null;
        }
        try{
            return getDateFormat().parse(strDate);
        }catch (ParseException e){
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 年月日时分秒
     * @return
     */
    public static DateFormat getDateFormatAll() {
        DateFormat df = threadLocal.get();
        if(df==null){
            df = new SimpleDateFormat(DATE_FORMAT_All);
            threadLocal.set(df);
        }
        return df;
    }

    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //不同年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1+1) ;
        }
        else    //同一年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1+1));
            return day2-day1+1;
        }
    }

    public static String formatDateAll(Date date) {
        if(null==date){
            return "";
        }
        return getDateFormatAll().format(date);
    }
    public static Date parseAll(String strDate) {
        if(Strings.isNullOrEmpty(strDate)){
            return null;
        }
        try{
            return getDateFormatAll().parse(strDate);
        }catch (ParseException e){
            e.printStackTrace();
            return null;
        }

    }

    public static boolean dateBetweenSE(Date start,Date current,Date end){
        if(null==start || null==current || null==end){
            return false;
        }
        if(start.compareTo(current)<=0 && end.compareTo(current)>=0){
            return true;
        }
        return false;
    }

    public static void main(String[] args)throws Exception{
        /*Date nowDate = new Date();
        String startDate = "2017-07-31 00:00:00";
        String endDate = "2017-08-06 12:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = sdf.parse(startDate);
        Date end = sdf.parse(endDate);
        boolean isBetween = dateBetweenSE(start,nowDate,end);
        System.err.println(isBetween);*/


        String lotteryTime = "2017-07-31";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (!StringUtils.isEmpty(lotteryTime)) {
            try {
                long now = new Date().getTime();
                long date = sdf.parse(lotteryTime).getTime()+ 24*60*60*1000;
                if (date < now) {
                    throw new Exception("已过摇号日期,不允许再进行核验、审核、注销操作!");
                }
            } catch (ParseException e) {
                e.printStackTrace();

            }
        }
    }
    public static String getStringDate() {
         Date currentTime = new Date();
         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String dateString = formatter.format(currentTime);
         return dateString;
        }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值