如何在Java 8中使用LocalDateTime查找当月的总天数? [英] How to find the total number of days in the month using LocalDateTime in Java 8?

查看:6850
本文介绍了如何在Java 8中使用LocalDateTime查找当月的总天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中使用java 8日期和时间类。我可以找到年,月,日,小时,分钟和秒,但我找不到一种方法可以找到一个月内的天数。



这是我的代码。

I am using java 8 date and time classes in my code. I can find year, month, day, hours, minutes and seconds, but I cannot find a method to find a number of days in a month.

Here is my code.

String oldestDateString = "2015-10-01 00:00:00";
String latestDateString = "2015-12-25 00:00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime oldestDate = LocalDateTime.parse(oldestDateString, formatter);
LocalDateTime latestDate = LocalDateTime.parse(latestDateString, formatter);
   
int year = oldestDate.getYear();
int month = oldestDate.getMonthValue();
int day = oldestDate.getDayOfMonth();
int hours = oldestDate.getHour();
int minutes = oldestDate.getMinute();
int seconds = oldestDate.getSecond();



我还要遍历oldestDate tolatestDate。如何迭代oldestDate到latestDate直到oldestDate达到latestDate?


I also want to iterate through "oldestDate" to "latestDate". How can I iterate through "oldestDate" to "latestDate" until the "oldestDate" reaches to "latestDate"?

推荐答案

你可以找到一个月的天数通过为下个月的第一天创建日期时间对象,并减去1天。然后由类对象调整到上个月的最后一天。



我不确定你是什么意思我怎么能通过oldestDate迭代到latestDate直到oldestDate到达latestDate 的。您在迭代期间想要实现的目标是什么?
You can find the number of days in a month by creating a datetime object for the first day of the following month, and subtracting 1 day. That will then be adjusted by the class object to the last day of the previous month.

I'm not sure what you mean by How can I iterate through "oldestDate" to "latestDate" until the "oldestDate" reaches to "latestDate". What exactly are you trying to achieve during the iteration?


这篇关于如何在Java 8中使用LocalDateTime查找当月的总天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆