Skip to content

Instantly share code, notes, and snippets.

@trevorrjohn
Created August 7, 2015 14:09
Show Gist options
  • Save trevorrjohn/b9f161d234b39ab6f20f to your computer and use it in GitHub Desktop.
Save trevorrjohn/b9f161d234b39ab6f20f to your computer and use it in GitHub Desktop.
JUnit TimeZoneRule for setting timeZone in test
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