Calculate Future Or Past Date In Java
Introduction
This tutorial will show you how you can calculate future or past date in Java.
The calculation is done on future date from a particular date to âplus a number of given daysâ or past date from a particular date to âminus a number of given daysâ.Future or past date calculation may be required for various purposes such as to calculate the age of a person on future date or on past date.
Related Posts:
- Calculate Future or Past Date in PHP
- Add or subtract days, weeks, months, years on Java 8 Date
- Check Given Date is Future or Past Date or Today
Prerequisites
Java 1.8+
Project Setup
The following pom.xml file can be used for maven based project:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.roytuts</groupId>
<artifactId>java-future-or-past-date</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies></dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</build>
</project> Calculate Future or Past Date
The below source code shows how you can calculate future or past date in Java.
package com.roytuts.java.future.or.past.date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class FuturePastDateApp {
private static final String DATE_FORMAT = "yyyy-MM-dd";
/**
* @param args
*/
public static void main(String[] args) {
String date = "2015-02-25"; // date from which a next or past date will be calculated
String futureDate = addDaysToDate(date, "280"); // get date after adding 280 days to the above date
System.out.println("Future Date : " + futureDate + "(" + DATE_FORMAT + ")");
String pastDate = subtractDaysFromDate(date, "30"); // get past date after subtracting 30 days from the above
// date
System.out.println("Past Date : " + pastDate + "(" + DATE_FORMAT + ")");
}
/**
*
* @param date
* @param days
* @return string
*/
private static String addDaysToDate(String date, String days) {
Calendar c = Calendar.getInstance();
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
try {
Date myDate = df.parse(date.trim());
c.setTime(myDate);
c.add(Calendar.DATE, Integer.parseInt(days));
} catch (ParseException e) {
e.printStackTrace();
}
String toDate = df.format(c.getTime());
return toDate;
}
/**
*
* @param date
* @param days
* @return string
*/
private static String subtractDaysFromDate(String date, String days) {
Calendar c = Calendar.getInstance();
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
try {
Date myDate = df.parse(date.trim());
c.setTime(myDate);
c.add(Calendar.DATE, (Integer.parseInt(days) * -1));
} catch (ParseException e) {
e.printStackTrace();
}
String toDate = df.format(c.getTime());
return toDate;
}
}
Output
On executing the above Java class you will see below output:
Future Date : 2015-12-02(yyyy-MM-dd)
Past Date : 2015-01-26(yyyy-MM-dd)
No comments
Leave a comment