{

How to get yesterday and tomorrow date in java with example


This tutorial is about multiple ways to find yesterday’s date and tomorrow’s date in java.

  • FInd yesterday Date using LocalDate minusDays(1) method and Calendar instance
  • Get Tomorrow’s Date using LocalDate plusDays(1)method and Calendar instance

How to get Yesterday’s Date in java

There are multiple ways we can retrieve the last 1-day date or previous day date in java.

  • Java8 LocalDate

Java8 introduced Date classes to java.time package. You can check previous post on LocalDate examples

It is easy to get the previous date in java8 with LocalDate

Following are steps

  • Get Current Date using the Localdate.now() method, LocalDate is a Date class that stores Date information without the time and zone information.
  • It has the minusDays method that returns previous days.
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 want the previous date and time, You can use the [LocalDateTime](java8-learn-localdatetime-basics-with.html) 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

    }
}
  • Calendar Object in java

Calendar is an abstract class in java.lang package that provides converting specific date and time values.

This example prints a 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

    }
}

How to get Tomorrows or the next Date in java

The following ways are to get the next date in java.

In Java8, We can use LocalDate and LocalDateTime classes for this.

  • LocalDate

LocalDate.plusDays() method return the future days. for tomorrow’s date, We can pass 1 as a parameter.

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 or Calendar.DAY_OF_MONTH=+1 to calendar object sets Calendar time to tomorrows date using add() method

Here 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

You learned to get previous and next dates in java using the below approaches.

  • Java8 LocalDate and LocalDateTime classes
  • Legacy Calendar Class in java

I prefer java8 classes as these are robust in handling date and time manipulation.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

BigInteger divide method in Java with examples| BigDecimal divide tutorials

BigInteger multiply method in Java with examples

How to Check whether Bigdecimal value is zero or not in Java

How to Convert BigDecimal to Double or Double to BigDecimal in java with examples

How to Convert BigDecimal to Float or Float to BigDecimal in java with examples

How to Rounding Bigdecimal values with 2 Decimal Places in Java

Multiple ways to fix java.util.NoSuchElementException: No value present error in java