我怎样才能得到31天前的日期?

Nod*_*bek 6 java jodatime

我怎么能x在31天之前得到哪一个current_date

x(date)___________________________current_date
                       31 
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 11

只需减去31天.例如:

LocalDate current = new LocalDate(2015, 6, 19);
LocalDate x = current.minusDays(31); // 2015-05-19
Run Code Online (Sandbox Code Playgroud)

获取当前日期,您可以使用:

LocalDate current = new LocalDate(); // Default time zone
Run Code Online (Sandbox Code Playgroud)

要么

LocalDate current = new LocalDate(zone); // Some specific zone
Run Code Online (Sandbox Code Playgroud)

或者你可能想要创建自己的"时钟"表示,它能够给你当前的Instant,在这种情况下你可以使用:

LocalDate current = clock.getCurrentInstant().toDateTime(zone).toLocalDate();
Run Code Online (Sandbox Code Playgroud)

(这允许您使用依赖注入来使用假时钟编写更简单的单元测试.)


Kri*_*ran 5

你可以试试这个:

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);
Run Code Online (Sandbox Code Playgroud)

否则你可以尝试:

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);
Run Code Online (Sandbox Code Playgroud)