Monday, January 28, 2013

How to Add 1 Year to Java Date


Add 1 Year to Java Date

public static void main(String[] args) {
        SampleJava sampleJava = new SampleJava();
       
      String refEffDateStr=  "28-Jan-2013" ;
       System.out.println("Old Date -->"+ refEffDateStr) ;
        Calendar calendar = Calendar.getInstance();
       
         try{
               SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
              java.util.Date javaUtilDate = simpleDateFormat.parse(refEffDateStr);
              
               calendar.setTime(javaUtilDate);
              
               calendar.add(Calendar.YEAR,1);
               Date newDate = calendar.getTime();
              
               long javaMilliseconds = javaUtilDate.getTime();
               java.sql.Date javaSqlDate = new java.sql.Date(javaMilliseconds);
               System.out.println("New Date Is -->"+ calendar.getTime());
               System.out.println("New Java Date Is -->"+ newDate);
           }
           catch(Exception e){
              
               e.printStackTrace();
           }
       
        System.out.println("In  Method :setRetroFinEffDate-->Value-->"+refEffDateStr );
       
    }

No comments: