Solved

Date time question

Posted on 2004-10-11
14
238 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now