欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于jdk1.8时间操作

程序员文章站 2024-02-29 19:18:58
...

最近刚入职一家公司,这两天没啥事做,坐我旁边的同事让帮忙写一个方法,闲着也是闲着,于是很爽快的答应了。需求如下:

四个参数(startDate endDate number period)   startDate根据period推算直到<= endDate 记录符合的日期

比如 startDate(2020-07-15) number(2) period(天) endDate(2020-07-21) :从今天开始 每2天一个周期 直到<=2020-07-21,结果为三条记录:2020-07-17,2020-07-19,2020-07-21

代码如下:

1.先定义一个obj

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Date;



/**
 * @author leadhow
 * @date 2020/7/15
 **/
@Data
@AllArgsConstructor
public class DateObj {

    /**
     * 开始日期
     */
    private Date startDate;

    /**
     * 截至日期
     */
    private Date endDate;

    /**
     * 周期性间隔数
     */
    private String num;

    /**
     * 周期 (天、周、月)
     */
    private String period;
}

2.定义test类方法并测试

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author leadhow
 * @date 2020/7/15
 **/
public class Test1 {
    public static void main(String[] args) {
        //把时间设置为2020-07-21
        Calendar calendar  =   Calendar.getInstance();
        calendar.set(2020, Calendar.JULY, 21);
        Date date=calendar.getTime();
        
        List<Date> dates = getValidDate(new DateObj(new Date(), date, "2", "天"));
        dates.forEach( validDate -> System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(validDate)));
    }

    public static List<Date> getValidDate(DateObj obj) {
        LocalDate startDate = dateToLocalDate(obj.getStartDate());
        LocalDate endDate = dateToLocalDate(obj.getEndDate());
        if(startDate.equals(endDate) || startDate.isAfter(endDate)) {
            return new ArrayList<>();
        }
        long number = Long.valueOf(obj.getNum());
        String period = obj.getPeriod();
        return validDate(startDate,endDate,number,period);
    }

    /**
     *
     * @param startDate
     * @param endDate
     * @param number
     * @param period
     * @return 符合条件的日期
     */
    private static List<Date> validDate(LocalDate startDate, LocalDate endDate, long number, String period) {
        List<Date> dates = new ArrayList<>();
        while (startDate.isBefore(endDate)) {
            switch (period) {
                case "天" :
                    startDate = startDate.plus(number, ChronoUnit.DAYS);
                    break;
                case "周" :
                    startDate = startDate.plus(number, ChronoUnit.WEEKS);
                    break;
                case "月" :
                    startDate = startDate.plus(number,ChronoUnit.MONTHS);
                    break;
                default:
                    break;
            }
            if(startDate.isBefore(endDate) || startDate.equals(endDate)) {
                dates.add(localDateToDate(startDate));
            }
        }
        return dates;
    }

    /**
     * Date转localDate
     */
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
        return zdt.toLocalDate();
    }

    /**
     * localDate转Date
     */
    public static Date localDateToDate(LocalDate localDate) {
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        Instant instant1 = zonedDateTime.toInstant();
        return Date.from(instant1);
    }
}
结果:
2020-07-17
2020-07-19
2020-07-21

ps:好久没更了,其实没啥技术含量,刷个存在感 peace yo~