我如何获得31天前的日期? [英] How do I get the date of 31 days ago?

查看:83
本文介绍了我如何获得31天前的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何获取x,该时间应该早于current_date 31天?

How can I get x which should be 31 days before current_date?

x(date)___________________________current_date
                       31 

推荐答案

只需减去31天.例如:

Just subtract 31 days. For example:

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

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

To get the current date, you could use:

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

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

或者您可能想要创建自己的时钟"表示形式,以便为您提供当前的Instant,在这种情况下,您将使用:

Or you may want to create your own "clock" representation which is able to give you the current Instant, in which case you'd use:

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

(这使您可以使用依赖注入来使用伪造的时钟编写更简单的单元测试.)

(That lets you use dependency injection to write simpler unit tests with a fake clock.)

这篇关于我如何获得31天前的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆