수색…


한 달의 마지막 날을 쉽게 찾을 수 있습니다.

매월 마지막 날을 찾으려면 복잡한 DateTime 체조를하거나 다음 방법을 사용할 수 있습니다.

2021 년 2 월의 마지막 날을 찾고 싶습니다. 다음을 수행하십시오.

Integer month = 2;
Integer day = null;
Integer year = 2021;

// Create a new DateTime object for the first day of the month following
// the date you're looking for.
DateTime dtTarget = DateTime.newInstance(year, month, 1);
//In this case we would be sure that month would not be out of bound
dtTarget = dtTarget.addMonths(1);
// Then use the .addDays() method to add negative 1 days. This gives you
// the last day of the target month.
DateTime lastDayOfMonth = dtTarget.addDays(-1);
day = lastDayOfMonth.day();

System.debug(lastDayOfMonth);
System.debug(day);

그러면 로그에 다음 출력이 생성됩니다.

18:19:57:005 USER_DEBUG [15]|DEBUG|2021-02-28 08:00:00
18:21:10:003 USER_DEBUG [16]|DEBUG|28

이것은 모든 add 메소드에 적용되어 과거의 DateTimes를 쉽고 빠르게 찾을 수 있습니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow