Pages

Monday, November 30, 2020

Java Get Next And Previous Dates

1. Overview

In this tutorial, We'll learn how to get next and previous dates from the current date or for any date in old and new java 8 beyond.

Sometime it is referred as tomorrows or yesterdays date is needed for a particular date. 

To solve this problem, we should be clear on the input date format. If the date is in string format then first need to get the date format in string and next need to convert into java Date.

Sometimes, input can be date in the format of java.util.Date or java.time.LocalDate.

Let us explore the all possible ways to get the old and new dates from the given date.

Java Get Next And Previous Dates


2. Getting the Next & Previous Date from java.util.Date


First, we will look at date in string format and next direct Date() object.


2.1 Date in String Format


Assume that input date is given in string format and we will print the next and previous dates also in the same string format.

Steps to get the next date:

Step 1: Convert the string to date
Step 2: Get the milliseconds from date and add 1 day milliseconds (24 * 60 * 60 * 1000) 
Step 3: Next, covert this addition of milliseconds to date and this will be next date for the given date.
Step 4: Convert next date to string form as input.

Steps to get the previous date:

Step 1: Convert the string to date
Step 2: Get the milliseconds from date and subtract 1 day milliseconds (24 * 60 * 60 * 1000) 
Step 3: Next, covert this subtraction of milliseconds to date and this will be previous date for the given date.
Step 4: Convert next date to string form as input.

The below examples covers how to increment or decrement a date by one day in java.


package com.javaprogramto.java8.dates.nextprevious;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NextPreviousFromStringDate {

	// Millseconds in a day
	private static final long ONE_DAY_MILLI_SECONDS = 24 * 60 * 60 * 1000;

	// date format
	private static final String DATE_FORMAT = "yyyy-MM-dd";

	public static void main(String[] args) throws ParseException {

		// Date in string format as "YYYY-MM-DD"
		String dateInString = "2021-12-12";

		// Simple date formatter
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
		Date date = dateFormat.parse(dateInString);

		// Getting the next day and formatting into 'YYYY-MM-DD'
		long nextDayMilliSeconds = date.getTime() + ONE_DAY_MILLI_SECONDS;
		Date nextDate = new Date(nextDayMilliSeconds);
		String nextDateStr = dateFormat.format(nextDate);

		// Getting the previous day and formatting into 'YYYY-MM-DD'
		long previousDayMilliSeconds = date.getTime() - ONE_DAY_MILLI_SECONDS;
		Date previousDate = new Date(previousDayMilliSeconds);
		String previousDateStr = dateFormat.format(previousDate);

		// printing the input, tomorrow and yesterday's date as strings. 
		System.out.println("Given Date : " + dateInString);
		System.out.println("Next Date : " + nextDateStr);
		System.out.println("Previous Date : " + previousDateStr);
	}
}
 
Output:
Given Date : 2021-12-12
Next Date : 2021-12-13
Previous Date : 2021-12-11
 

2.2 Get Next and Previous date from java.util.Date format


Next example is to get the next and previous dates from java.util.Date as current date time.
package com.javaprogramto.java8.dates.nextprevious;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NextPreviousFromUtilDate {

	// Millseconds in a day
	private static final long ONE_DAY_MILLI_SECONDS = 24 * 60 * 60 * 1000;

	// date format
	private static final String DATE_FORMAT = "yyyy-MM-dd";

	public static void main(String[] args) throws ParseException {

		// current date time
		Date currentDate = new Date();

		// Simple date formatter to show in the input and output dates to redable form.
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

		// Getting the next day and formatting into 'YYYY-MM-DD'
		long nextDayMilliSeconds = currentDate.getTime() + ONE_DAY_MILLI_SECONDS;
		Date nextDate = new Date(nextDayMilliSeconds);
		String nextDateStr = dateFormat.format(nextDate);

		// Getting the previous day and formatting into 'YYYY-MM-DD'
		long previousDayMilliSeconds = currentDate.getTime() - ONE_DAY_MILLI_SECONDS;
		Date previousDate = new Date(previousDayMilliSeconds);
		String previousDateStr = dateFormat.format(previousDate);

		// printing the input, tomorrow and yesterday's date as strings.
		System.out.println("Current Date : " + dateFormat.format(currentDate));
		System.out.println("Next Date : " + nextDateStr);
		System.out.println("Previous Date : " + previousDateStr);
	}
}
 
Output:
Current Date : 2020-11-30
Next Date : 2020-12-01
Previous Date : 2020-11-29
 

3. Java 8 - Getting the Next & Previous Date


Next, How to get the tomorrow and yesterday dates in java 8.

First create a date object using LocalDate class. Next, call its plusDays(1) and minusDays(1) methods to get the next and previous dates in java.

These two methods are part of java 8 api and simplifies the processing of adding required number of days.

In our case, we need to add one day to get next date and mins one day to get previous date.

package com.javaprogramto.java8.dates.nextprevious;

import java.text.ParseException;
import java.time.LocalDate;

/**
 * How to get the next and previous dates in java 8?
 * 
 * @author javaprogramto.com
 *
 */

public class Java8NextPreviousDates {

	public static void main(String[] args) throws ParseException {

		// Obtaining current date
		LocalDate currentDate = LocalDate.now();

		// Getting the next date using plusDays() method
		LocalDate nextDate = currentDate.plusDays(1);

		// Getting the previous date using minusDays() method
		LocalDate previousDate = currentDate.minusDays(1);

		// printing the input, tomorrow and yesterday's dates
		System.out.println("Current Date : " + currentDate);
		System.out.println("Next Date : " + nextDate);
		System.out.println("Previous Date : " + previousDate);
	}
}
 
Output:
Current Date : 2020-11-30
Next Date : 2020-12-01
Previous Date : 2020-11-29
 


4. Conclusion


In this article, we've seen how to obtain the next and previous days from current date or any given time in traditional java.util.Date class and LocalDate in java 8.




No comments:

Post a Comment

Please do not add any spam links in the comments section.