parent
e812995138
commit
0b0285b6ee
@ -0,0 +1,16 @@
|
||||
package enums;
|
||||
|
||||
|
||||
public enum DateTimePatternEnum {
|
||||
YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss"), YYYY_MM_DD("yyyy-MM-dd"), YYYYMM("YYYYMM"),YYYY_DOT_MM_DOT_DD("yyyy.MM.dd");
|
||||
|
||||
private String pattern;
|
||||
|
||||
DateTimePatternEnum(String pattern) {
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
public String getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package util;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import enums.DateTimePatternEnum;
|
||||
|
||||
public class DateUtil {
|
||||
|
||||
private static final Object lockObj = new Object();
|
||||
private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
|
||||
|
||||
private static SimpleDateFormat getSdf(final String pattern) {
|
||||
ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern);
|
||||
if (tl == null) {
|
||||
synchronized (lockObj) {
|
||||
tl = sdfMap.get(pattern);
|
||||
if (tl == null) {
|
||||
tl = new ThreadLocal<SimpleDateFormat>() {
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue() {
|
||||
return new SimpleDateFormat(pattern);
|
||||
}
|
||||
};
|
||||
sdfMap.put(pattern, tl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tl.get();
|
||||
}
|
||||
|
||||
public static String format(Date date, String pattern) {
|
||||
return getSdf(pattern).format(date);
|
||||
}
|
||||
|
||||
public static Date parse(String dateStr, String pattern) {
|
||||
try {
|
||||
return getSdf(pattern).parse(dateStr);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new Date();
|
||||
}
|
||||
|
||||
public static LocalDate fromLocateDate2String(Date date) {
|
||||
Instant instant = date.toInstant();
|
||||
ZoneId zone = ZoneId.systemDefault();
|
||||
LocalDate localDate = instant.atZone(zone).toLocalDate();
|
||||
return localDate;
|
||||
}
|
||||
|
||||
public static Date getDayAgo(Integer day) {
|
||||
LocalDateTime localDateTime = LocalDateTime.now().minusDays(day);
|
||||
ZoneId zone = ZoneId.systemDefault();
|
||||
Instant instant = localDateTime.atZone(zone).toInstant();
|
||||
return Date.from(instant);
|
||||
}
|
||||
|
||||
public static List<String> getBetweenDate(Date startDate, Date endDate) {
|
||||
LocalDate startLocalDate = fromLocateDate2String(startDate);
|
||||
LocalDate endLocalDate = fromLocateDate2String(endDate);
|
||||
long numOfDays = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
|
||||
List<LocalDate> localDateList = Stream.iterate(startLocalDate, date -> date.plusDays(1)).limit(numOfDays).collect(Collectors.toList());
|
||||
|
||||
List<String> dateList = localDateList.stream().map(date -> date.format(DateTimeFormatter.ofPattern(DateTimePatternEnum.YYYY_MM_DD.getPattern()))).collect(Collectors.toList());
|
||||
return dateList;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.luoo.user.controller;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import enums.DateTimePatternEnum;
|
||||
import util.DateUtil;
|
||||
|
||||
public class MyControllerTest {
|
||||
|
||||
@Test
|
||||
public void testBirthDay() {
|
||||
String birthDay="2020.02.02";
|
||||
Date date=DateUtil.parse(birthDay, DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern());
|
||||
assertTrue(null!=date);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.luoo.user.util;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
import com.apifan.common.random.RandomSource;
|
||||
|
||||
public class NickNameUtilTest {
|
||||
int size = 4;
|
||||
|
||||
String[] content = new String[] { "~", "`", "@", "$", "%", "^", "&", "*", "+", "-", "|", ".", "<", ">" };
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
int total = 100000;
|
||||
System.out.println("total: " + total);
|
||||
StopWatch sw = new StopWatch();
|
||||
sw.start();
|
||||
IntStream.range(0, total).parallel().mapToObj(j -> getRandomNickName()).limit(100).forEach(System.out::println);
|
||||
for (int i = 4; i < 5; i++) {
|
||||
size = i;
|
||||
long distinctNickName = IntStream.range(0, total).parallel().mapToObj(j -> getRandomNickName()).distinct()
|
||||
.count();
|
||||
System.out.println("length: " + i + " distinctCount: " + distinctNickName);
|
||||
}
|
||||
sw.stop();
|
||||
System.out.println(sw.prettyPrint());
|
||||
}
|
||||
|
||||
public String getRandomNickName() {
|
||||
int index = RandomSource.numberSource().randomInt(0, content.length);
|
||||
String character = content[index];
|
||||
return "雀乐-" + RandomSource.personInfoSource().randomChineseNickName(4) + character
|
||||
+ RandomSource.personInfoSource().randomChineseNickName(4);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue