1

I made a small program to count days. to count 150 days from May 22nd. But the result is 18th of October. The actual date is 19th October. Can anyone help me find whats wrong with my code.


Calendar mine = new GregorianCalendar(2013, Calendar.MAY,22);
    int month = Calendar.MAY;
    int counter = 0;
    for(int i=mine.get(Calendar.DAY_OF_MONTH);i<=mine.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
    System.out.println("i "+i);

        counter++;
        System.out.println("counter "+counter);
    if(i==mine.getActualMaximum(Calendar.DAY_OF_MONTH)){
        month++;
        i=1;

        mine.set(2013, month, i);
        counter++;
        System.out.println("i "+i+" "+mine.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
        if(counter == 150){

        System.out.println("day "+i+ counter +"days"+ "month:"+ mine.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
        break;
        }
    }
        if(counter == 150){

        System.out.println("i "+i+" counter "+ counter +" date:"+mine.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
        break;
        }
        }
Dan Jay
  • 874
  • 10
  • 27

3 Answers3

3

You can just use add function of the Calendar

Calendar mine = new GregorianCalendar(2013, Calendar.MAY,22);
mine.add(Calendar.DAY_OF_YEAR, 150);
System.out.println(mine.getTime());

will print

Sat Oct 19 00:00:00 IST 2013

But the real problem with your code was that, you were incrementing counter first and then doing comparison with 150.

do it like this and it should fix your code

if(counter == 150){
   System.out.println("i "+i+" counter "+ counter +date:"+mine.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));
   break;
}
counter++;
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • wow... thanks. How silly I missed that. – Dan Jay May 24 '13 at 06:33
  • THANKS sanbhat,, vikingsteve,, MadProgrammer for your valuable help. – Dan Jay May 24 '13 at 06:41
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Mar 02 '17 at 05:29
2

Or you could use Joda DateTime:

import org.joda.time.DateTime;
....
System.out.println(new DateTime("2013-05-22").plusDays(150));
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Mar 02 '17 at 05:29
  • @BasilBourque correct. Everyone - please see Basil's answer, I'm upvoting it, it should rise to the top. – vikingsteve Mar 02 '17 at 08:23
2

tl;dr

LocalDate.of( 2013, Month.MAY, 22 )
         .plusDays( 150 )

Using java.time

Much easier with the modern java.time classes.

The LocalDate class represents a date-only value without time-of-day and without time zone.

The Month enum offers a dozen pre-defined objects, one for each month of the year.

LocalDate ld = LocalDate.of( 2013, Month.MAY, 22 ) ;
LocalDate later = ld.plusDays( 150 );

See this code live at IdeOne.com.

2013-10-19


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154