然后,我们调用lengthOfMonth()方法来获取该月份的天数。 接下来,我们可以编写一个简单的示例程序来测试这个方法: publicclassMain{publicstaticvoidmain(String[]args){intyear=2022;intmonth=6;intdaysInMonth=getDaysInMonth(year,month);System.out.println("Days in month: "+daysInMonth);}publicstaticintgetDaysInM...
还可以用这个类得到 当月共有多少天,YearMonth 实例的 lengthOfMonth() 方法可以返回当月的天数,在判断 2 月有 28 天还是 29 天时非常有用。 1YearMonth currentYearMonth = YearMonth.now(); 2System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth()); ...
1YearMonth currentYearMonth = YearMonth.now();2System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());3YearMonth creditCardExpiry = YearMonth.of(2018, Month.FEBRUARY);4System.out.printf("Your credit card expires on %s %n", creditCardExpiry)...
YearMonthcurrentYearMonth=YearMonth.now(); System.out.printf("Days in month year %s: %d%n",currentYearMonth,currentYearMonth.lengthOfMonth()); YearMonthcreditCardExpiry=YearMonth.of(2019,Month.FEBRUARY); System.out.printf("Your credit card expires on %s %n",creditCardExpiry); } } D...
首先,需要创建两个LocalDate对象表示两个日期。然后,可以使用ChronoUnit.DAYS.between()方法计算这两个日期之间的天数。具体代码如下: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassMain{publicstaticvoidmain(String[]args){LocalDate dat...
nextInt(); System.out.println("请输入月"); month = scanner.nextInt(); System.out.println(year + "年" + month + "月有" + days(year, month) + "天"); } public static int days(int year, int month) { int days = 0; if (month != 2) { sw...
(period.getYears());// 输出:11, 因为period值为11月27天,即month字段为11System.out.println(period.getMonths());// 输出:27, 因为period值为11月27天,即days字段为27System.out.println(period.getDays());// 输出:P14M27D, 因为period为11月27天,加上3月,变成14月27天System.out.println(period...
package lianxi;import java.util.Scanner;public class Lianxi {/param args/public static void main(String[] args) {// TODO Auto-generated method stubint year,month,day;int days = 0;int d = 0;int e;Scanner sca = new Scanner(System.in);do{e=0;System.out.println("输入年:")...
1.Month 1.1 部分源码: *@implSpec*Thisisanimmutableandthread-safeenum.**@since1.8*/publicenumMonthimplementsTemporalAccessor,TemporalAdjuster{/*** The singleton instance for the month of January with 31 days.* This has the numeric value of {@code 1}.*/JANUARY,/*** The singleton instance for...
LocalDatedate=LocalDate.now();//获取年intyear=date.get(ChronoField.YEAR);//获取 月份intmonth=date.get(ChronoField.MONTH_OF_YEAR);//获取 几号intdayOfMonth=date.get(ChronoField.DAY_OF_MONTH);//获取 今天是今年第几天intdayOfYear=date.get(ChronoField.DAY_OF_YEAR);//获取 星期几intdayOfWeek=dat...