69 lines
2.2 KiB
Java
69 lines
2.2 KiB
Java
package com.jero.modules.utils;
|
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
import com.github.javafaker.Faker;
|
|
|
|
import java.time.LocalDate;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author: Mzaxd
|
|
* @Date: 2024/4/9 9:35
|
|
*/
|
|
public class RandomNumberUtil {
|
|
|
|
public static final Faker faker = new Faker();
|
|
|
|
public static double getRandomDouble() {
|
|
// 生成一个在0到100之间,小数点后有2位的随机浮点数
|
|
return faker.number().randomDouble(2, 0, 100);
|
|
}
|
|
|
|
public static int getRandomInt() {
|
|
return faker.number().randomDigit();
|
|
}
|
|
|
|
public static int getRandomIntBetween(int min, int max) {
|
|
return faker.number().numberBetween(min, max);
|
|
}
|
|
|
|
// 生成指定大小和类型的列表,填充伪随机数据
|
|
public static <T> List<T> generateFakeDataList(int size, Class<T> type) {
|
|
List<T> resultList = new ArrayList<>();
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
if (type.equals(Double.class)) {
|
|
// 对于 Double 类型,生成伪随机双精度值
|
|
resultList.add(type.cast(faker.number().randomDouble(2, 50, 100)));
|
|
} else if (type.equals(Integer.class)) {
|
|
// 对于 Integer 类型,生成伪随机整数值
|
|
resultList.add(type.cast(faker.number().numberBetween(1, 100)));
|
|
} else {
|
|
// 可以扩展更多的类型支持
|
|
throw new IllegalArgumentException("Unsupported type: " + type);
|
|
}
|
|
}
|
|
|
|
return resultList;
|
|
}
|
|
|
|
// 生成包含今天及之前29天日期的列表,格式为"MM-DD"
|
|
public static List<String> getLast30DaysFormattedDatesAscending() {
|
|
List<String> datesList = new ArrayList<>();
|
|
LocalDate startDate = LocalDate.now().minusDays(29); // 从30天前开始
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
|
|
|
|
for (int i = 0; i < 30; i++) {
|
|
LocalDate date = startDate.plusDays(i); // 递增日期
|
|
String formattedDate = date.format(formatter);
|
|
datesList.add(formattedDate);
|
|
}
|
|
|
|
return datesList;
|
|
}
|
|
|
|
|
|
}
|