Solved

Date time question

Posted on 2004-10-11
14
267 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 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
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!

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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