How to get yesterday and tomorrow date in java with example
- Admin
- Feb 4, 2024
- Java-examples
This tutorial explores multiple ways to find yesterday’s date and tomorrow’s date in Java.
- FInd yesterday Date using
LocalDate minusDays(1)
method andCalendar
instance - Get Tomorrow’s Date using
LocalDate plusDays(1)
method andCalendar
instance
Find Yesterday’s Date
There are multiple ways we can retrieve the last 1-day date or previous day date in java.
Using Java 8 LocalDate
Java 8 introduced the LocalDate examples class in the
java.time
package, making it easy to manipulate dates.- Get the current date using the
LocalDate.now()
method.LocalDate
stores date information without the time and zone. - Use the
minusDays
method to obtain the previous day.
import java.time.LocalDate; public class Test { public static void main(String[] args) throws Exception{ LocalDate currentDate = LocalDate.now(); System.out.println(currentDate); // 2021-09-01 LocalDate previousDay = currentDate.minusDays(1); System.out.println(previousDay); //2021-08-31 } }
If you need the previous date and time, you can use the LocalDateTime class in place of
LocalDate
.import java.time.LocalDateTime; public class Test { public static void main(String[] args) throws Exception{ LocalDateTime currentDate = LocalDateTime.now(); System.out.println(currentDate); // 2021-09-01T10:08:55.568 LocalDateTime previousDay = currentDate.minusDays(1); System.out.println(previousDay); //2021-08-31T10:08:55.568 } }
- Get the current date using the
Using Calendar Object
The
Calendar
abstract class, available in the java.util package, allows you to manipulate specific date and time values.Here’s an example of printing yesterday’s date and time:
- It prints the Date object from the calendar using the getTime method.
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; public class Test { public static void main(String[] args) throws Exception{ DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd"); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); System.out.println(calendar.getTime()); //Tue Aug 31 09:56:28 IST 2021 dateFormat.format(calendar.getTime()); // Change format as per "yyyy-MMM-dd" } }
let’s see an example of how to set yesterday’s date into a calendar object
you can get the previous day using Calendar.HOUR= -24 hours or Calendar.DAY_OF_MONTH =-1 as given example
import java.util.Calendar; public class Test { public static void main(String[] args) throws Exception{ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.HOUR, -24); System.out.println(calendar.getTime()); //Tue Aug 31 11:08:25 IST 2021 Calendar calendar2 = Calendar.getInstance(); calendar2.add(Calendar.DAY_OF_MONTH, -1); calendar2.getTime(); System.out.println(calendar.getTime()); //Tue Aug 31 11:08:25 IST 2021 } }
Get Tomorrow’s Date
The following ways are to get the next date in java.
To get tomorrow’s date, you can use the LocalDate
plusDays
(1) method with LocalDate
or a Calendar
instance.
Using Java 8 LocalDate
LocalDate.plusDays()
method return the future days. for tomorrow’s date, We can pass 1 as a parameter.Get the current date using LocalDate.now(). Use the plusDays method to obtain the next day.
This example returns tomorrow with Date only not time information.
import java.time.LocalDate; public class Test { public static void main(String[] args) throws Exception{ LocalDate currentDate = LocalDate.now(); System.out.println(currentDate); // 2021-09-01 LocalDate previousDay = currentDate.plusDays(1); System.out.println(previousDay); //2021-09-02 } }
LocalDateTime
to return Tomorrow date and time
import java.time.LocalDateTime; public class Test { public static void main(String[] args) throws Exception{ LocalDateTime currentDate = LocalDateTime.now(); System.out.println(currentDate); // 2021-09-01T11:14:45.223 LocalDateTime previousDay = currentDate.plusDays(1); System.out.println(previousDay); //2021-09-02T11:14:45.223 } }
With Calendar Object Calendar Object is a legacy class that works before the java8 version.
Adding
Calendar.HOUR=+24
orCalendar.DAY_OF_MONTH=+1
to calendar object sets Calendar time to tomorrows date usingadd()
methodHere is an example
import java.util.Calendar; public class Test { public static void main(String[] args) throws Exception{ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.HOUR, +24); System.out.println(calendar.getTime()); //Tue Aug 31 11:08:25 IST 2021 Calendar calendar2 = Calendar.getInstance(); calendar2.add(Calendar.DAY_OF_MONTH, +1); calendar2.getTime(); System.out.println(calendar.getTime()); //Tue Aug 31 11:08:25 IST 2021 } }
Conclusion
In this tutorial, you’ve learned various approaches to get previous and next dates in Java, including:
- Using Java 8 LocalDate and LocalDateTime classes.
- Utilizing the legacy Calendar class for date manipulation. For date and time manipulation, Java 8 classes are recommended due to their robustness.