您当前的位置: 首页 > 

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

产生指定范围内的LocalDate对象

梁云亮 发布时间:2020-01-08 14:46:06 ,浏览量:2

方式一(重点):
public class GenLocalDateUtil {
 /**
     * 生成指定范围内的随机日期时间
     * @param min 起始日期,比如:2000-3-3 8:28:58
     * @param max 终止日期,比如:2004-12-12 12:12:12
     * @return
     */
    public static LocalDateTime genLocalDateTime(String min, String max) {
        if(!(min.contains(" ") || min.contains("-") || min.contains(":"))){
            throw new RuntimeException("日期参数格式不对");
        }
        if(!(max.contains(" ") || max.contains("-") || max.contains(":"))){
            throw new RuntimeException("日期参数格式不对");
        }
        String[] mins = min.split("[ ,\\-,:]");
        String[] maxs = max.split("[ ,\\-,:]");
 
        LocalDateTime localDateTimeMin = LocalDateTime.of(Integer.parseInt(mins[0]), Integer.parseInt(mins[1]),
                Integer.parseInt(mins[2]),Integer.parseInt(mins[3]),Integer.parseInt(mins[4]),Integer.parseInt(mins[5]));
        LocalDateTime localDateTimeMax = LocalDateTime.of(Integer.parseInt(maxs[0]), Integer.parseInt(maxs[1]),
                Integer.parseInt(maxs[2])+1,Integer.parseInt(mins[3]),Integer.parseInt(mins[4]),Integer.parseInt(mins[5]));
 
        //获取日期所对应的数字
        long timeMin = localDateTimeMin.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long timeMax = localDateTimeMax.toInstant(ZoneOffset.of("+8")).toEpochMilli();
 
        double random = Math.random(); // [0,1)
        long digit = (long) (random * (timeMax - timeMin + 1) + timeMin);
 
        LocalDateTime res = LocalDateTime.ofInstant(Instant.ofEpochMilli(digit), ZoneOffset.of("+8"));
 
        return res;
    }
 
    /**
     * 生成指定范围内的随机日期
     * @param min 起始日期,比如:2000-3-3
     * @param max 终止日期,比如:2004-12-12
     * @return
     */
    public static LocalDate genLocalDate(String min, String max) {
        if(!min.contains("-")){
            throw new RuntimeException("日期参数格式不对");
        }
        if(!max.contains("-")){
            throw new RuntimeException("日期参数格式不对");
        }
        min +=" 0:0:0";
        max +=" 23:59:60";
        LocalDateTime res = genLocalDateTime(min,max);
 
        return res.toLocalDate();
    }
}
方式二:
public class GenLocalDateUtil {
	// LocalDate转Date
	public static Date localDate2Date(LocalDate localDate) {
		if (null == localDate) {
			return null;
		}
		ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
		return Date.from(zonedDateTime.toInstant());
	}

	// Date转LocalDate
	public static LocalDate date2LocalDate(Date date) {
		if (null == date) {
			return null;
		}
		return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
	}

	public static LocalDate randomLocalDate(String min, String max) {
		String[] mins = min.split("-");
		String[] maxs = max.split("-");

		LocalDate localDateMin = LocalDate.of(Integer.parseInt(mins[0]), Integer.parseInt(mins[1]),
				Integer.parseInt(mins[2]));
		LocalDate localDateMax = LocalDate.of(Integer.parseInt(maxs[0]), Integer.parseInt(maxs[1]),
				Integer.parseInt(maxs[2]));

		Date dateMin = localDate2Date(localDateMin);
		Date dateMax = localDate2Date(localDateMax);
		
		long timeMin = dateMin.getTime();//获取日期所对应的数字
		long timeMax = dateMax.getTime();

		double random = Math.random(); // [0,1)
		long digit = (long) (random * (timeMax - timeMin + 1) + timeMin);
		Date date = new Date(digit);
		LocalDate res = date2LocalDate(date);
		return res;
	}

	public static void main(String[] args) {
		for (int i = 0; i             
关注
打赏
1665409997
查看更多评论
0.0397s