如何在Java中更改值new Date() [英] How to change the value new Date() in java

查看:66
本文介绍了如何在Java中更改值new Date()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,测试人员测试应用程序时需要经常更改日期和时间.当前,我们必须使用系统命令date-s来更改系统时间,但这也会导致服务器上的其他应用程序也受到影响.我只想更改此应用程序的Date(),而不必想要更改应用程序本身,因为使用新日期的地方太多了

In my application, the date and time need to be changed frequently when the tester tests the application. Currently, we have to use the system command date-s to change the system time,But this will cause other applications on the server to be affected as well。I want to change the Date() only this application, and I don't want to change the application itself, because there are so many places where new Date is used

推荐答案

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 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.

这篇关于如何在Java中更改值new Date()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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