Created
          August 7, 2015 14:09 
        
      - 
      
- 
        Save trevorrjohn/b9f161d234b39ab6f20f to your computer and use it in GitHub Desktop. 
    JUnit TimeZoneRule for setting timeZone in test
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class TimeZoneRule implements TestRule { | |
| private final String timeZoneId; | |
| public TimeZoneRule(String timeZoneId) { | |
| this.timeZoneId = timeZoneId; | |
| } | |
| @Override | |
| public Statement apply(final Statement base, Description description) { | |
| return new Statement() { | |
| @Override | |
| public void evaluate() throws Throwable { | |
| TimeZone defaultTimeZone = TimeZone.getDefault(); | |
| try { | |
| TimeZone.setDefault(TimeZone.getTimeZone(timeZoneId)); | |
| base.evaluate(); | |
| } finally { | |
| TimeZone.setDefault(defaultTimeZone); | |
| } | |
| } | |
| }; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment