I want to get the previous day (24 hours) from the current time.
e.g if current time is Date currentTime = new Date();
2011-04-25 12:15:31:562 GMT
How to determine time i.e
2011-04-24 12:15:31:562 GMT
DateTime yesterday = new DateTime(). minusDays(1);
The minusDays() method of LocalDate class in Java is used to subtract the number of specified day from this LocalDate and return a copy of LocalDate. For example, 2019-01-01 minus one day would result in 2018-12-31. This instance is immutable and unaffected by this method call.
Date today = new Date(); SimpleDateFormat formattedDate = new SimpleDateFormat("yyyyMMdd"); Calendar c = Calendar. getInstance(); c. add(Calendar. DATE, 1); // number of days to add String tomorrow = (String)(formattedDate.
Description. Some java. util. Date constructors like new Date(int year, int month, int day) , new Date(int year, int month, int date, int hrs, int min) and new Date(int year, int month, int date, int hrs, int min, int sec) are deprecated and the Calendar should be used instead.
You can do that using Calendar class:
Calendar cal = Calendar.getInstance();
cal.setTime ( date ); // convert your date to Calendar object
int daysToDecrement = -1;
cal.add(Calendar.DATE, daysToDecrement);
date = cal.getTime(); // again get back your date object
please checkout this here: Java Date vs Calendar
Calendar cal=Calendar.getInstance();
cal.setTime(date); //not sure if date.getTime() is needed here
cal.add(Calendar.DAY_OF_MONTH, -1);
Date newDate = cal.getTime();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With