2

I have started writing some code and need to print all the dates in a month, I can do this by adding one each day but there must be a shorter way that I am missing. This is my code so far.

I am aware that it is not the prettiest and I am wondering how to print the date while it increments for each day in January without having to constantly add 1 each time then println each time.

public static void main(String [] args) {

Calendar calendar = Calendar.getInstance()
calendar.set.(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));

System.out.println(calendar.getTime());
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());

}

}
Andrew
  • 31
  • 3

5 Answers5

2

simple exmple using Java 8 Local date, asuming input as in the question

import java.time.LocalDate;

public class Test
{
    public static void main(String[] args)
    {
        LocalDate ld = LocalDate.of(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
        do {
            System.out.println(ld.toString());
            ld = ld.plusDays(1);
        } while (ld.getDayOfMonth() > 1);  // arive at 1st of next month
    }
}   
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
1

Here's a simple example that prints all the days in the month. The time parts have been set to zero.

Here, we printed all the days in March 2016.

Tue Mar 01 00:00:00 MST 2016
Wed Mar 02 00:00:00 MST 2016
Thu Mar 03 00:00:00 MST 2016
Fri Mar 04 00:00:00 MST 2016
Sat Mar 05 00:00:00 MST 2016
Sun Mar 06 00:00:00 MST 2016
Mon Mar 07 00:00:00 MST 2016
Tue Mar 08 00:00:00 MST 2016
Wed Mar 09 00:00:00 MST 2016
Thu Mar 10 00:00:00 MST 2016
Fri Mar 11 00:00:00 MST 2016
Sat Mar 12 00:00:00 MST 2016
Sun Mar 13 00:00:00 MST 2016
Mon Mar 14 00:00:00 MDT 2016
Tue Mar 15 00:00:00 MDT 2016
Wed Mar 16 00:00:00 MDT 2016
Thu Mar 17 00:00:00 MDT 2016
Fri Mar 18 00:00:00 MDT 2016
Sat Mar 19 00:00:00 MDT 2016
Sun Mar 20 00:00:00 MDT 2016
Mon Mar 21 00:00:00 MDT 2016
Tue Mar 22 00:00:00 MDT 2016
Wed Mar 23 00:00:00 MDT 2016
Thu Mar 24 00:00:00 MDT 2016
Fri Mar 25 00:00:00 MDT 2016
Sat Mar 26 00:00:00 MDT 2016
Sun Mar 27 00:00:00 MDT 2016
Mon Mar 28 00:00:00 MDT 2016
Tue Mar 29 00:00:00 MDT 2016
Wed Mar 30 00:00:00 MDT 2016
Thu Mar 31 00:00:00 MDT 2016

This code will work with Java 6, Java 7, and Java 8.

package com.ggl.testing;

import java.util.Calendar;

public class PrintMonth {

    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance();

        if (args.length == 2) {
            int year = Integer.parseInt(args[0]);
            int month = Integer.parseInt(args[1]);
            calendar.set(year, month, 1);
        }

        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        while (calendar.get(Calendar.MONTH) == month) {
            System.out.println(calendar.getTime());
            calendar.add(Calendar.DATE, 1);
        }

    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
1
public static void main(String[] args) {
        LocalDate date = LocalDate.of(Integer.parseInt(args[0]), Integer.parseInt(args[1]), 1);

        for (int i = 0; i < date.lengthOfMonth(); i++) {
            System.out.println(date);
            date = date.plusDays(1);
        }

    }
andrucz
  • 1,971
  • 2
  • 18
  • 30
0

This is a working example:

https://github.com/OpenGamma/OG-Platform/blob/master/projects/OG-Util/src/main/java/com/opengamma/util/time/LocalDateRange.java#L113

  public static LocalDateRange of(LocalDate startDateInclusive, LocalDate endDate, boolean endDateInclusive) {
    ArgumentChecker.notNull(startDateInclusive, "startDate");
    ArgumentChecker.notNull(endDate, "endDate");
    if (endDateInclusive == false && endDate.isBefore(LocalDate.MAX)) {
      endDate = endDate.minusDays(1);
    }
    if (endDate.isBefore(startDateInclusive)) {
      throw new IllegalArgumentException("Start date must be on or after end date");
    }
    return new LocalDateRange(startDateInclusive, endDate);
  }
Dongsun
  • 134
  • 2
0

How about this example?:

@JRubyMethod(name = {"asctime", "ctime"})
    public RubyString asctime() {
        DateTimeFormatter simpleDateFormat;

        if (dt.getDayOfMonth() < 10) {
            simpleDateFormat = ONE_DAY_CTIME_FORMATTER;
        } else {
            simpleDateFormat = TWO_DAY_CTIME_FORMATTER;
        }
        String result = simpleDateFormat.print(dt);
        return getRuntime().newString(result);
    }

Full source here: http://code.openhub.net/file?fid=VJqxO5av-KgtoQFAp9juIzamnTc&cid=KOJoiAJBzj4&s=Increment%20and%20print%20all%20days%20in%20a%20month&pp=0&fl=Java&ff=1&filterChecked=true&fp=144796&mp,=1&ml=0&me=1&md=1&projSelected=true#L0

unilux
  • 21
  • 1