Wednesday 24 September 2014

Java program to add or substract dates

Java program to add or substract dates

Below is the java program for date addition. In example below, you are adding 3 days to date 20140101. You can change the format of the date by changing the format mentioned in the code in red below. 


-----dateadd.java code------------

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.ParseException;
public class dateadd {
public static void main (String[] args) {
DateFormat formatter = new SimpleDateFormat("yyyymmdd");
Date dt=new Date();
try
            {
 dt = formatter.parse(args[0]);
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.DATE,Integer.parseInt(args[1]) );
dt = c.getTime();
System.out.println(formatter.format(dt));
            } catch (ParseException e)
            {
                e.printStackTrace();
            }
}
}


Also check out the commonly used date functions in netezza:
http://dwbitechguru.blogspot.ca/2014/09/commonly-used-netezza-date-functions.html

No comments:

Post a Comment