wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to do this in java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to do this in java ?" Watch "How to do this in java ?" New topic
Author

How to do this in java ?

Stephane Clinckart
Ranch Hand

Joined: Oct 21, 2003
Posts: 89
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 21, 2003
Posts: 89
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
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 21, 2003
Posts: 89
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 ;-)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephane Clinckart
Ranch Hand

Joined: Oct 21, 2003
Posts: 89
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.
 
jQuery in Action, 2nd edition
 
subject: How to do this in java ?
 
Similar Threads
Struts validation regular expressions
WA #1.....word association
Container classes in Spring
How to sort two dimentional array in javascript
Calculate date difference [Gregorian calender]