【问题标题】:print out date range from January 1 1910 to December 30 2025打印日期范围从 1910 年 1 月 1 日到 2025 年 12 月 30 日
【发布时间】:2021-07-13 22:40:52
【问题描述】:
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
public class Details
{
    public static void main(String [] args)
    {
        HashMap<Integer, String> hmap = new HashMap<Integer, String>();
        //Adding elements to HashMap
        hmap.put(1, "January");
        hmap.put(2, "January");
        hmap.put(3, "January");
        hmap.put(4, "January");
        hmap.put(5, "January");
        hmap.put(6, "January");
        hmap.put(7, "January");
        hmap.put(8, "January");
        hmap.put(9, "January");
        hmap.put(10, "January");
        //FOR LOOP
        System.out.println("For Loop:");
        for (Map.Entry me : hmap.entrySet()) {
          System.out.println("Key: "+ me.getKey() + " & Value: " + me.getValue());
        }

        //WHILE LOOP & ITERATOR
        System.out.println("While Loop:");
        Iterator iterator = hmap.entrySet().iterator();
        while (iterator.hasNext()) {
             Map.Entry me2 = (Map.Entry) iterator.next();
          System.out.println("Key: "+ me2.getKey() + " & Value: " + me2.getValue());
        } 
    }
}

我的输出:

For Loop:
Key: 1 & Value: January
Key: 2 & Value: January
Key: 3 & Value: January
Key: 4 & Value: January
Key: 5 & Value: January
Key: 6 & Value: January
Key: 7 & Value: January
Key: 8 & Value: January
Key: 9 & Value: January
Key: 10 & Value: January
While Loop:
Key: 1 & Value: January
Key: 2 & Value: January
Key: 3 & Value: January
Key: 4 & Value: January
Key: 5 & Value: January
Key: 6 & Value: January
Key: 7 & Value: January
Key: 8 & Value: January
Key: 9 & Value: January
Key: 10 & Value: January

我想要这个输出:

1: January 1, 1910
2: January 2, 1910
3: January 3, 1910
4: January 4, 1910
5: January 5, 1910.....

up to

41758: December 28, 2025
41759: December 29, 2025
41760: December 30, 2025

感谢您的帮助

【问题讨论】:

  • 您真的想在地图中存储数千个日期对象,然后将它们输出到控制台吗?或者您是否正在寻找一种方法来输出从特定开始日期到结束日期的所有日期?
  • 是的,只需从 1910 年 1 月 1 日开始日期到 2025 年 12 月 30 日打印出来。这是我坚持的学校作业,我尝试了很多其他方法,但未在此处发布。谢谢@厄立特里亚
  • 所以这意味着我们应该引导并指出正确的方向,并尽可能多地留给自己,以便您学习?尤其是避免给你完整的工作代码(也称为spoon-feeding)?
  • 我不确定您从哪里得到HashMap 会帮助您的想法。我怀疑它可以。

标签: java date for-loop hashmap calendar


【解决方案1】:

你可以这样做。查看java.time 包,了解更多有用的日期/时间类。

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;


LocalDate start = LocalDate.of(1910,1,1);
LocalDate end = LocalDate.of(2025,12,31);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMMM d, yyyy");
int key = 1;
while (start.isBefore(end)) {
    System.out.println(key++ + " : " + start.format(fmt));
    start = start.plus(1, ChronoUnit.DAYS);
}

打印这样的东西。

1 : January 1, 1910
2 : January 2, 1910
3 : January 3, 1910
4 : January 4, 1910
5 : January 5, 1910
6 : January 6, 1910
7 : January 7, 1910
8 : January 8, 1910
9 : January 9, 1910
10 : January 10, 1910
11 : January 11, 1910
12 : January 12, 1910
...
...

另一方面,如果您不想使用任何导入的类来执行此操作,则可以按以下方式进行:

  • 创建月份名称数组
  • 创建一个包含每月最大天数的数组(非闰年)。
  • 创建闰年方法(稍后解释)。
String[] monthNames = { "January", "February", "March",
        "April", "May", "June", "July", "August", "September",
        "October", "November", "December" };
int[] monthDays =
        { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = 1;
int month = 0; // months go from 0 to 11
int year = 1910;
int count = 0;

用于打印日期范围的简单 while 循环驱动程序。

while (!(year == 2025 && month == 11 && day == 31)) {
    count++;
    System.out.printf("%d : %s %d, %d%n", count,
            monthNames[month], day, year);

     if (day++ >= monthDays[month]) {
         // take care of February in a leap year
         if (month == 1 && day == 29 && isLeap(year)) {
             continue;
         }
         day = 1;        // reset day if past end of month.
         month++;
         if (month == 12) {
             month = 0;  // reset month and increment year
             year++;
         } 
     }
         
}

只有能被 4 整除的非世纪年和能被 400 整除的世纪年才是闰年。首先检查非世纪年份,因为它们发生得更频繁。

public static boolean isLeap(int year) {
    return year % 100 != 0 && year % 4 == 0 || year % 400 == 0;
}

【讨论】:

    【解决方案2】:

    这是一种生成请求数据的方法,正确地(希望)考虑闰年等。我会注意到运行的最后一个数字与示例输出不同。

    如果希望将数据存储到某个数据数组中,请根据需要替换 output 方法。

        private static DateTimeFormatter FMTR = DateTimeFormatter.ofPattern("MMMM d, yyyy");
        
        public static LocalDate genNextDate(LocalDate inpDate)
        {
            return inpDate.plusDays(1);
        }
        
        
        public static void output(int cntr, LocalDate inpDate)
        {
    
            System.out.printf("%6d: %s%n", cntr, FMTR.format(inpDate));
        }
        
        
        public static void doIt()
        {
            final LocalDate endDate = LocalDate.of(2025, 12, 31);
            int cntr = 1;
            LocalDate date = LocalDate.of(1910, 1, 1);
            
            while (date.isBefore(endDate)) {
                output(cntr, date);
                date = genNextDate(date);
                cntr++;
            }
        }
    
       public static void main(String[] args)
        {
            doIt();
        }
    

    示例输出

         1: January 1, 1910
         2: January 2, 1910
         3: January 3, 1910
         4: January 4, 1910
         5: January 5, 1910
         6: January 6, 1910
         7: January 7, 1910
         8: January 8, 1910
         9: January 9, 1910
         ...
     42364: December 26, 2025
     42365: December 27, 2025
     42366: December 28, 2025
     42367: December 29, 2025
     42368: December 30, 2025
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 2018-07-25
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多