Reputation: 41
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());
}
}
Upvotes: 3
Views: 2347
Reputation: 339342
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.
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.
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 ) ;
}
Upvotes: 2
Reputation: 2021
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);
}
}
Upvotes: 1
Reputation: 51553
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);
}
}
}
Upvotes: 1
Reputation: 21
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);
}
Upvotes: 0
Reputation: 14348
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
}
}
Upvotes: 3
Reputation: 134
This is a working example:
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);
}
Upvotes: 0