Offer

Java-工具类存储

本博客用于存储平时写的工具类所用代码,方便后续查询学习

1. DateUtil.java
2. 未完待续





DateUtil.java:日期工具类

由于之前需求开发涉及到一些Date的计算转换,之前是用Calendar,SimpleDateFormat之类的,比较麻烦,于是打算转用LocalDateTime,方便计算

​ 之前博客链接:https://www.cnblogs.com/meditation5201314/p/13471096.html

​ LocalDate(年月日),LocalTime(时分秒),LocalDateTime(前面两者结合)然后用工具类存储方法,方便以后使用。

package huyuqiao.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * @ProjectName: DateUtils
 * @Package: Date
 * @Description:
 * @Author: huyuqiao
 * @CreateDate: 2020/11/5 14:18
 */
@Component
public class DateUtils {
    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);


    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
    /************************************Calendar---工具类*****************************************/
    /**
     * 日期减去一天
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String dateSubOneDate(String date)  {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.stringToDate(date, DATE_FORMAT_YYYY_MM_DD));
        calendar.add(Calendar.DATE, -1);//当前时间减去一天,即一天前的时间
        return dateToString(calendar.getTime(), DATE_FORMAT_YYYY_MM_DD);
    }
    /**
     * String 转 Date
     *
     * @param date
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Date 转 String
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }


    /************************************LocalDateTime---工具类*****************************************/
    //当前日期的下个星期
    public static LocalDateTime getNextWeekDay(Date date){
        LocalDateTime time = dateToLocalDateTime(date);
        return time.plus(1, ChronoUnit.WEEKS);
    }

    //Date转LocalDateTime
    public static LocalDateTime dateToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    //LocalDateTime转Date
    public static Date localDateTimeToDate(LocalDateTime localDateTime){
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    //比较两个Date时间先后
    public static Boolean isOneAfterTwo(Date one, Date two){
        return dateToLocalDateTime(one).isAfter(dateToLocalDateTime(two));
    }

    //检查闰年
    public static Boolean isLeadYear(LocalDateTime localDateTime){
        return localDateTime.toLocalDate().isLeapYear();
    }

    public static void main(String[] args) throws InterruptedException {
        Date one = new Date();
//        Thread.sleep(3000); //暂停3秒
        Date two = new Date();
        System.out.println(one + "  " + two);
        System.out.println(isOneAfterTwo(one, two));
        System.out.println(isLeadYear(LocalDateTime.now()));
    }
}

posted @ 2020-11-05 15:07  Empirefree  阅读(130)  评论(0编辑  收藏  举报