Saturday, 7 December 2013

Textual description of firstImageUrl

Date Airthmetics Java

This is a simple java program which will convert a date range in year wise manner . For example if from date is 10th jan 2012 and to date is 30th april 2013 then output of this program will be 10th jan 2012 to 31st dec 2012 and 1st jan 2013 to 30th april 2013 .
package com.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateTest
{
    public static void main(String[] args) throws ParseException
 {
  
     SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy");
     Date from = fmt.parse("01/10/12");
     Date to = fmt.parse("04/30/12");
     
     Calendar fromCal = Calendar.getInstance();
     fromCal.setTime(from);
     int fromYear = fromCal.get(Calendar.YEAR);
     Calendar toCal = Calendar.getInstance();
     toCal.setTime(to);
     int toYear = toCal.get(Calendar.YEAR);
     
     int diff = toYear - fromYear ;
     
     for(int i =0 ; i;ltdiff ; i++)
     {
      Calendar calendarEnd=Calendar.getInstance();
         calendarEnd.set(Calendar.YEAR,fromYear);
         calendarEnd.set(Calendar.MONTH,11);
         calendarEnd.set(Calendar.DAY_OF_MONTH,31);
         
         Date calendarEndDate =calendarEnd.getTime();
         
         System.out.println("From "+ fmt.format(from)+" To " +fmt.format(calendarEndDate));
         
         calendarEnd.add(Calendar.DATE,1);
         from = calendarEnd.getTime();
         fromYear++;
     }
     
      System.out.println("From "+ fmt.format(from)+" To " +fmt.format(to));
     
 }
}
Post comments and Suggestions .