How get instance of Past or Future date in Java

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.

I have seen many developers keep searching on google about "how to add or subtract days in java.util.Date". or "how to calculate 7 days date past or future from present date in core java"

My previous post about date utility methods used setTime() method of Date object for getting the back date. After doing some more reading I realized that this can be done in a much easier manner by using java.util.Calendar.add() method. Good thing about this method is the static final constants provided by java.util.Calendar class itself. This gives more flexibility in manipulating the date with all levels of constants.

Calendar.SECOND
Calendar.MINUTE
Calendar.HOUR
Calendar.DATE
Calendar.MONTH
Calendar.YEAR


Below are the example methods which uses Calendar.add method with these constants. This demonstrates how we can get a past of future date for any time we are looking for(Seconds, minutes,hours, days,months or years)


/**
* Get the date java.util.Date object for days before current date
*
* @param days
* @return
*/
public static Date getDateBeforeDays(int days) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -days); // -days
return cal.getTime();
}

/**
* Get the date java.util.Date object for days after current date
*
* @param days
* @return
*/
public static Date getDateAfterDays(int days) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, days); // +days
return cal.getTime();
}

/**
* Get the date java.util.Date object for years after current date
*
* @param years
* @return
*/
public static Date getDateAfterYears(int years) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, years); // +years
return cal.getTime();
}

/**
* Get the date java.util.Date object for years before current date
*
* @param years
* @return
*/
public static Date getDateBeforeYears(int years) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -years); // -years
return cal.getTime();
}

/**
* Get the date java.util.Date object for months after current date
*
* @param months
* @return
*/
public static Date getDateAfterMonths(int months) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, months); // +months
return cal.getTime();
}

/**
* Get the date java.util.Date object for months before current date
*
* @param months
* @return
*/
public static Date getDateBeforeMonths(int months) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -months); // -months
return cal.getTime();
}

/**
* Get the date java.util.Date object for hours before current date
*
* @param hours
* @return
*/
public static Date getDateBeforeHours(int hours) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -hours); // -hours
return cal.getTime();
}

/**
* Get the date java.util.Date object for hours after current date
*
* @param hours
* @return
*/
public static Date getDateAfterHours(int hours) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, hours); // +hours
return cal.getTime();
}

/**
* Get the date java.util.Date object for minutes before current date
*
* @param minutes
* @return
*/
public static Date getDateBeforeMinutes(int minutes) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -minutes); // -minutes
return cal.getTime();
}

/**
* Get the date java.util.Date object for minutes after current date
*
* @param minutes
* @return
*/
public static Date getDateAfterMinutes(int minutes) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, minutes); // +minutes
return cal.getTime();
}

/**
* Get the date java.util.Date object for seconds before current date
*
* @param seconds
* @return
*/
public static Date getDateBeforeSeconds(int seconds) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, -seconds); // -seconds
return cal.getTime();
}

/**
* Get the date java.util.Date object for seconds after current date
*
* @param seconds
* @return
*/
public static Date getDateAfterSeconds(int seconds) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, seconds); // +seconds
return cal.getTime();
}






How to get Java Date of past?, How to get Java Date of future?, How to get Java Date after days, Java Date before days, Java Date after months, Java date after years,Java Date before months, Java date before years, Java Date future, getting of future date by using number of days and format java, getting of past date by using number of days and format java, How to get Java Time of past?, How to get Java Time of future?, How to get Java Time after days, Java Time before days, Java Time after months, Java date after years,Java Time before months, Java date before years, Java Time future, getting of future date by using number of days and format java, getting of past date by using number of days and format java, Java previous Date, Java next date, How to get Java Time of previous?, How to get Java Time of next? Java Time next, getting of next date by using number of days and format java, getting of previous date by using number of days and format java, How to get Java Date of previous?, How to get Java Date of next? Java Date next, calendar getinstance, Java date utilities, Java date Utility, Java date Util, display a future date in java, java date future, java future date, java date util functions, date function java, util function java

Related

How-To 5870367862050726481

Post a Comment Default Comments

...

item