How to get yesterday and tomorrow date in java with example


This tutorial is about multiple ways to find yesterday date and tomorrows date in java.

How to get Yesterday Date in java

There are multiple ways we can retrieve 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 previous date in java8 with LocalDate

Following are steps

  • Get Current Date using Localdate.now() method, LocalDate is an Date class stores Date information without time and zone information.
  • It has minusDays method which 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 previous date and time, You can use [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 which provides converting specific date and time values.

This example prints an yesterdays date and time

  • It prints Date object from calendar using 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 how to set yesterday date into calendar object

you can get 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 next Date in java

Following ways are to get 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 date, We can pass 1 as parameter.

This example returns tomorrows 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 an 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 which works before 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 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.