其他分享
首页 > 其他分享> > Android工具类篇 日期工具类【DateUtil】

Android工具类篇 日期工具类【DateUtil】

作者:互联网

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: penglaoxi
 * @CreateDate: 2021/04/30 12:52
 */
@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()));
    }
}


标签:DateUtil,return,String,public,date,static,Date,工具,Android
来源: https://blog.csdn.net/weixin_44720673/article/details/116301240