
Hello,
I have a problem about how to calculate date, the result from this code is not complete , this is my code . please help me. thank you
public void a(){
String date1 = jTextField33.getText(); String date2 = jTextField12.getText(); try { getDiffer(date1, date2); } catch (ParseException e1) { // TODO Auto-generated catch block //e1.printStackTrace(); } } public void getDiffer(String dt1, String dt2) throws ParseException{
Date date1;// = new Date(); Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); date1 = sdf.parse(dt1); 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 = curdate + "-" + curmonth + "-" + cur_year;
Date date2; // String newdate = "19-07-2006"; date2 = sdf.parse(dt2); 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 noofdays = 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){ noofdays = 29; }else{ noofdays = 28; } }
if(curmonth == 1 || curmonth == 3 || curmonth == 5 || curmonth == 7 || curmonth == 8 || curmonth == 10 || cur_month == 12){ noofdays = 31; } if(curmonth == 4 || curmonth == 6 || curmonth == 9 || curmonth == 11){ noofdays = 30; } if(cur_date > date){ day = cur_date - date; }else{ day = curdate - date + noof_days; curmonth = curmonth - 1; }
if(cur_month > month){ curmonth = curmonth - month; }else{ curmonth = curmonth - month + 12; curyear = curyear - 1; } if(cur_year > year){ curyear = curyear - year; }else{ cur_year = 0; }
jTextField32.setText( + day + " Hr " + curmonth + " Bln " +curyear + " Thn " );
}

Here is an example that accepts two dates from the user and calculates the date difference in terms of days.
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
class DateDifference{
public static int daysBetween(Date d1, Date d2){
return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
}
public static void main(String[] args){
JFrame f=new JFrame();
JLabel label1=new JLabel("Enter Date1: ");
JLabel label2=new JLabel("Enter Date2: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
JButton button=new JButton("Calculate");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
String d1=text1.getText();
String d2=text2.getText();
Date date1 = new SimpleDateFormat("dd-MM-yyyy").parse(d1);
Date date2 = new SimpleDateFormat("dd-MM-yyyy").parse(d2);
Calendar cal1=Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2=Calendar.getInstance();
cal2.setTime(date2);
int days=daysBetween(cal1.getTime(),cal2.getTime());
JOptionPane.showMessageDialog(null,"No of days: "+days);
}
catch(Exception ex){}
}
});
JPanel p=new JPanel(new GridLayout(3,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(button);
f.add(p);
f.setVisible(true);
f.pack();
}
}

Thank you very much,
How about if i want to get result not just a day, ex : 15 day 2 month 3 year
please help me,
Thank's
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.