HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java – Get Next and Previous Date

Java – Get Next and Previous Date

Java examples to get the next day or previous day for any given day. The example uses legacy java.util.Date class as well as java.time.LocalDate class from Java 8.

We can use this example code to calculate tomorrow and yesterday date base on the date of today.

1. java.time.LocalDate

Use LocalDate‘s plusDays() and minusDays() method to get the next day and previous day, by adding and subtracting 1 from today.

private LocalDate findNextDay(LocalDate localdate)
{
	return localdate.plusDays(1);
}

private LocalDate findPrevDay(LocalDate localdate)
{
	return localdate.minusDays(1);
}

2. java.util.Date

Use Date class constructor and pass the time in milliseconds. To get the time for yesterday, get the time for today and subtract total milliseconds in a day.

Similarily, add total milliseconds in a day to get the time for next date.

private static final long MILLIS_IN_A_DAY = 1000 * 60 * 60 * 24;

private static Date findNextDay(Date date)
{
	return new Date(date.getTime() + MILLIS_IN_A_DAY);
}

private static Date findPrevDay(Date date)
{
	return new Date(date.getTime() - MILLIS_IN_A_DAY);
}

3. Complete Example to get next and previous date

import java.time.LocalDate;
import java.util.Date;

public class FindNextPrevDay 
{
	private static final long MILLIS_IN_A_DAY = 1000 * 60 * 60 * 24;
	
	public static void main(String[] args) 
	{
		Date today = new Date();
		
		System.out.println("Today     :: " + findNextDay(today));
		System.out.println("Next date :: " + findNextDay(today));
		System.out.println("Prev date :: " + findPrevDay(today));
		
		LocalDate todayDate = LocalDate.now();
		
		System.out.println("Today     :: " + todayDate);
		System.out.println("Next date :: " + findNextDay(todayDate));
		System.out.println("Prev date :: " + findPrevDay(todayDate));
	}
	
	private static Date findNextDay(Date date)
	{
		return new Date(date.getTime() + MILLIS_IN_A_DAY);
	}
	
	private static Date findPrevDay(Date date)
	{
		return new Date(date.getTime() - MILLIS_IN_A_DAY);
	}
	
	private static LocalDate findNextDay(LocalDate localdate)
	{
		return localdate.plusDays(1);
	}
	
	private static LocalDate findPrevDay(LocalDate localdate)
	{
		return localdate.minusDays(1);
	}
}

Program output:

Today     :: Sun May 03 19:49:34 IST 2020
Next date :: Sun May 03 19:49:34 IST 2020
Prev date :: Fri May 01 19:49:34 IST 2020

Today     :: 2020-05-02
Next date :: 2020-05-03
Prev date :: 2020-05-01

Drop me your questions in comments.

Happy Learning !!

Sourcecode Download

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)