Solved

Days between Dates

Posted on 2000-04-04
10
363 Views
Last Modified: 2010-03-30
Given two dates (DD/MM/YYYY) can anyone supply me with code that will return the number of days between the two dates.

Also a reverse function is needed given a date and the number of days can you produce code that will return a date

Date is a record of Day and Month and Year that are all integer subtypes.

Days is an integer
0
Comment
Question by:phuzzy
  • 4
  • 3
  • 2
10 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2683117
I'll assume day,month and year are integers:

1.

import java.util.Date;

public class Dates{

   public static long getDaysBetween(int d1, int m1, int y1, int d2, int m2, int y2){

       long secDiff = Date.UTC(y2-1900,m2,d2,0,0,0)-Date.UTC(y1-1900,m1,d1,0,0,0);
       return Math.abs(secDiff/(60*60*24));
    }


    public static Date getDateAfterDays(int d1, int m1, int y1, int days){

       return new Date(Date.UTC(y1-1900,m1,d1)+days*24*60*60);
    }

}


I have to say though that the question looks like a homework question, but in that case, I think my answer doesn't answer it :-)
0
 

Author Comment

by:phuzzy
ID: 2683142
The Story...

2nd Year Student University Of Ulster (20 years old).

Assignment : Develop a room booking system for the Faculty Of Informatics. in ADA!!!

Problem :

This is not exactly a HOMEWORK question.  The teamwork assignment is quickly approaching 3000 lines of code and one of our team members is in hospital with meningitis.  Two weeks past the deadline i have decided to take on one of his packages.  I have 4 other HUGE assignments to hand in a this assignment is beginig to annoy me.  My head is not right and i cant for the life of me work out how to do this.  

I just need a little help on this very small bit of code.  The course director know all abpout this and understands due to the ilness in the team.

As i said before the assigment is in Ada but i know how to read and write in Java so conversion to ADA will be easy, provided no in-built packages of Java are used :-)

Thank You
0
 
LVL 2

Expert Comment

by:azawawi
ID: 2683148
The java.util.Date.getTime() method returns the number of miliseconds since 1970. So to get the difference in seconds between two Dates do something like this:

class Test {
      public static void main( String args[] ) {
            java.util.Date d1, d2;
            java.text.SimpleDateFormat sdf;

            try {
                  sdf = new java.text.SimpleDateFormat();
                  sdf.applyPattern("dd-MM-yyyy");
                  d1 = sdf.parse("1-1-2000");
                  d2 = sdf.parse("1-1-2001");
                  int daysBetween = (int)((d2.getTime() - d1.getTime()) / (1000L * 3600 * 24));
                  System.out.println(daysBetween);
            } catch( java.text.ParseException e ) {
                  // Handle Date Parsing Exception here...
                  System.out.println("Error in Parsing date...");
            }
      }
}

Hope that this helps you...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:phuzzy
ID: 2683182
As i said in my last comment the code is actua;lly going to be converted to ADA95 and therefore java classes that perform the functions cannot be used.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2683455
Do you need that considering that some years have 366 days and that sometimes february has 29 days and that some months have 30 days and others 31?
This sounds like a very easy problem...I've written such stuff when I was in 7th grade as homework assignment...
0
 
LVL 2

Accepted Solution

by:
azawawi earned 100 total points
ID: 2683740
Hope this is what you want...

/*
      This procedure uses the Julian calendar mathmatical equations to convert
      two dates and give the # of days inbetween.
*/

public class CalcDate {

      public static long julian_date( int day, int month, int year ) {
            long jd =
                  (1461 * (year + 4800 + (month - 14) / 12)) / 4 +
                  (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12 +
                  (3 * (( year + 4900 + (month - 14) / 12) / 100)) / 4 +
                  day - 32075;
            return jd;
      }

      public static void main( String args[] ) {
            long d1, d2;

            d1 = julian_date( 1,1,1999 );
            d2 = julian_date( 1,1,2000 );
            System.out.println( d2 - d1 );
      }

}
0
 

Author Comment

by:phuzzy
ID: 2684986
Sarcasim is not appreciated Sasha.  If youre so clever then why didn't you answer the question of the top of your head??????

Don't try to be clever, be clever (or sumfin)

Anyway thanks alot azawawi for the answer
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2687106
Umm, that wasn't sarcasm, we really did solve such problems in school...
I didn't give the answer right away because I was lazy to calculate all the formulas azawawi had and wanted to know first whether you needed it to consider special cases...
0
 

Author Comment

by:phuzzy
ID: 2687787
Sorry I was in a mood and read your response wrongly.

I must apologise but i people agree with me that i does look like a sarcastic response.

Maybe it's my British humour or sumfin

Again Sorry.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 70
CSV file parsing thru Java 13 39
how to debug htl and js pages 8 53
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 63
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question