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.
/** * 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().
how to find a future date….
for an example… from the date "24/4/1994" i want to add 225 days and find the date after that.. how??
i have a date as "04/50"
i am expecting if the current year is 2014 then i want the format is like "04/2014"
please help me some one
how i get the feature date ……..?
i will pass string as "04/50" from this if current year is 2014 then i am expecting 04/2050
please help me some one