2015-06-19 13 views

Odpowiedz

7

Po prostu odejmij 31 dni. Na przykład:

LocalDate current = new LocalDate(2015, 6, 19); 
LocalDate x = current.minusDays(31); // 2015-05-19 

Do dostać aktualną datę, można użyć:

LocalDate current = new LocalDate(); // Default time zone 

lub

LocalDate current = new LocalDate(zone); // Some specific zone 

A może chcesz stworzyć swój własny "Zegar" reprezentację, która jest w stanie podać aktualny numer Instant, w którym to przypadku użyłbyś:

LocalDate current = clock.getCurrentInstant().toDateTime(zone).toLocalDate(); 

(który pozwala używać Dependency Injection napisać prostszych testów jednostkowych z fałszywą zegara.)

+0

Dziękuję Jon Skeet! –

3

Można spróbować to:

LocalDate current = new LocalDate();//Constructs an instance set to the current local time evaluated using ISO chronology in the default zone. 
LocalDate x = current.minusDays(31); 

Albo inaczej można spróbować:

LocalDate current = LocalDate.now();//Obtains a LocalDate set to the current system millisecond time using ISOChronology in the default time zone 
LocalDate x = current.minusDays(31); 
+0

Dziękuję Krishanthy Mohanachandran! –

1

Możesz użyć JODA API, jeśli chcesz, jego zaawansowanie i użyteczne funkcje:

String DATE_PATTERN = "dd/MM/yyyy"; 
DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_PATTERN); 
String systemDate = formatter.print(DateTime.now()); 
System.out.println("Current Date : " + systemDate); 
String newDate = formatter.print(DateTime.now().minusDays(31)); 
System.out.println("Date 31 days ago : " + newDate); 

wyjściowa: Aktualna Data: 19/06/2015

Data 31 dni temu: 19/05/2015

Powiązane problemy