Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment and print all days in a month [duplicate]

Tags:

java

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());

}

}
like image 213
Andrew Avatar asked Dec 13 '25 15:12

Andrew


2 Answers

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
    }
}   
like image 116
Sharon Ben Asher Avatar answered Dec 16 '25 05:12

Sharon Ben Asher


tl;dr

Use the modern java.time classes, never Calendar.

YearMonth currentMonth = YearMonth.now( ZoneId.of( "America/Edmonton" ) ) ;  // Determine the current month as seen in a particular time zone.
currentMonth
    .atDay( 1 )                                             // First of this month.
    .datesUntil( currentMonth.plusMonths( 1 ).atDay( 1 ) )  // Until first of next month.
    .forEach( System.out :: println ) ;                     // Print each of the dates in that range.

Avoid legacy date-time classes

Never use Calendar class. That class is part of the terribly flawed legacy date-time classes dating back to the earliest days of Java. These classes were years ago supplanted by the modern java.time classes built into Java 8+, defined in JSR 310.

java.time

Represent a month with YearMonth class.

Determining the current month requires a time zone. For any given moment, the date varies around the globe by time zone. Around the end/start of the month, as the date varies so does the month.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;
YearMonth currentMonth = YearMonth.now( z ) ;

Represent a date with LocalDate class.

LocalDate firstOfMonth = currentMonth.atDay( 1 ) ;

Get a stream of LocalDate objects for all the days between two dates.

LocalDate firstOfNextMonth = currentMonth.plusMonths( 1 ).atDay( 1 ) ;
Stream < LocalDate > datesStream = firstOfMonth.datesUntil( firstOfNextMonth ) ;

For each of those dates, generate a print text in standard ISO 8601 format.

datesStream.forEach( System.out :: println ) ;
2024-08-01
2024-08-02
…

If you are not comfortable with streams, make a List of LocalDate objects from that datesUntil stream.

List < LocalDate > datesList = datesStream.toList() ;
for ( LocalDate localDate : datesList ) 
{
    System.out.println( localDate ) ;
}
like image 32
Basil Bourque Avatar answered Dec 16 '25 06:12

Basil Bourque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!