Home Answers Viewqa Java-Beginners Ask date difference

 
 


hendra
Ask date difference
2 Answer(s)      11 months ago
Posted in : Java Beginners

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 " );

}

View Answers

June 18, 2012 at 4:55 PM


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();
    }
}

June 19, 2012 at 7:10 AM


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









Related Pages:
Ask date difference
Ask date difference  Hello, I have a problem about how to calculate date, the result from this code is not complete , this is my code . please help... dates from the user and calculates the date difference in terms of days. import
ask java - Date Calendar
ask java  sory i'am wrong language. i want to ask to this forum how... but in java i found too but i can't to join in another frame. And i want to ask how to subtract a date, ex:i work since 2008/09/01 so i'am work until now 1 month 10
javascript date difference in years
javascript date difference in years  I want to find the difference... date1=new Date(); var date2 =new Date("Jan, 11, 2000") var yearDiff=date1.getFullYear()-date2.getFullYear(); document.write("Date difference in years : "+yearDiff
javascript date difference in months
javascript date difference in months  How can I find the difference... of months by using getMonth() method .To get the date difference in months you...; <head> <script type="text/javascript"> var date1=new Date(); var
javascript date difference in days.
javascript date difference in days.   I want to find the difference... of days by using method getDay(). Now to find date difference...=new Date(); var date2 =new Date("Jan, 11, 2000") var yearDiff=date1.getFullYear
ask how function jCalender - Date Calendar
ask how function jCalender  d, Halo friend, i want to ask how to make this script can run and call JCalender. I want to know how to make private this code.i has already insert plugin in my netbeans. try
JavaScript Date Difference
JavaScript Date Difference...; In this section, we are going to determine the difference between two dates...() that returns the time (in milliseconds) elapsed from 1st January, 1970 to the current Date
date
date  can u tell me how to calculate difference between a user provided date and the system date in java
date
date  can u tell me how to calculate difference between a user provided date and the system date in java
date
date  i want difference between the date entered in a jtextfield and the system date in java
Ask Servlet questions online
Ask Servlet questions online       Feel free to ask any question about servlets from Roseindia and enjoy the difference of your life because we have just
Date Difference
Date Difference       This example learns how to make the difference between two dates and how... Object class and makes a difference between a Date object and a set of integer
date_diff
date_diff date_diff alias DateTime::diff returns the difference between two DateTime objects. It returns the difference of two dates, if it exists or return... [, bool $absolute ] ) Parameters datetime - The date to compare
Java Swing Date Difference
Java Swing Date Difference In this tutorial, you will learn how to find the difference between two dates. Here is an example that accepts two dates from... the following method that calculates the date difference in terms of days
difference
difference  difference between hashtable and hashtree
difference
difference  what's the difference between mysql and sql
difference
difference  difference b/w java and asp.net
Difference
Difference  What Is the difference between JDK & SDK
Calculate Month Difference in mysql - SQL
Calculate Month Difference in mysql  hi , i am rahul.Can anybody plz help me.i am calculating month difference in mysql5.0 procedure . i have done coding for it. it's counting difference between months. but what i want
difference
difference    what is the difference between the JDBC-ODBC bridge, the Native-API-Partly-Java driver, and the JDBC-Net-All-Java Driver is the placement of the database access libraries   Please visit the following link
difference
difference  difference between thread and process in java   Difference between Process and Thread: 1)Process is a program under execution whereas Thread is a part of program. 2)Process are heavy weight programs which
difference
difference  difference between Method Overloading and method Overriding   Difference between Method Overloading and Method Overriding: Overriding is the concept of having functions of same name and signature
mysql difference between two numbers
mysql difference between two numbers  How to get total bate difference between two dates for example 1/01/2012 and 1/02/2012 in MYSQL?   ..., '01/02/2012', 103))   DATEDIFF() in MYSQL always returns the difference
Difference in two dates - Java Beginners
Difference in two dates  Hello there once again........ Dear Sir... on that. The thing is, that I need to find the difference between the two dates in JAVA..... if something happens in my application, I want to get the difference between the current
Mysql Date Manipulation
Mysql Date Manipulation       Mysql Date Manipulation find out the difference between two values... Date Manipulation. To grasp this example, we create a table 'Person' with field
Date related query - Date Calendar
Date related query  hi, m a new biginner of java.i m intrested in finding out d difference in d two dates "in terms of number of days "entered by d user using bufferedReader. how can i convert a string into a date object.pls
Hi .Difference between two Dates - Java Beginners
Hi .Difference between two Dates  Hi Friend.... Thanks for ur Very good response.. Can u plz guide me the following Program.... difference... static void main(String args[]){ DateDifference difference = new
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss... SimpleDateFormat(format); Date dateObj1 = sdf.parse(date1 + " " + time1); Date
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss... SimpleDateFormat(format); Date dateObj1 = sdf.parse(date1 + " " + time1); Date
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss... SimpleDateFormat(format); Date dateObj1 = sdf.parse(date1 + " " + time1); Date
javascript date comparison
= new Date(); var difference = today - date1; var days =parseInt(difference...javascript date comparison  How to compare dates and display the difference in terms of days?   <html> <head> <script
SQL Date, SQL Date Examples
SQL Date       Mysql Date Today The Tutorial illustrate an example from Date Today in My sql. The Query used in the below example the now
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss... SimpleDateFormat(format); Date dateObj1 = sdf.parse(date1 + " " + time1); Date
calculate difference of two times and express it in terms of hours
calculate difference of two times and express it in terms of hours  i need jsp code to find out the difference of two times in format hh:mm:ss... SimpleDateFormat(format); Date dateObj1 = sdf.parse(date1 + " " + time1); Date
Ask for latest version for hibernate
Ask for latest version for hibernate  any new version for hibernate after 3.0, if yes can u give some exmp for latest version of hibernate
Ask about java
Ask about java  Create a java program for add and remove the details of a person, which is not using by database, a simply java program. If possible, please wil it in switch case
Parsing into date object
Parsing into date object  Here is my code: String de = (String..."); Date d=new Date(); out.println(d); Date toDt; int oneDay = 24*60*60*1000; toDt...(diff); **I tried this to get the difference between the two dates
Parsing into date object
Parsing into date object  Here is my code: String de = (String..."); Date d=new Date(); out.println(d); Date toDt; int oneDay = 24*60*60*1000; toDt...(diff); **I tried this to get the difference between the two dates
Ask about looping in database
Ask about looping in database  Good afternoon, I want to ask something, Now i have 2 tables,name of the table is RULE and Heritage. Table heritage...=True,A2=True : code is the same data Rule and Heritage. I want to ask
Ask XML Questions
Ask XML Questions     .... If our visitors face any sorts of problem in XML scripting, share with us. Ask... need to login and post your questions on our site under our service ‘Ask
ask - Java Beginners
ask   dear how to "print out" into a file ? regard suhadi  Hi friend, Please explain properly requirement. I am sending simple code according to your requirement. import java.io.*; public class
Ask java count
Ask java count  Good morning, I have a case where there are tables sumborrowbook fieldnya: codebook, bookname, and sumborrowbook . I want to make the results: for example: | code book | name of book | sum | | b001
java yesterday - Date Calendar
to make date sustract, the result is to make 1 month and 1 day like this, right. now how about 1 year and 1 month? And Can help me to using Date Calender what... { public static void main(String s[]) throws ParseException{ Date date1
to calculate the difference between two dates in java - Java Beginners
to calculate the difference between two dates in java  to write a function which calculates the difference between 2 different dates 1.The function...(); // Set the date for both of the calendar instance calendar1.set(2007, 12
continue java - Date Calendar
continue java  and how to subtract date(day month year) form jframe...(); } } }); } public void getDiffer(String dt1, String dt2) throws ParseException{ Date date1;// = new Date(); Calendar calendar = Calendar.getInstance
date
date   how to insert date in database? i need coding
PHP Date function
PHP Date Function In this section we will show you the different formatting options available in the PHP Date function PHP date() function provides many formatting options. You can use the options to format the date output
jsp - Date Calendar
jsp  What is the difference between JSP and .Net?  what a great question?..... you should not compare jsp wid .NET JSP is just an web component within java enterprise/web application. .NET is an development platform
What is the difference between JSP expression language and scriptlets? - JSP-Servlet
What is the difference between JSP expression language and scriptlets?  Hi, I just want to ask what is the difference between JSP Expression Langauge and JSP scriptlets? Pls help me.   Hi friend
date
date  how to insert date using html through the combobox

Ask Questions?

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.