Java 关于临时调整器的使用。lastDayOfMonth()

Java 关于临时调整器的使用。lastDayOfMonth(),java,localdate,java-time,dayofmonth,Java,Localdate,Java Time,Dayofmonth,以下是我的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"%> <%@ pag

以下是我的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>
我预计“月移日期”应该是2018年12月31日,但它返回2019年12月31日

根据Javadoc:

 TemporalAdjusters.lastDayOfMonth() 
返回“月的最后一天”调整器,该调整器返回设置为当前月份最后一天的新日期

我今天运行jsp,为什么问题只发生在“十二月”localdate对象中,而不是发生在十一月

示例

2008年12月29日星期一写为“2009-W01-1”
2010年1月3日星期日 写的是“2009-W53-7”


你写的

DateTimeFormatter.ofPattern("YYYY-MM-dd"))
使用模式
YYYY
,您将使用基于周的年份进行格式化。
将模式更改为
yyyy-MM-dd
,它使用纪年


如果您确定始终需要
2018-12-31
格式(ISO 8601格式),则无需指定任何格式化程序
monthshiftstartdate.toString()
将为您提供所需的内容(对于其他
LocalDate
变量也是如此)。
DateTimeFormatter.ofPattern("YYYY-MM-dd"))
DateTimeFormatter.ofPattern("yyyy-MM-dd"))