Solved

Date time question

Posted on 2004-10-11
14
223 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

15 Experts available now in Live!

Get 1:1 Help Now