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());

Java8 version 8 can use the following methods:

LocalDateTime now =;
 now = now.minus(30, ChronoUnit.DAYS);