Java gets the current time and the previous day of implementation code

  • 2020-04-01 02:23:47
  • OfStack

Without further ado, go to code


String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Date dNow = new Date();   //The current time
Date dBefore = new Date();
Calendar calendar = Calendar.getInstance(); //Get a calendar
calendar.setTime(dNow);// the The current time Assigned to the calendar 
calendar.add(Calendar.DAY_OF_MONTH, -1);  //Set it to the day before
dBefore = calendar.getTime();   //Get the day before

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //Set the time format
String defaultStartDate = sdf.format(dBefore);    //Format the day before
String defaultEndDate = sdf.format(dNow); // formatting The current time

System.out.println(" The day before was: " + defaultStartDate);
System.out.println(" The generation time is: " + defaultEndDate);  


Related articles: