4

Here is my code fragment. I need to subtract days from a certain date but I'm not getting the results I expect:

public class TestingDates {

    public static void main(String[] args) {
        Date toDate=new Date();
        Date fromDate=new Date();
        for (int i = 0; i < 30; i++) {
            fromDate.setTime(toDate.getTime() - i * 24 * 60 * 60 * 1000);
            System.out.println(fromDate);   
        }
    }
}

And i'm confuse with this result

here is the output

Fri Sep 13 12:24:50 IST 2013
Thu Sep 12 12:24:50 IST 2013
Wed Sep 11 12:24:50 IST 2013
Tue Sep 10 12:24:50 IST 2013
Mon Sep 09 12:24:50 IST 2013
Sun Sep 08 12:24:50 IST 2013
Sat Sep 07 12:24:50 IST 2013
Fri Sep 06 12:24:50 IST 2013
Thu Sep 05 12:24:50 IST 2013
Wed Sep 04 12:24:50 IST 2013
Tue Sep 03 12:24:50 IST 2013
Mon Sep 02 12:24:50 IST 2013
Sun Sep 01 12:24:50 IST 2013
Sat Aug 31 12:24:50 IST 2013
Fri Aug 30 12:24:50 IST 2013
Thu Aug 29 12:24:50 IST 2013
Wed Aug 28 12:24:50 IST 2013
Tue Aug 27 12:24:50 IST 2013
Mon Aug 26 12:24:50 IST 2013
Sun Aug 25 12:24:50 IST 2013
Sat Aug 24 12:24:50 IST 2013
Fri Aug 23 12:24:50 IST 2013
Thu Aug 22 12:24:50 IST 2013
Wed Aug 21 12:24:50 IST 2013
Tue Aug 20 12:24:50 IST 2013
Tue Oct 08 05:27:38 IST 2013
Mon Oct 07 05:27:38 IST 2013
Sun Oct 06 05:27:38 IST 2013
Sat Oct 05 05:27:38 IST 2013
Fri Oct 04 05:27:38 IST 2013

please help me to sort out this issue

0

4 Answers 4

12

You seem to be wanting to calculate backwards 30 days...You should try using Calendar instead

Calendar cal = Calendar.getInstance();
cal.setTime(toDate);
for (int i = 0; i < 30; i++) {
    cal.add(Calendar.DATE, -1);
    System.out.println(cal.getTime());
}
Sign up to request clarification or add additional context in comments.

6 Comments

+1 The difference between the same time from one day to the next can be 23 or 25 hours. This handles daylight savings correctly so it doesn't do the same thing, but may be more correct.
Thanks MadProgrammer :-)
Please do not recommend Calendar anymore. Joda Time is much less confusing to use. The add() method here just takes two integers!
@MichaelPiefel 1st Calendar is in the default libraries; 2nd I would argue that Calendar is generally easier to learn then JodaTime (given the number of classes within the API); 3rd Yes, JodaTime is generally a more powerful API
Given the numerous questions on how to do simple things like finding out last Monday with Java’s Calendar, I disagree about the ‘easier to learn’ part.
|
9

You face the integer overflow. The maximal value for your int variable i is 2^31-1.

Use long type for the i variable to avoid the overflow.

    for (long i = 0; i < 30; i++) {
        fromDate.setTime(toDate.getTime() - i * 24 * 60 * 60 * 1000);
        System.out.println(fromDate);   
    }

1 Comment

@PeterLawrey, I agree. However, for the task itself (subtracting day by day) I would recommend the Calendar solution of @MadProgrammer above.
3

You get an overflow in your calculation

    public static void main(String[] args) {
        Date toDate=new Date();
        Date fromDate=new Date();
        for (int i = 0; i < 30; i++) {
            int x = i * 24 * 60 * 60 * 1000;
            System.out.println(x);
            fromDate.setTime(toDate.getTime() - x);
            System.out.println(fromDate);   
        }
}

results in

0
Fri Sep 13 09:32:00 CEST 2013
86400000
Thu Sep 12 09:32:00 CEST 2013
172800000
Wed Sep 11 09:32:00 CEST 2013
259200000
Tue Sep 10 09:32:00 CEST 2013
345600000
Mon Sep 09 09:32:00 CEST 2013
432000000
Sun Sep 08 09:32:00 CEST 2013
518400000
Sat Sep 07 09:32:00 CEST 2013
604800000
Fri Sep 06 09:32:00 CEST 2013
691200000
Thu Sep 05 09:32:00 CEST 2013
777600000
Wed Sep 04 09:32:00 CEST 2013
864000000
Tue Sep 03 09:32:00 CEST 2013
950400000
Mon Sep 02 09:32:00 CEST 2013
1036800000
Sun Sep 01 09:32:00 CEST 2013
1123200000
Sat Aug 31 09:32:00 CEST 2013
1209600000
Fri Aug 30 09:32:00 CEST 2013
1296000000
Thu Aug 29 09:32:00 CEST 2013
1382400000
Wed Aug 28 09:32:00 CEST 2013
1468800000
Tue Aug 27 09:32:00 CEST 2013
1555200000
Mon Aug 26 09:32:00 CEST 2013
1641600000
Sun Aug 25 09:32:00 CEST 2013
1728000000
Sat Aug 24 09:32:00 CEST 2013
1814400000
Fri Aug 23 09:32:00 CEST 2013
1900800000
Thu Aug 22 09:32:00 CEST 2013
1987200000
Wed Aug 21 09:32:00 CEST 2013
2073600000
Tue Aug 20 09:32:00 CEST 2013
-2134967296
Tue Oct 08 02:34:48 CEST 2013
-2048567296
Mon Oct 07 02:34:48 CEST 2013
-1962167296
Sun Oct 06 02:34:48 CEST 2013
-1875767296
Sat Oct 05 02:34:48 CEST 2013
-1789367296
Fri Oct 04 02:34:48 CEST 2013

see the x value before your date jumps.

Comments

2

Try to print the actual int you get after this line:

i * 24 * 60 * 60 * 1000

Here is the output:

86400000
172800000
259200000
345600000
432000000
518400000
604800000
691200000
777600000
864000000
950400000
1036800000
1123200000
1209600000
1296000000
1382400000
1468800000
1555200000
1641600000
1728000000
1814400000
1900800000
1987200000
2073600000
-2134967296
-2048567296
-1962167296
-1875767296
-1789367296

As you can see you create a number higher than the one an int can store and thus get overflow. The solution is using a long, that is a 64 bit number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.