2012-03-14 15 views
96

Ho bisogno di ottenere il primo appuntamento (come org.joda.time.LocalDate) di un mese e l'ultimo. Ottenere il primo è banale, ma ottenere l'ultimo sembra aver bisogno di una logica in quanto i mesi hanno una lunghezza diversa e la lunghezza di febbraio varia anche nel corso degli anni. Esiste già un meccanismo per questo già integrato in JodaTime o devo implementarlo da solo?Come ottenere l'ultima data di un determinato mese con JodaTime?

+1

Solo un heads-up, questo funziona anche con i tipi di 'DateTime' :) – vikingsteve

risposta

186

ne dite:

LocalDate endOfMonth = date.dayOfMonth().withMaximumValue(); 

dayOfMonth() restituisce un LocalDate.Property che rappresenta il "giorno del mese" campo in un modo che conosce l'origine LocalDate.

Come accade, il metodo withMaximumValue() è ancora documented a consigliarlo per questo compito particolare:

Questa operazione è utile per ottenere un LocalDate l'ultimo giorno del mese, come durata dei mesi vari.

LocalDate lastDayOfMonth = dt.dayOfMonth().withMaximumValue(); 
+0

@Jon Skeet Come ottenere questo utilizzando la nuova API Date e Time di Java 8? –

+4

@ WarrenM.Nocos: userei 'dt.with (TemporalAdjusters.lastDayOfMonth())' –

0

una vecchia questione, ma un risultato superiore del Google quando ero alla ricerca di questo.

Se qualcuno ha bisogno l'ultimo giorno stesso come un int utilizzando invece JodaTime si può fare questo:

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; 
} 
-1

Utilizzando JodaTime, siamo in grado di fare questo:

 

    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() { 
     return new 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); 
    }

come descritto qui nel mio piccolo gist on github: A JodaTime and java.util.Date Util Class with a lot of usefull functions.

5

Un altro semplice metodo è questo:

//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); 
+0

cosa succede se il tuo mese è = 12? – jon

Problemi correlati