Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:47:27+00:00 2026-06-06T01:47:27+00:00

I’m using JodaTime 2.1 and I’m looking for a pattern to unit test code

  • 0

I’m using JodaTime 2.1 and I’m looking for a pattern to unit test code which performs date/time operations to make sure it behaves well for all time zones and independent of DST.

Specifically:

  1. How can I mock the system clock (so I don’t have to mock all the places where I call new DateTime() to get the current time)
  2. How can I do the same for the default time zone?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:47:29+00:00Added an answer on June 6, 2026 at 1:47 am

    You can use a @Rule for this. Here is the code for the rule:

    import org.joda.time.DateTimeZone;
    import org.junit.rules.TestWatcher;
    import org.junit.runner.Description;
    
    public class UTCRule extends TestWatcher {
    
        private DateTimeZone origDefault = DateTimeZone.getDefault();
    
        @Override
        protected void starting( Description description ) {
            DateTimeZone.setDefault( DateTimeZone.UTC );
        }
    
        @Override
        protected void finished( Description description ) {
            DateTimeZone.setDefault( origDefault );
        }
    }
    

    You can use the rule like this:

    public class SomeTest {
    
        @Rule
        public UTCRule utcRule = new UTCRule();
    
        ....
    }
    

    This will change the current time zone to UTC before each test in SomeTest will be executed and it will restore the default time zone after each test.

    If you want to check several time zones, use a rule like this one:

    import org.joda.time.DateTimeZone;
    import org.junit.rules.TestWatcher;
    import org.junit.runner.Description;
    
    public class TZRule extends TestWatcher {
    
        private DateTimeZone origDefault = DateTimeZone.getDefault();
    
        private DateTimeZone tz;
    
        public TZRule( DateTimeZone tz ) {
            this.tz = tz;
        }
    
        @Override
        protected void starting( Description description ) {
            DateTimeZone.setDefault( tz );
        }
    
        @Override
        protected void finished( Description description ) {
            DateTimeZone.setDefault( origDefault );
        }
    }
    

    Put all the affected tests in an abstract base class AbstractTZTest and extend it:

    public class UTCTest extends AbstractTZTest {
        @Rule public TZRule tzRule = new TZRule( DateTimeZone.UTC );
    }
    

    That will execute all tests in AbstractTZTest with UTC. For each time zone that you want to test, you’ll need another class:

    public class UTCTest extends AbstractTZTest {
        @Rule public TZRule tzRule = new TZRule( DateTimeZone.forID( "..." );
    }
    

    Since test cases are inherited, that’s all – you just need to define the rule.

    In a similar way, you can shift the system clock. Use a rule that calls DateTimeUtils.setCurrentMillisProvider(...) to simulate that the test runs at a certain time and DateTimeUtils.setCurrentMillisSystem() to restore the defaults.

    Note: Your provider will need a way to make the clock tick or all new DateTime instances will have the same value. I often advance the value by a millisecond each time getMillis() is called.

    Note 2: That only works with joda-time. It doesn’t affect new java.util.Date().

    Note 3: You can’t run these tests in parallel anymore. They must run in sequence or one of them will most likely restore the default timezone while another test is running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.