如何从年份和月份名称获取日期列表

时间:2017-02-16 07:28:53

标签: java date

如何从特定年份和月份获取(java.util.Date)日期列表

示例:我有一年如'2017'和月份名称如'February'我想获得二月或其他月份的日期列表。

,例如

2017-02-01,
2017-02-02,
2017-02-03,
2017-02-04,
2017-02-05,
2017-02-06,
2017-02-07
....
2017-02-28.

请帮我示例代码,谢谢

4 个答案:

答案 0 :(得分:0)

首先,您需要找到特定月份可以获得的天数,Calendar.getActualMaximum(int field)

这很容易
  

给定此Calendar的时间值,返回指定日历字段可能具有的最大值。例如,MONTH字段的实际最大值在某些年份为12,在希伯来日历系统中为其他年份的13。

从这里开始,只需使用一个循环来创建每个值(或者使用Stream API可能更容易,但我对它不好......)。

这是一个使用这种方法的简单例子(根本不是答案):

Calendar c = Calendar.getInstance();
    for(int i = 0; i <= c.getActualMaximum(Calendar.MONTH); ++i){
        c.set(Calendar.MONTH, i);
        System.out.format("There is %d days in %d\n", c.getActualMaximum(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH));
    }

输出:

There is 31 days in 0
There is 28 days in 1
There is 31 days in 2
There is 30 days in 3
There is 31 days in 4
There is 30 days in 5
There is 31 days in 6
There is 31 days in 7
There is 30 days in 8
There is 31 days in 9
There is 30 days in 10
There is 31 days in 11

答案 1 :(得分:0)

您可以使用:

  • 首先,您应该从输入年份和月份2017/February
  • 的第一天开始
  • 第二,你应该得到本月的天数
  • 第三,你应该循环到你的日期中的数字,从1到28,并在你的日期添加一天

public static void main(String[] args) throws ParseException { int year = 2017; String month = "February"; SimpleDateFormat format = new SimpleDateFormat("yyyy/MMMM", Locale.US); Date utilDate = format.parse(year + "/" + month); //get first day of your month System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(utilDate)); //get days of months Calendar cal = Calendar.getInstance(); cal.setTime(utilDate); int monthMaxDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //loop and add a day to your date for (int i = 0; i < monthMaxDays - 1; i++) { cal.add(Calendar.DATE, 1); System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(cal.getTime())); } }

请参阅此code run live at IdeOne.com

祝你好运

答案 2 :(得分:0)

java.time

使用java.time包中的现代日期时间类。

String input = "2017 February" ;

解析为YearMonth个对象。定义格式模式以匹配您的输入。

    String input = "2017 February";
    DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuu MMMM" , Locale.US );
    YearMonth ym = YearMonth.parse ( input , f );

循环该月的天数。对于每个日期,请获取LocalDate个对象。

System.out.println ( "===== Days of " + ym + " =====" );
for ( int i = 1 ; i <= ym.lengthOfMonth () ; i ++ ) {
    LocalDate localDate = ym.atDay ( i );
    System.out.println ( localDate );  // Uses standard ISO 8601 format by default when generating a string.
}
System.out.println ( "=================" );
  

=====天数=====

     

2017年2月1日

     

2017年2月2日

     

2017年2月3日   ...

您可以看到code run live at IdeOne.com

如果您想查看使用Java Streams编写的此类代码,请参阅我的问题:Use Java streams to collect objects generated in a for loop

关于java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和&amp; SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore

答案 3 :(得分:-1)

我使用JODA数据时间让我的生活更轻松,这就是解决方案:

package com.github.dibyaranjan.service;

import org.joda.time.DateTime;

public class Test {
    public static void main(String[] args) {
        DateTime dateTime = new DateTime();
        dateTime = dateTime.withDate(2017, 02, 01); // Used the date and time mentioned in your question
        int currentMonth = dateTime.getMonthOfYear();
        boolean isCurrentMonth = true;
        while (isCurrentMonth) {
            isCurrentMonth = (dateTime.getMonthOfYear() == currentMonth);
            if (isCurrentMonth) {
                String dateTimeFormatter = "yyyy-MM-dd";
                System.out.println(dateTime.toString(dateTimeFormatter));
            }
            dateTime = dateTime.plusDays(1);
        }
    }
}
相关问题