且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java中更改值new Date()

更新时间:2021-07-07 08:25:44

java.util.Date API非常老.日历 Calendar API取代了该API,它又又旧又过时又不好用,反过来又被 java.time API所取代-看看 ju有多老日期是?

The java.util.Date API is very very old. It was replaced by the Calendar API which is also old and obsolete and bad, and that in turn was replaced by the java.time API - see how old j.u.Date is?

无论如何, j.u.Date 不允许您在此处使用所需的内容.它不支持自定义时钟.

At any rate, j.u.Date just does not allow what you need here. It has no support for custom clocks.

但是 java.time DOES .

您可以调用: Instant.now(),它与 new Date()的新API等效(juDate的名字被错误地命名.它不代表日期;它代表时间的瞬间).有一个变种调用: Instant.now(clock).这样您就可以选择一个自定义时钟.

You can call: Instant.now(), which is the new API equivalent of new Date() (j.u.Date is epically badly named. It does not represent dates at all; it represents instants in time). There is a variant call: Instant.now(clock). This lets you pick a custom clock.

安排代码以获取要使用的时钟实例(例如,通过依赖注入"),或使用一种设置方式,使用未设置的时钟,这意味着您默认为系统时钟( Clock.systemUTC()),但您可以实现自己的时钟实例,该时钟实例确实可以完全满足您的需要:让测试代码配置"时钟以返回测试所需的任何日期/时间,而无需弄乱计算机的时钟完全没有.

Arrange for the code to obtain the clock instance to use, for example via Dependency Injection, or have a way to set it, with an unset clock meaning you default to the system clock (Clock.systemUTC()), but you can implement your own clock instance, which can indeed do exactly what you need: Have test code 'configure' the clock to return whatever dates/times you need for the test, without requiring messing with your computer's clock at all.

因此,您的三步解决方案:

So, your 3-step solution:

  1. 消除所有旧API的用法: java.text.DateTimeFormat java.util.Date java.sql.Timestamp 代码>,所有这些都需要删除.使用 java.time.Instant java.time.LocalDateTime java.time.LocalDate java.time.ZonedDateTime 反之亦然(新API的类型更多,因为时间本质上比 juDate 认为的要复杂得多;这是旧API的众多缺陷之一.还要消除所有调用)以及 System.currentTimeMillis().任何从过时的API中获取当前时间"的内容都需要删除.

  1. Eliminate all usage of the old API: java.text.DateTimeFormat, java.util.Date, java.sql.Timestamp, these all need to go away. Use java.time.Instant, java.time.LocalDateTime, java.time.LocalDate, java.time.ZonedDateTime, etc instead (the new API has way more types, because time is inherently more complicated than j.u.Date thinks it is; it is one of the many flaws of that old API. Also eliminate ALL calls to System.currentTimeMillis() as well. Anything that gets 'the current time' in terms of obsolete API needs to go.

设置某种类型的依赖项注入解决方案(自己编写,或使用匕首,guice,spring等现成的解决方案),以便可以注入Clock的实例.使用 Instant.now(clock)表单获取当前"日期和时间,而不要使用无参数的 Instant.now().

Set up a dependency injection solution of some sort (write it yourself, or use off the shelf solutions like dagger, guice, spring, etc) so you can inject an instance of Clock. Use the Instant.now(clock) form to obtain the 'current' date and time, and never the args-less Instant.now().

实施时钟的自定义暗示,并使用它来设置代码中查询当前日期/时间"以注入此测试时钟"的所有各个点.

Implement a custom impl of clock and use this to set all the various points in your code that query 'current date/time' to inject this 'test clock'.

然后,瞧.测试valhalla.

Then, voila. Test valhalla.