Selasa, 08 Juli 2014

How to add, substract days in current date in Java | Increment, decrement date by days with example, tutorial




While working in Java projects, we often needs to increment or decrement
date e.g. adding one days to current date to get tomorrow's date, subtracting
one day to get yesterday's date etc. Since date in Java is maintained as long millisecond value,
Sometime, programmer tend to add 24 hours as one day, which could be wrong if
day falls on a day light saving time zone, where a day could be either 23 or 25
hour long. When you add or subtract days from date,
other components' of date e.g. month and year must roll. In this Java date
tutorial, we will see two ways to
increment and decrement date in Java
. One approach uses
java.util.Calendar from JDK
and other uses
DateUtils class from Apache commons lang
library.
DateUtils class provides convenient addDays(Date,
int days)
method, which accept a date and number of days to add, you can subtract
days by passing negative value. Similarly
java.util.Calendar provides Calendar.add() method,
which accept a calendar field, for adding days, you need to use
Calendar.DAY_OF_MONTH. Similar
to
DateUtils, you can pass positive number to increment
date, and negative integer to decrement date in Java.







Increment, Decrement Date in Java



How to increment and decrement dates in Java with exampleHere is Java program to increment and decrement date in Java. This code
shows multiple examples e.g. adding one days, subtracting one day, adding
multiple days to check if month and year rolls or not. First set of examples,
uses
java.util.Calendar and next set uses static methods from DateUtils of Apache commons
lang library. This program uses
java.util.Date for
demonstration purpose, but if you are receiving date as String, you can also convert String to date in Java,
before calling these method.



import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateUtils;

/**
* Java program to increment, decrement date in Java. This examples shows
* two ways to add or subtract days form date, java.util.Calendar and DateUtils
* from Apache commons lang.
*
* @author Javin Paul
*/

public class IncrementDateJava {

public static void main(String args[]) {

//Using Calendar to increment and decrement days from date in Java
Date today = new Date();
System.out.println("Today is " + toddMMyy(today));

Calendar cal = Calendar.getInstance();

//adding one day to current date
cal.add(Calendar.DAY_OF_MONTH, 1);
Date tommrrow = cal.getTime();
System.out.println("Tomorrow will be " + toddMMyy(tommrrow));

//substracting two day from date in Java
cal.add(Calendar.DAY_OF_MONTH, -2);
Date yesterday = cal.getTime();
System.out.println("Yesterday was " + toddMMyy(cal.getTime()));


//Using Apache commons DateUtils to increment and decrement date in Java
Date increment = DateUtils.addDays(today, 1);
System.out.println("Increment one day to date in Java using DateUtils " + toddMMyy(increment));

Date decrement = DateUtils.addDays(today, -1);
System.out.println("Decrement one day from date in Java " + toddMMyy(decrement));

//adding 27 days to current date in Java, to see if month rolls
Date dateAfter27Days = DateUtils.addDays(today, 27);
System.out.println("Date after 27 days " + toddMMyy(dateAfter27Days));

//adding 305 days to current date to check if year rolls or not
Date afterManyDays = DateUtils.addDays(today, 305);
System.out.println("Date after 305 days in Java " + toddMMyy(afterManyDays));

}


public static String toddMMyy(Date day){
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy");
String date = formatter.format(day);
return date;
}

}

Output
Today is 05-03-13
Tomorrow will be 06-03-13
Yesterday was 04-03-13
Increment one day to date in Java using DateUtils 06-03-13
Decrement one day from date in Java 04-03-13
Date after 27 days 01-04-13
Date after 305 days in Java 04-01-14




Both examples of incrementing dates are simple enough to use. Just
remember that
getInstance() method of Calendar class
takes current locale into account and may return a calendar other than
GregorianCalendar. Also you
might be tempted to use
roll() method from Calendar class,
which seems right for job and it does in case of
GregorianCalendar. Though
it's worth noting that default implementation of
Calendar.roll(int
field, int amount)
doesn't roll months and year.





That's all about how to increment and decrement date in Java. We
have seen, how to add one or multiple days into current date to get a date in
future. Both
Calendar and DateUtils looks good
for the job, and it's upto you, which one to choose. Prefer
Calendar over DateUtils, if you
are not using Apache commons lang in your project.





Relate Java Date and Time tutorials from Javarevisited





























Source:http://javarevisited.blogspot.com/2013/04/ow-to-increment-decrement-date-by-days-java.html

Tidak ada komentar:

Posting Komentar