Solved

Date time question

Posted on 2004-10-11
14
253 Views
Last Modified: 2010-03-31

If I have startDate as "11.10.2004 10:00" and endDate as "11.10.2004 18:00", i do the calculation normally.

If I have start date as "11.10.2004 10:00" and end date as "13.10.2004 08:00", i want to have:

initially,

      startDate = "11.10.2004 10:00"
    endDate = "11.10.2004 23:59"
                
in the next time loop,

      startDate = "12.10.2004 00:00"
      endDate = "12.10.2004 23:59"
   
in the next time loop(or final),
      startDate = "13.10.2004 00:00"
      endDate = "13.10.2004 08:00"
          
Thanks.
0
Comment
Question by:ldbkutty
  • 8
  • 3
  • 3
14 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 12274703
Just increment the date

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
cal.setTime(sdf.parse("11.10.2004 23:59"));
for(int i = 0;i < 3;i++) {
      cal.add(Calendar.DATE, i);
      Date d = cal.getTime();
      System.out.println(d);
 
}
               

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12274712
(You will have to deal with the end part in a similar way, allowing for the final 'non-symmetrical' end date)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12274787

        String startDate = "11.10.2004 10:00";
        String endDate = "13.10.2004 08:00";
        sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
        Date dStart=null, dEnd=null;
        try {
         dStart = sdf.parse(startDate);
         dEnd = sdf.parse(endDate);
        } catch (ParseException ex) {
           
        }
       
        Calendar cal = Calendar.getInstance();
        cal.setTime(dStart);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
       
        Date d = cal.getTime();
        boolean done = false;
        do {
            // Do your stuff
            System.out.println("Start= " + d);
            cal.setTime(d);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date d2 = cal.getTime();
            if ( d2.after(dEnd) ) {
                System.out.println("End= " + dEnd); // Use dEnd
                done = true;
            }
            else {
                System.out.println("End= " + d2); // Use d2
            }
           
            cal.setTime(d);
            cal.add(Calendar.DATE, 1);
            d = cal.getTime();
        } while (!done);
       
Output:
Start= Mon Oct 11 00:00:00 CEST 2004
End= Mon Oct 11 23:59:00 CEST 2004
Start= Tue Oct 12 00:00:00 CEST 2004
End= Tue Oct 12 23:59:00 CEST 2004
Start= Wed Oct 13 00:00:00 CEST 2004
End= Wed Oct 13 08:00:00 CEST 2004
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Accepted Solution

by:
zzynx earned 450 total points
ID: 12274803
Correction:  (Forgot that your first start date should be the given one)

        String startDate = "11.10.2004 10:00";
        String endDate = "13.10.2004 08:00";
        sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
        Date dStart=null, dEnd=null;
        try {
         dStart = sdf.parse(startDate);
         dEnd = sdf.parse(endDate);
        } catch (ParseException ex) {
           
        }
       
        Calendar cal = Calendar.getInstance();
       
        Date d = dStart;
        boolean done = false;
        do {
            // Do your stuff
            System.out.println("Start= " + d);
            cal.setTime(d);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date d2 = cal.getTime();
            if ( d2.after(dEnd) ) {
                System.out.println("End= " + dEnd); // Use dEnd
                done = true;
            }
            else {
                System.out.println("End= " + d2); // Use d2
            }
           
            cal.setTime(d);
            cal.add(Calendar.DATE, 1);
            d = cal.getTime();
        } while (!done);

Output:
Start= Mon Oct 11 10:00:00 CEST 2004
End= Mon Oct 11 23:59:00 CEST 2004
Start= Tue Oct 12 10:00:00 CEST 2004
End= Tue Oct 12 23:59:00 CEST 2004
Start= Wed Oct 13 10:00:00 CEST 2004
End= Wed Oct 13 08:00:00 CEST 2004
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12274812
And of course your catch should

        } catch (ParseException ex) {

        }

contain something meaningful
0
 
LVL 32

Author Comment

by:ldbkutty
ID: 12274822
Thanks, but i actually want the output as:

Output:
Start= Mon Oct 11 10:00:00 CEST 2004
End= Mon Oct 11 23:59:00 CEST 2004
Start= Tue Oct 12 00:00:00 CEST 2004
End= Tue Oct 12 23:59:00 CEST 2004
Start= Wed Oct 13 00:00:00 CEST 2004
End= Wed Oct 13 08:00:00 CEST 2004

didnt went thru ur program...will look now,. :-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12274840
See my last comment ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12274888
Sorry ldbkutty. Another correction is needed at the end of the loop:

            cal.add(Calendar.DATE, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);   // <<< add this
            cal.set(Calendar.MINUTE, 0);             // <<< add this
            d = cal.getTime();
0
 
LVL 32

Author Comment

by:ldbkutty
ID: 12274894
Actually, in your first o/p, i see:

Output:
Start= Mon Oct 11 00:00:00 CEST 2004 // PS: Here
End= Mon Oct 11 23:59:00 CEST 2004
Start= Tue Oct 12 00:00:00 CEST 2004   // PS: Here
End= Tue Oct 12 23:59:00 CEST 2004
Start= Wed Oct 13 00:00:00 CEST 2004 // PS: Here
End= Wed Oct 13 08:00:00 CEST 2004

in the second,

Output:
Start= Mon Oct 11 10:00:00 CEST 2004 // PS: Here
End= Mon Oct 11 23:59:00 CEST 2004
Start= Tue Oct 12 10:00:00 CEST 2004 // PS: Here
End= Tue Oct 12 23:59:00 CEST 2004
Start= Wed Oct 13 10:00:00 CEST 2004 // PS: Here
End= Wed Oct 13 08:00:00 CEST 2004

but i actually want the output as:

Output:
Start= Mon Oct 11 10:00:00 CEST 2004 // PS: Here
End= Mon Oct 11 23:59:00 CEST 2004
Start= Tue Oct 12 00:00:00 CEST 2004 // PS: Here
End= Tue Oct 12 23:59:00 CEST 2004
Start= Wed Oct 13 00:00:00 CEST 2004 // PS: Here
End= Wed Oct 13 08:00:00 CEST 2004

Thanks,.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12274910
The last correction gives the output you want. Sorry.
0
 
LVL 32

Author Comment

by:ldbkutty
ID: 12274915
ok, sorry for not looking at your code :-(

thanks :-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12274925
So to conclude:

        String startDate = "11.10.2004 10:00";
        String endDate = "13.10.2004 08:00";
        sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
        Date dStart=null, dEnd=null;
        try {
         dStart = sdf.parse(startDate);
         dEnd = sdf.parse(endDate);
        } catch (ParseException ex) {
           
        }
       
        Calendar cal = Calendar.getInstance();
       
        Date d = dStart;
        boolean done = false;
        do {
            // Do your stuff
            System.out.println("Start= " + d);            // Start with the given start date
            cal.setTime(d);
            cal.set(Calendar.HOUR_OF_DAY, 23);   // set hours to 23
            cal.set(Calendar.MINUTE, 59);             // minutes to 59
            cal.set(Calendar.SECOND, 0);             // clear seconds
            cal.set(Calendar.MILLISECOND, 0);     // and millis
            Date d2 = cal.getTime();
            if ( d2.after(dEnd) ) {                         // Check if we don't exceed our stop date
                System.out.println("End= " + dEnd); // Use dEnd
                done = true;
            }
            else {
                System.out.println("End= " + d2); // Use d2
            }
           
            cal.setTime(d);
            cal.add(Calendar.DATE, 1);                // Add one day
            cal.set(Calendar.HOUR_OF_DAY, 0);   // set hours to 0
            cal.set(Calendar.MINUTE, 0);              // minutes too
            d = cal.getTime();
        } while (!done);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12274928
Thank you :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12284933
8-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

830 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