Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Date-Calendar java yesterday

Have Programming Question? Ask it here!
 
 
 


hendra
java yesterday
1 Answer(s)      5 years and 11 months ago
Posted in : Date-Calendar



Advertisement
View Answers

October 11, 2008 at 11:43 AM


for getting year month and days between two dates use following code

and for took a calender from another windwo use java script

package rajanikant.code.sep_2008;

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

public class DateDifference {

public static void main(String s[]) throws ParseException{


Date date1 = new Date();
Calendar calendar = Calendar.getInstance();

calendar.setTime(date1);
int cur_month = calendar.get(Calendar.MONTH)+1;
int cur_year = calendar.get(Calendar.YEAR);
int cur_date = calendar.get(Calendar.DATE);
String df1 = cur_date + "-" + cur_month + "-" + cur_year;

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date2;
String newdate = "19-07-2006";
date2 = sdf.parse(newdate);
calendar.setTime(date2);
int month = calendar.get(Calendar.MONTH)+1;
int year = calendar.get(Calendar.YEAR);
int date = calendar.get(Calendar.DATE);

String df2 = date + "-" + month + "-"+ year;

int day = 0;
int no_of_days = 0;
int flag = 0;
if (cur_year % 4 == 0) {

if (cur_year % 100 != 0) {
flag = 1;
}
else if (cur_year % 400 == 0) {
flag = 1;
}
else {
flag = 0;
}
}
// It is not divisible by 4.
else {
flag = 0;
}
if(cur_month == 2){
if(flag == 1){
no_of_days = 29;
}else{
no_of_days = 28;
}
}


if(cur_month == 1 || cur_month == 3 || cur_month == 5 || cur_month == 7 || cur_month == 8 || cur_month == 10 || cur_month == 12){
no_of_days = 31;
}
if(cur_month == 4 || cur_month == 6 || cur_month == 9 || cur_month == 11){
no_of_days = 30;
}
if(cur_date > date){
day = cur_date - date;
}else{
day = cur_date - date + no_of_days;
cur_month = cur_month - 1;
}

if(cur_month > month){
cur_month = cur_month - month;
}else{
cur_month = cur_month - month + 12;
cur_year = cur_year - 1;
}
if(cur_year > year){
cur_year = cur_year - year;
}else{
cur_year = 0;
}

System.out.println("Difference Between " + df1 + " and " + df2 + " is : " +cur_year + " Year " + cur_month + " Month " +day + " Days");
}
}


Rajanikant


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com