Find the time now minus the time in 30 days.

  java, question

I want to get the latest 30 days, endDate is the current time, startDate is the current time minus 30 days, how to find startDate? ? ? ? ? ? Thank you
String endDate = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(new Date());
String startDate = “”;

If java8 cannot be used, it is recommended to use the Calendar class. The code is as follows.AddThe second parameter of the method passes a negative value.

Calendar now = Calendar.getInstance();
 now.add(Calendar.DAY_OF_MONTH, -30);
 String endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now.getTime());
 System.out.println(endDate);

Java8 version 8 can use the following methods:

LocalDateTime now = LocalDateTime.now();
 now = now.minus(30, ChronoUnit.DAYS);
 System.out.println(now.toString());