为什么建议使用你LocalDateTime,而不是Date?
生成一个订单号以年月日开头
1.当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
2.当前日期加一年减一天,在转成日期格式
String timeStr1 = LocalDateTime.now().plusYears(1l).minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Date parse = sdf.parse(timeStr1);
LocalDate now = LocalDate.now();//当前日期
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateNow = now.format(fmt);//LocalDate 转 String
// LocalDate localDate = now.minusMonths(1);//获取前一月
// String dateMonths = localDate.format(fmt);
// LocalDate localDate = now.minusDays(15);//前十五天
LocalDate localDate = now.plusDays(15);//后十五天
String fifteen = localDate.format(fmt);
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
String str= sdf.format(new Date());
System.err.println(str);
20210127163557
/**
* 获取订单号
* @return
*/
public static String getOrderNo() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String newDate = sdf.format(new Date());
String result = "";
Random random = new Random();
for (int i = 0; i < 3; i++) {
result += random.nextInt(10);
}
return newDate + result;
}