• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to do this in java ?

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
var instant = DateTime.Parse('”2000-01-01”);

using (SystemTime.Is(instant))

{

// do some work

var currentTime = SystemTime.UtcNow;

}

More details here: http://jonathan-oliver.blogspot.com/2009/10/testing-timestatic-calls-and.html

Thanks.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will get better attention if we know what the question is about.

Do you mean to get the time with methods like System#currentTimeMillis()?
Or would you want a Calendar object? Or one of the DateFormat classes (possibly SimpleDateFormat)?
 
Stephane Clinckart
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You will get better attention if we know what the question is about.

Do you mean to get the time with methods like System#currentTimeMillis()?
Or would you want a Calendar object? Or one of the DateFormat classes (possibly SimpleDateFormat)?



Did you read what this lib is doing ?
My goal is to achieve the same in terms of functionnality in java.

The code I spot do following:

In the Do block... the systemTime is set to 2000/01/01

When the block is closed, the SystemTime is "restored".

Another sample of the same idea:
http://melgrubb.wordpress.com/2008/12/31/use-datetimecontext-to-put-your-code-in-a-time-warp/

Thanks for help.

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. I don't think you can use the System.setProperty() method since there doesn't seem to be a property corresponding to current time. Sorry I can't help any more.
 
Stephane Clinckart
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't know. I don't think you can use the System.setProperty() method since there doesn't seem to be a property corresponding to current time. Sorry I can't help any more.



Hi,

thanks anyway or help.

The problem with this method is how to "restore" the normal date at the end ?

Another point I would like to achieve... if it is possible... is to have different time in my Threads.

Thread 1 --> 2000/01/01
Thread 2 --> Current Time
Thread 3 --> Time in the future
...

Very usefull for testing an application.

But... this would be the sherry on top of my cake ;-)
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use another variable to store the old value, then restore that old value at the end. Or simply use the new variable instead of the old one.
Note that you may need to copy (or clone()) the old value if you intend to modify it. E.g.
 
Stephane Clinckart
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Use another variable to store the old value, then restore that old value at the end. Or simply use the new variable instead of the old one.
Note that you may need to copy (or clone()) the old value if you intend to modify it. E.g.



The idea is to change the "systemTime" inside the application... or better in the block.

The target is to have the ability to test a lib, a class, ... on a specified time.

By exemple on 2008/31/12 --> This date was special if I well remember... day 366 of the year what is not common.

I didn't find a way to do this in Java.
It looks like C# is able to do stuff that is not possible in Java... at least for this specific problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic