?
Solved

Date time question

Posted on 2004-10-11
14
Medium Priority
?
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 3
14 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 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
Technology Partners: 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!

 
LVL 37

Accepted Solution

by:
zzynx earned 1800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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