1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.util;
16  
17  import java.text.ParseException;
18  import java.text.SimpleDateFormat;
19  import java.time.ZoneId;
20  import java.util.Date;
21  import java.util.TimeZone;
22  
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  
28  public class CachingDateFotmatterTest {
29  
30      private final static String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm";
31  
32      SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
33      TimeZone perthTZ = TimeZone.getTimeZone("Australia/Perth");
34      TimeZone utcTZ = TimeZone.getTimeZone("UTC");
35  
36      @BeforeEach
37      public void setUp() {
38          sdf.setTimeZone(utcTZ);
39      }
40  
41      @Test
42      public void timeZoneIsTakenIntoAccount() throws ParseException {
43  
44          ZoneId perthZone = ZoneId.of("Australia/Perth");
45          CachingDateFormatter cdf = new CachingDateFormatter(DATE_PATTERN, perthZone);
46  
47          Date march26_2015_0949_UTC = sdf.parse("2015-03-26T09:49");
48          System.out.print(march26_2015_0949_UTC);
49  
50          String result = cdf.format(march26_2015_0949_UTC.getTime());
51          // AWST (Perth) is 8 hours ahead of UTC
52          assertEquals("2015-03-26T17:49", result);
53      }
54  
55  }