Get the previous date of the current date

Calendar is equivalent to a calendar, which is an abstract class (Abstract Class) in Java, and GregorianCalendar is a concrete implementation of it.
public String getBeforeDate(){
		Date date = new Date();
		//declare date formatting style
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// Convert the formatted date string to Date
			date = dateFormat.parse(dateFormat.format(date));
		} catch (Exception e) {
			e.printStackTrace ();
		}
		//Get Calendar instance through Calendar's implementation class
		Calendar calendar = GregorianCalendar.getInstance();
		//set the formatted date
		calendar.setTime(date);
		//get current day
		int day = calendar.get(Calendar.DATE);
		//Get the date of the previous day
		calendar.set(Calendar.DATE, day-1);
		//Obtain
		String dateStr = dateFormat.format(calendar.getTime());
		return dateStr;
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326744525&siteId=291194637