Example Code

Java Date Utility methods: Getting past/future date from days

1 Mins read

Date manipulation is required in everyday Java programming. Creating a date instance for a past or future date is required at many places where we are trying to have a date range.
The Java Utility method getDateBeforeDays() returns a java.util.Date instance with a date before number of days specified.

JAVA Date Utility Tutorial Java,JAVA Date Utility, This program Demonstrates the proper use of Date functionality,Date and Time, get past and future dates, util methods to get date from past/previous days. util method/function for getting a future date. java date utilities, java date utility, get java date after days, hours, minutes, seconds, java calculate future date, java calculate past date,
/**
* This utility method returns a past date before number of days.
* @param days
* @return
*/
public static Date getDateBeforeDays(int days) {
long backDateMS = System.currentTimeMillis() - ((long)days) *24*60*60*1000;
Date backDate = new Date();
backDate.setTime(backDateMS);
return backDate;
}

The Java Utility method getDateAfterDays() returns a java.util.Date instance with a date after number of days specified.

/**
* This utility method returns a future date after number of days.
* @param days
* @return
*/
public static Date getDateAfterDays(int days) {
long backDateMS = System.currentTimeMillis() + ((long)days) *24*60*60*1000;
Date backDate = new Date();
backDate.setTime(backDateMS);
return backDate;
}    

You can keep these Utility methods as public static methods of a class say DateUtility and call it anywhere in your application using DateUtility.getDateAfterDays() or DateUtility.getDateBeforeDays().

display a future date in java, java date future, java future date
Related posts
.NETExample CodeProgramming

Custom CRM Search Application in MVC

32 Mins read
Today, I will explain how to create advanced search using MVC application same as CRM Advanced Search. For that first you have…
CheatsheetExample CodeExcelTutorials

15 Useful Excel Formula Cheat Sheet

5 Mins read
Looking for list of excel formulas? We have a create a cheat sheet for excel formulas to help beginners. The excel Spreadsheet…
Example CodeJavaRegEx

How To Use Regular Expressions In Java

3 Mins read
Have you ever found yourself needing to search through a string and check whether a condition is true or not? For instance,…
Power your team with InHype

Add some text to explain benefits of subscripton on your services.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *