首页 > 其他分享 >项目测试中自定义时间

项目测试中自定义时间

时间:2023-01-05 19:56:58浏览次数:46  
标签:return String 自定义 项目 int 测试 LocalDateTime useCustomDateTime public

  在项目中测试有时候会加速时间,如活动是七天的周期,测试肯定得加速

这个工具类就是为了这种情况,设定多少分一天就可以达到。useCustomDateTime在上线就改为false:使用LocalDateTime类

package util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;


public class CustomDateTime {
    public String startLocalDateTime; //开始的日期 格式:2023-01-06 00:00:00
    public long startMillis = System.currentTimeMillis(); //开始的时间戳
    public boolean useCustomDateTime; //打开自定义日期 true:自定义; false:使用LocalDateTime类
    public int intervalMinute; //多少分钟算一天(useCustomDate为true生效)
    private String formatter = "yyyy-MM-dd HH:mm:ss";

    public String offsetId;
    public ZoneId zone;

    public String getStartLocalDateTime() {
        return startLocalDateTime;
    }

    public void setStartLocalDateTime(String startLocalDateTime) {
        this.startLocalDateTime = startLocalDateTime;
    }

    public long getStartMillis() {
        return startMillis;
    }

    public void setStartMillis(long startMillis) {
        this.startMillis = startMillis;
    }

    public boolean isUseCustomDateTime() {
        return useCustomDateTime;
    }

    public void setUseCustomDateTime(boolean useCustomDateTime) {
        this.useCustomDateTime = useCustomDateTime;
    }

    public String getFormatter() {
        return formatter;
    }

    public void setFormatter(String formatter) {
        this.formatter = formatter;
    }

    public String getOffsetId() {
        return offsetId;
    }

    public void setOffsetId(String offsetId) {
        this.offsetId = offsetId;
        this.zone = ZoneOffset.of(offsetId);
    }

    public ZoneId getZone() {
        return zone;
    }

    public void setZone(ZoneId zone) {
        this.zone = zone;
    }

    public int getIntervalMinute() {
        return intervalMinute;
    }

    public void setIntervalMinute(int intervalMinute) {
        this.intervalMinute = intervalMinute;
    }

    /**
     * 获取当前的LocalDateTime
     * @return
     */
    public LocalDateTime getLocalDateTime() {
        if (!useCustomDateTime) {
            return LocalDateTime.now(zone);
        }
        int intervalSecond = 60 * 60 * 24 / (intervalMinute * 60);
        int second = (int)(System.currentTimeMillis() - startMillis) / 1000 * intervalSecond;
        DateTimeFormatter df = DateTimeFormatter.ofPattern(formatter);
        LocalDateTime localDateTime = LocalDateTime.parse(startLocalDateTime, df);
        return localDateTime.plusSeconds(second);
    }

    public LocalDate getLocalDate() {
        if (!useCustomDateTime) {
            return LocalDate.now(zone);
        }
        int intervalSecond = 60 * 60 * 24 / (intervalMinute * 60);
        int second = (int)(System.currentTimeMillis() - startMillis) / 1000 * intervalSecond;
        DateTimeFormatter df = DateTimeFormatter.ofPattern(formatter);
        LocalDateTime localDateTime = LocalDateTime.parse(startLocalDateTime, df);
        return localDateTime.plusSeconds(second).toLocalDate();
    }

    public static void main(String[] args) throws InterruptedException {
        CustomDateTime customDateTime = new CustomDateTime();
        customDateTime.setIntervalMinute(2);
        customDateTime.setStartLocalDateTime("2023-01-06 00:00:00");
        customDateTime.setUseCustomDateTime(true);
        customDateTime.setOffsetId("+8");
        TimeUnit.SECONDS.sleep(1);
        System.out.println(customDateTime.getLocalDateTime());
        System.out.println(customDateTime.getLocalDate());
    }

}

在项目可以属性改成配置文件设定 useCustomDateTime测试的时候就改为true;

useCustomDateTime在上线就改为false

标签:return,String,自定义,项目,int,测试,LocalDateTime,useCustomDateTime,public
From: https://www.cnblogs.com/weiapro/p/17028708.html

相关文章