日期工具类

package com.hskj.research.controller.openApi.daka.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
/**
 * 		日期工具类
 *     
 * Created by  Caos
 */
public class DateUtil {
	
	/**
	 * @Description:   判断是否为当天日期
	 * @param date
	 * @return boolean 
	 */
	public static boolean isToday(Date date) {
		
		boolean flag = false;
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate compare_date = instant.atZone(zoneId).toLocalDate();
        LocalDate today = LocalDate.now();
        if (today.equals(compare_date)) {
        	flag = true;
        }
		return flag;
	}
	
	/**
	 * @Description:   判断时间 是否小于当天
	 * @param date
	 * @return boolean 
	 */
	public static boolean isLessToday(Date date) {

		boolean flag = false;
		ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = LocalDate.now();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        Date today = Date.from(zdt.toInstant());
        if (date.getTime() < today.getTime()) {
        	flag = true;
        }
		return flag;
	}
	
	public static void main(String[] args) {
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date_time;
		try {
			date_time = format.parse("2018-12-21 10:59:59");
			boolean isToday = isToday(date_time);
			System.out.println(isToday == true ? "是当天":"不是当天");
			boolean lessToday = isLessToday(date_time);
			System.out.println(lessToday ==  true ? "小于当天" :"大于等于当前");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}

}

你可能感兴趣的:(java,java工具类)