View Javadoc
1   package fr.ifremer.dali.util;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import org.apache.commons.lang3.time.DateUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  import java.text.ParseException;
33  import java.time.LocalDateTime;
34  import java.time.ZoneOffset;
35  import java.time.ZonedDateTime;
36  import java.time.format.DateTimeFormatter;
37  import java.util.Date;
38  import java.util.Locale;
39  
40  public class TimeUtilsTest {
41  
42      private static final Log log = LogFactory.getLog(TimeUtilsTest.class);
43  
44      private static final String DATE_FORMAT_PATTERN = "dd/MM/yyyy HH:mm";
45      private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern(DATE_FORMAT_PATTERN);
46  
47      private static final String DATE1 = "16/09/2015 12:00";
48      private static final String DATE2 = "25/12/2015 12:00";
49  
50      @Test
51      public void testTimeZone() {
52  
53          try {
54              Date javaDate1 = DateUtils.parseDate(DATE1, Locale.FRANCE, DATE_FORMAT_PATTERN);
55              log.info(javaDate1);
56  
57              LocalDateTime dateTime1 = LocalDateTime.parse(DATE1, DATE_FORMAT);
58              log.info(dateTime1);
59              LocalDateTime dateTime2 = LocalDateTime.parse(DATE2, DATE_FORMAT);
60              log.info(dateTime2);
61  
62              // with +2
63              DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_PATTERN).withZone(ZoneOffset.ofHours(1));
64              ZonedDateTime dateTime1a = ZonedDateTime.parse(DATE1, dateTimeFormatter);
65              log.info(dateTime1a);
66              ZonedDateTime dateTime2a = ZonedDateTime.parse(DATE2, dateTimeFormatter);
67              log.info(dateTime2a);
68  
69              // with -4
70              dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_PATTERN).withZone(ZoneOffset.ofHours(-4));
71              ZonedDateTime dateTime1b = ZonedDateTime.parse(DATE1, dateTimeFormatter);
72              log.info(dateTime1b);
73              ZonedDateTime dateTime2b = ZonedDateTime.parse(DATE2, dateTimeFormatter);
74              log.info(dateTime2b);
75  
76  
77          } catch (ParseException e) {
78              Assert.fail(e.getLocalizedMessage());
79          }
80  
81      }
82  }