Is it possible to change the current time of a stream or jvm instance in java?

I want to test some methods of the class that contain logic depending on the current time of its launch. I would like to set the / jvm / system stream time to a specific future date in JUnit setup and run my tests. Also I would like it to be temporary. Has anyone done this before?

I was thinking of something like TimeZone.setDefault (timezone)

+5
source share
4 answers

You can use AspectJ and roundabout methods java.lang.System.currentTimeMillis(), java.util.Date.new()and java.util.Calendar.getInstance(), which adjust the time to suit your needs.

+4
source

, java.util.Date.

joda-time, . . this

+2

. , , TimeHelper, : Date factory, DateFormatters .., .

Easymock , , .

+2

. long, - , TimeZone. , "" TimeZone, , , ; long, , TimeZone, - , .

, "" , . , TimeZone , , , .

, - . , !

.

0

All Articles