增加并打印一个月内的所有日子

时间:2016-03-07 16:18:19

标签: java

我已经开始编写一些代码,并且需要在一个月内打印所有日期,我可以通过每天添加一个来做到这一点,但必须有一个我错过的更短的方式。到目前为止,这是我的代码。

我知道这不是最漂亮的,我想知道如何打印日期,而它在1月份的每一天都会增加,而不必每次都添加1次,然后每次都打印出来。

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

}

}

5 个答案:

答案 0 :(得分:1)

使用Java 8本地日期的简单例子,如问题中的输入

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
    }
}   

答案 1 :(得分:1)

这是一个打印月内所有日期的简单示例。时间部分已设置为零。

在这里,我们在2016年3月的所有日子都打印出来。

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

此代码适用于Java 6,Java 7和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);
        }

    }

}

答案 2 :(得分: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);
        }

    }

答案 3 :(得分:0)

这是一个有效的例子:

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

答案 4 :(得分:0)

这个例子怎么样?:

@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);
    }

完整来源: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