반응형
    
    
    
  JodaTime으로 특정 달의 마지막 날짜를 얻는 방법은 무엇입니까?
org.joda.time.LocalDate한 달 의 첫 번째 날짜 와 마지막 날짜를 가져와야합니다 . 첫 번째를 얻는 것은 사소한 일이지만 마지막을 얻는 것은 달의 길이가 다르고 2 월의 길이는 해에 따라 다르기 때문에 약간의 논리가 필요한 것 같습니다. JodaTime에 이미 내장 된 메커니즘이 있습니까 아니면 직접 구현해야합니까?
어때 :
LocalDate endOfMonth = date.dayOfMonth().withMaximumValue();
dayOfMonth()LocalDate.Property원래를 알고있는 방식으로 "일"필드를 나타내는를 반환합니다 LocalDate.
이 withMaximumValue()방법은 이 특정 작업에 권장하도록 문서화 되어 있습니다.
이 작업은 월 길이가 다양하므로 해당 월의 마지막 날에 LocalDate를 얻는 데 유용합니다.
LocalDate lastDayOfMonth = dt.dayOfMonth().withMaximumValue();
또 다른 간단한 방법은 다음과 같습니다.
//Set the Date in First of the next Month:
answer = new DateTime(year,month+1,1,0,0,0);
//Now take away one day and now you have the last day in the month correctly
answer = answer.minusDays(1);
오래된 질문이지만 이것을 찾고 있었을 때 최고의 Google 결과입니다.
누군가가 intJodaTime을 사용 하는 대신 실제 마지막 날이 필요한 경우 다음을 수행 할 수 있습니다.
public static final int JANUARY = 1;
public static final int DECEMBER = 12;
public static final int FIRST_OF_THE_MONTH = 1;
public final int getLastDayOfMonth(final int month, final int year) {
    int lastDay = 0;
    if ((month >= JANUARY) && (month <= DECEMBER)) {
        LocalDate aDate = new LocalDate(year, month, FIRST_OF_THE_MONTH);
        lastDay = aDate.dayOfMonth().getMaximumValue();
    }
    return lastDay;
}
JodaTime을 사용하여 다음을 수행 할 수 있습니다.
    public static final Integer CURRENT_YEAR = DateTime.now (). getYear ();
    public static final Integer CURRENT_MONTH = DateTime.now (). getMonthOfYear ();
    public static final Integer LAST_DAY_OF_CURRENT_MONTH = DateTime.now ()
            .dayOfMonth (). getMaximumValue ();
    public static final Integer LAST_HOUR_OF_CURRENT_DAY = DateTime.now ()
            .hourOfDay (). getMaximumValue ();
    public static final Integer LAST_MINUTE_OF_CURRENT_HOUR = DateTime.now (). minuteOfHour (). getMaximumValue ();
    public static final Integer LAST_SECOND_OF_CURRENT_MINUTE = DateTime.now (). secondOfMinute (). getMaximumValue ();
    public static DateTime getLastDateOfMonth () {
        새로운 DateTime (CURRENT_YEAR, CURRENT_MONTH,
                LAST_DAY_OF_CURRENT_MONTH, LAST_HOUR_OF_CURRENT_DAY,
                LAST_MINUTE_OF_CURRENT_HOUR, LAST_SECOND_OF_CURRENT_MINUTE);
    } 
github에 대한 내 작은 요점에서 설명하는 것처럼 유용한 기능이 많은 JodaTime 및 java.util.Date Util 클래스.
반응형
    
    
    
  'program story' 카테고리의 다른 글
| Android 에뮬레이터는 SQLite 데이터베이스를 어디에 저장합니까? (0) | 2020.08.09 | 
|---|---|
| base.base.method ()를 호출하는 방법? (0) | 2020.08.09 | 
| asp.net MVC 4 및 MVC 5에서 기본 컨트롤러를 설정하는 방법 (0) | 2020.08.09 | 
| 런타임 중에 방향을 잠그는 방법 (0) | 2020.08.08 | 
| 이미 컴파일 된 APK에 서명하는 방법 (0) | 2020.08.08 |