| package com.dbgs.blct.utils; |
| |
| import java.text.SimpleDateFormat; |
| import java.util.ArrayList; |
| import java.util.Date; |
| import java.util.List; |
| import java.util.Random; |
| import java.util.stream.Collectors; |
| |
| public class RandomDateUtil { |
| public static void main(String[] args) throws Exception { |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| List<String> dates = randomDateString("2022-08-30 00:00:00", "2022-08-30 23:59:59", 1, -1); |
| |
| System.out.println(dates.get(0)); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static List<Date> randomDateDate(String start, String end, int size, int order) throws Exception { |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| Date startTime = sdf.parse(start); |
| Date endTime = sdf.parse(end); |
| Random random = new Random(); |
| List<Date> dates = random.longs(size, startTime.getTime(), endTime.getTime()).mapToObj(t -> new Date(t)).collect(Collectors.toList()); |
| dates.sort((t1, t2) -> { |
| return t1.compareTo(t2) * order; |
| }); |
| return dates; |
| } |
| |
| public static List<String> randomDateString(String start, String end, int size, int order) throws Exception { |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| Date startTime = sdf.parse(start); |
| Date endTime = sdf.parse(end); |
| Random random = new Random(); |
| List<Date> dates = random.longs(size, startTime.getTime(), endTime.getTime()).mapToObj(t -> new Date(t)).collect(Collectors.toList()); |
| dates.sort((t1, t2) -> { |
| return t1.compareTo(t2) * order; |
| }); |
| ArrayList<String> strings = new ArrayList<>(); |
| List<String> collect = dates.stream().map(obj -> sdf.format(obj)).collect(Collectors.toList()); |
| return collect; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |