View Javadoc
1   package ch.qos.logback.core.util;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.text.ParseException;
6   import java.text.SimpleDateFormat;
7   import java.util.Date;
8   import java.util.TimeZone;
9   
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  public class CachingFotmatterTest {
14  
15      final static String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm";
16  
17      SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
18      TimeZone perthTZ = TimeZone.getTimeZone("Australia/Perth");
19      TimeZone utcTZ = TimeZone.getTimeZone("UTC");
20  
21      @Before
22      public void setUp() {
23          sdf.setTimeZone(utcTZ);
24      }
25  
26      @Test
27      public void timeZoneIsTakenIntoAccount() throws ParseException {
28  
29          CachingDateFormatter cdf = new CachingDateFormatter(DATE_PATTERN);
30          TimeZone perthTZ = TimeZone.getTimeZone("Australia/Perth");
31          cdf.setTimeZone(perthTZ);
32  
33          Date march26_2015_0949_UTC = sdf.parse("2015-03-26T09:49");
34          System.out.print(march26_2015_0949_UTC);
35  
36          String result = cdf.format(march26_2015_0949_UTC.getTime());
37          // AWST (Perth) is 8 hours ahead of UTC
38          assertEquals("2015-03-26T17:49", result);
39      }
40  }