Hi Friends,

This is post Dedicated to who are attending Java based Interview, be caution now a days some of the companies asking to write date difference program in any language. Even experience also they don't consider, what you have they consider only programmatic skill. Experience people may feel very strange. and they are giving some twist do you know they are providing system without any latest updates and no internet connection, all of suddenly they asking to write program. So please go thought the following program it may help you.



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

public class DateCheck {
public static void  main(String arg[]) {

try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd");
//Date currentDate = new Date();
Date date2 = sdf.parse("2013-Jan-1");

Date currentDate = sdf.parse("2013-Feb-28");

Calendar cal1 = Calendar.getInstance();
cal1.setTime(currentDate);


cal1.set(Calendar.HOUR_OF_DAY, 0);
cal1.set(Calendar.MINUTE, 0);
cal1.set(Calendar.SECOND, 0);
cal1.set(Calendar.MILLISECOND, 0);

Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);


System.out.println(cal1.getTime()+"----"+cal2.getTime());
long diff = cal1.getTimeInMillis() - cal2.getTimeInMillis();
System.out.println(diff);
long diffDays = diff / (24 * 60 * 60 * 1000)+1;
System.out.println(diffDays);

if(diffDays==0){
System.out.println("Both Dates are same");
}
else if(diffDays>0){
System.out.println("Given date is less than current date");
}
else if(diffDays<0){
System.out.println("Given date is Greater than current date");
}
}catch(Exception e){
System.out.println(e);
}
}
}

Note: This is not a regular post. currently running post on struts

Post a Comment

 
Top