Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

import java.util.*; import java.text.DateFormat; import javax.swing.JOptionPane; public class zz4_dateDifference extends javax.swing.JFrame { private void jButton1ActionPerformed(java.awt.event.

ActionEvent evt) { DateFormat df; df=DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK);

Date dt1=new Date(); // Date type variable dt1 declared Date dt2=new Date(); // Date type variable dt2 declared

String sd1=d1.getText(); // d1 is the name of the textfield that will accept start date in ddmm-yy format String sd2=d2.getText();// d2 is the name of the textfield that will accept end date in ddmm-yy format

try{ dt1=df.parse(sd1); // converting string sd1 to date type and storing in Date type variable dt1 dt2=df.parse(sd2); // converting string sd2 to date type and storing in Date type variable dt2 }catch(Exception e){}

long diffinms=dt2.getTime()-dt1.getTime(); long ndays=diffinms/(1000*60*60*24);

JOptionPane.showMessageDialog(this,dt1+" Diiference "+ndays);

/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new zz4_dateDifference().setVisible(true); } }); }

// Variables declaration - do not modify private javax.swing.JTextField d1; private javax.swing.JTextField d2; private javax.swing.JTextField d3; private javax.swing.JButton jButton1; // End of variables declaration

You might also like