打印日期范围从1910年1月1日到2025年12月30日

jyztefdp  于 2021-07-14  发布在  Java
关注(0)|答案(2)|浏览(480)
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Iterator;
  4. public class Details
  5. {
  6. public static void main(String [] args)
  7. {
  8. HashMap<Integer, String> hmap = new HashMap<Integer, String>();
  9. //Adding elements to HashMap
  10. hmap.put(1, "January");
  11. hmap.put(2, "January");
  12. hmap.put(3, "January");
  13. hmap.put(4, "January");
  14. hmap.put(5, "January");
  15. hmap.put(6, "January");
  16. hmap.put(7, "January");
  17. hmap.put(8, "January");
  18. hmap.put(9, "January");
  19. hmap.put(10, "January");
  20. //FOR LOOP
  21. System.out.println("For Loop:");
  22. for (Map.Entry me : hmap.entrySet()) {
  23. System.out.println("Key: "+ me.getKey() + " & Value: " + me.getValue());
  24. }
  25. //WHILE LOOP & ITERATOR
  26. System.out.println("While Loop:");
  27. Iterator iterator = hmap.entrySet().iterator();
  28. while (iterator.hasNext()) {
  29. Map.Entry me2 = (Map.Entry) iterator.next();
  30. System.out.println("Key: "+ me2.getKey() + " & Value: " + me2.getValue());
  31. }
  32. }
  33. }

我的输出:

  1. For Loop:
  2. Key: 1 & Value: January
  3. Key: 2 & Value: January
  4. Key: 3 & Value: January
  5. Key: 4 & Value: January
  6. Key: 5 & Value: January
  7. Key: 6 & Value: January
  8. Key: 7 & Value: January
  9. Key: 8 & Value: January
  10. Key: 9 & Value: January
  11. Key: 10 & Value: January
  12. While Loop:
  13. Key: 1 & Value: January
  14. Key: 2 & Value: January
  15. Key: 3 & Value: January
  16. Key: 4 & Value: January
  17. Key: 5 & Value: January
  18. Key: 6 & Value: January
  19. Key: 7 & Value: January
  20. Key: 8 & Value: January
  21. Key: 9 & Value: January
  22. Key: 10 & Value: January

我想要这个输出:

  1. 1: January 1, 1910
  2. 2: January 2, 1910
  3. 3: January 3, 1910
  4. 4: January 4, 1910
  5. 5: January 5, 1910.....
  6. up to
  7. 41758: December 28, 2025
  8. 41759: December 29, 2025
  9. 41760: December 30, 2025

谢谢你的帮助

uxh89sit

uxh89sit1#

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

  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.temporal.ChronoUnit;
  4. LocalDate start = LocalDate.of(1910,1,1);
  5. LocalDate end = LocalDate.of(2025,12,31);
  6. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMMM d, yyyy");
  7. int key = 1;
  8. while (start.isBefore(end)) {
  9. System.out.println(key++ + " : " + start.format(fmt));
  10. start = start.plus(1, ChronoUnit.DAYS);
  11. }

像这样的指纹。

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

另一方面,如果不想使用任何导入的类来执行此操作,则可以按以下方式执行:
创建月份名称数组
创建每月最大天数(非闰年)的数组。
创建闰年方法(稍后解释)。

  1. String[] monthNames = { "January", "February", "March",
  2. "April", "May", "June", "July", "August", "September",
  3. "October", "November", "December" };
  4. int[] monthDays =
  5. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  6. int day = 1;
  7. int month = 0; // months go from 0 to 11
  8. int year = 1910;
  9. int count = 0;

一个简单的while循环驱动程序来打印日期范围。

  1. while (!(year == 2025 && month == 11 && day == 31)) {
  2. count++;
  3. System.out.printf("%d : %s %d, %d%n", count,
  4. monthNames[month], day, year);
  5. if (day++ >= monthDays[month]) {
  6. // take care of February in a leap year
  7. if (month == 1 && day == 29 && isLeap(year)) {
  8. continue;
  9. }
  10. day = 1; // reset day if past end of month.
  11. month++;
  12. if (month == 12) {
  13. month = 0; // reset month and increment year
  14. year++;
  15. }
  16. }
  17. }

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

  1. public static boolean isLeap(int year) {
  2. return year % 100 != 0 && year % 4 == 0 || year % 400 == 0;
  3. }
展开查看全部
uyhoqukh

uyhoqukh2#

这是一种生成所请求的数据的方法,正确地(人们希望)计算闰年等。我将注意到,运行的最后一个数字与示例输出的数字不同。
如果需要将数据存储到某个数据数组中,请替换 output 方法。

  1. private static DateTimeFormatter FMTR = DateTimeFormatter.ofPattern("MMMM d, yyyy");
  2. public static LocalDate genNextDate(LocalDate inpDate)
  3. {
  4. return inpDate.plusDays(1);
  5. }
  6. public static void output(int cntr, LocalDate inpDate)
  7. {
  8. System.out.printf("%6d: %s%n", cntr, FMTR.format(inpDate));
  9. }
  10. public static void doIt()
  11. {
  12. final LocalDate endDate = LocalDate.of(2025, 12, 31);
  13. int cntr = 1;
  14. LocalDate date = LocalDate.of(1910, 1, 1);
  15. while (date.isBefore(endDate)) {
  16. output(cntr, date);
  17. date = genNextDate(date);
  18. cntr++;
  19. }
  20. }
  21. public static void main(String[] args)
  22. {
  23. doIt();
  24. }

示例输出

  1. 1: January 1, 1910
  2. 2: January 2, 1910
  3. 3: January 3, 1910
  4. 4: January 4, 1910
  5. 5: January 5, 1910
  6. 6: January 6, 1910
  7. 7: January 7, 1910
  8. 8: January 8, 1910
  9. 9: January 9, 1910
  10. ...
  11. 42364: December 26, 2025
  12. 42365: December 27, 2025
  13. 42366: December 28, 2025
  14. 42367: December 29, 2025
  15. 42368: December 30, 2025
展开查看全部

相关问题