关于TemporalAdjusters.lastDayOfMonth()的用法
About the usage of TemporalAdjusters.lastDayOfMonth()
这是我的 jsp 页面源代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.time.format.DateTimeFormatter"%>
<%@ page import="java.time.temporal.TemporalAdjusters"%>
<%@ page import="java.time.LocalDate"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Local Date Demo</title>
</head>
<body>
<%
int year=2018,month=12,date=1;
LocalDate theMonthShiftStartDate=LocalDate.of(year,month,date);
LocalDate theMonthShiftEndDate=theMonthShiftStartDate.with(TemporalAdjusters.lastDayOfMonth());
LocalDate previousMonthShiftStartDate=theMonthShiftStartDate.plusMonths(-1);
LocalDate previousMonthShiftEndDate=previousMonthShiftStartDate.with(TemporalAdjusters.lastDayOfMonth());
%>
theMonthShiftStartDate=<%=theMonthShiftStartDate.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))%><br>
theMonthShiftEndDate=<%=theMonthShiftEndDate.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))%><br>
previousMonthShiftStartDate=<%=previousMonthShiftStartDate.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))%><br>
previousMonthShiftEndDate=<%=previousMonthShiftEndDate.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))%><br>
</body>
</html>
为什么输出如下:
theMonthShiftStartDate=2018-12-01
theMonthShiftEndDate=2019-12-31
previousMonthShiftStartDate=2018-11-01
previousMonthShiftEndDate=2018-11-30
我预计 "theMonthShiftEndDate" 应该是 2018-12-31,但是 return 2019-12-31。
根据 Javadoc:
TemporalAdjusters.lastDayOfMonth()
Returns "last day of month" 调节器,return将新日期设置为当月的最后一天。
我运行今天jsp,为什么问题只发生在"december" localdate对象,11月份没有发生?
参见 week-dates 上的维基百科。
Examples:
Monday 29 December 2008 is written "2009-W01-1"
Sunday 3 January 2010
is written "2009-W53-7"
你写了
DateTimeFormatter.ofPattern("YYYY-MM-dd"))
使用模式 YYYY
,您正在使用 week-based-year.
进行格式化
将您的模式更改为 yyyy-MM-dd
,它使用 year-of-era
DateTimeFormatter.ofPattern("yyyy-MM-dd"))
这是我的 jsp 页面源代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.time.format.DateTimeFormatter"%>
<%@ page import="java.time.temporal.TemporalAdjusters"%>
<%@ page import="java.time.LocalDate"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Local Date Demo</title>
</head>
<body>
<%
int year=2018,month=12,date=1;
LocalDate theMonthShiftStartDate=LocalDate.of(year,month,date);
LocalDate theMonthShiftEndDate=theMonthShiftStartDate.with(TemporalAdjusters.lastDayOfMonth());
LocalDate previousMonthShiftStartDate=theMonthShiftStartDate.plusMonths(-1);
LocalDate previousMonthShiftEndDate=previousMonthShiftStartDate.with(TemporalAdjusters.lastDayOfMonth());
%>
theMonthShiftStartDate=<%=theMonthShiftStartDate.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))%><br>
theMonthShiftEndDate=<%=theMonthShiftEndDate.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))%><br>
previousMonthShiftStartDate=<%=previousMonthShiftStartDate.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))%><br>
previousMonthShiftEndDate=<%=previousMonthShiftEndDate.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))%><br>
</body>
</html>
为什么输出如下:
theMonthShiftStartDate=2018-12-01
theMonthShiftEndDate=2019-12-31
previousMonthShiftStartDate=2018-11-01
previousMonthShiftEndDate=2018-11-30
我预计 "theMonthShiftEndDate" 应该是 2018-12-31,但是 return 2019-12-31。
根据 Javadoc:
TemporalAdjusters.lastDayOfMonth()
Returns "last day of month" 调节器,return将新日期设置为当月的最后一天。
我运行今天jsp,为什么问题只发生在"december" localdate对象,11月份没有发生?
参见 week-dates 上的维基百科。
Examples:
Monday 29 December 2008 is written "2009-W01-1"
Sunday 3 January 2010 is written "2009-W53-7"
你写了
DateTimeFormatter.ofPattern("YYYY-MM-dd"))
使用模式 YYYY
,您正在使用 week-based-year.
进行格式化
将您的模式更改为 yyyy-MM-dd
,它使用 year-of-era
DateTimeFormatter.ofPattern("yyyy-MM-dd"))