• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp;
33 
34 import static org.testng.Assert.assertEquals;
35 
36 import java.util.Calendar;
37 import java.util.Date;
38 import java.util.GregorianCalendar;
39 import java.util.TimeZone;
40 
41 import org.testng.annotations.Test;
42 
43 /**
44  * Test.
45  */
46 @Test
47 public class TestDateTimeUtils {
48 
49     private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
50     private static final TimeZone PARIS_TZ = TimeZone.getTimeZone("Europe/Paris");
51 
52     //-----------------------------------------------------------------------
test_toInstant_Date()53     public void test_toInstant_Date() {
54         Date date = new Date(123456);
55         assertEquals(DateTimeUtils.toInstant(date), Instant.ofEpochMilli(123456));
56     }
57 
test_toDate_Instant()58     public void test_toDate_Instant() {
59         Instant instant = Instant.ofEpochMilli(123456);
60         assertEquals(DateTimeUtils.toDate(instant), new Date(123456));
61     }
62 
63     //-----------------------------------------------------------------------
test_toInstant_Calendar()64     public void test_toInstant_Calendar() {
65         Calendar calendar = GregorianCalendar.getInstance();
66         calendar.setTimeInMillis(123456);
67         assertEquals(DateTimeUtils.toInstant(calendar), Instant.ofEpochMilli(123456));
68     }
69 
test_toZDT_Calendar()70     public void test_toZDT_Calendar() {
71         ZonedDateTime zdt = ZonedDateTime.of(2012, 6, 30, 11, 30, 40, 0, PARIS);
72         Calendar calendar = GregorianCalendar.getInstance(PARIS_TZ);
73         calendar.setFirstDayOfWeek(Calendar.MONDAY);
74         calendar.setMinimalDaysInFirstWeek(4);
75         calendar.clear();
76         calendar.set(2012, 6 - 1, 30, 11, 30, 40);
77         assertEquals(DateTimeUtils.toZonedDateTime(calendar), zdt);
78     }
79 
test_toCalendar_ZDT()80     public void test_toCalendar_ZDT() {
81         ZonedDateTime zdt = ZonedDateTime.of(2012, 6, 30, 11, 30, 40, 0, PARIS);
82         GregorianCalendar calendar = new GregorianCalendar(PARIS_TZ);
83         calendar.setFirstDayOfWeek(Calendar.MONDAY);
84         calendar.setMinimalDaysInFirstWeek(4);
85         calendar.set(2012, 6 - 1, 30, 11, 30, 40);
86         calendar.set(Calendar.MILLISECOND, 0);
87         calendar.setTimeInMillis(calendar.getTimeInMillis());
88         calendar.setGregorianChange(new Date(Long.MIN_VALUE));
89         GregorianCalendar test = DateTimeUtils.toGregorianCalendar(zdt);
90         assertEquals(test, calendar);
91     }
92 
93     //-----------------------------------------------------------------------
test_toZoneId_TimeZone()94     public void test_toZoneId_TimeZone() {
95         assertEquals(DateTimeUtils.toZoneId(PARIS_TZ), PARIS);
96     }
97 
test_toTimeZone_ZoneId()98     public void test_toTimeZone_ZoneId() {
99         assertEquals(DateTimeUtils.toTimeZone(PARIS), PARIS_TZ);
100     }
101 
102     //-----------------------------------------------------------------------
test_toLocalDate_SqlDate()103     public void test_toLocalDate_SqlDate() {
104         @SuppressWarnings("deprecation")
105         java.sql.Date sqlDate = new java.sql.Date(2012 - 1900, 6 - 1, 30);
106         LocalDate localDate = LocalDate.of(2012, 6, 30);
107         assertEquals(DateTimeUtils.toLocalDate(sqlDate), localDate);
108     }
109 
test_toSqlDate_LocalDate()110     public void test_toSqlDate_LocalDate() {
111         @SuppressWarnings("deprecation")
112         java.sql.Date sqlDate = new java.sql.Date(2012 - 1900, 6 - 1, 30);
113         LocalDate localDate = LocalDate.of(2012, 6, 30);
114         assertEquals(DateTimeUtils.toSqlDate(localDate), sqlDate);
115     }
116 
117     //-----------------------------------------------------------------------
test_toLocalTime_SqlTime()118     public void test_toLocalTime_SqlTime() {
119         @SuppressWarnings("deprecation")
120         java.sql.Time sqlTime = new java.sql.Time(11, 30, 40);
121         LocalTime localTime = LocalTime.of(11, 30, 40);
122         assertEquals(DateTimeUtils.toLocalTime(sqlTime), localTime);
123     }
124 
test_toSqlTime_LocalTime()125     public void test_toSqlTime_LocalTime() {
126         @SuppressWarnings("deprecation")
127         java.sql.Time sqlTime = new java.sql.Time(11, 30, 40);
128         LocalTime localTime = LocalTime.of(11, 30, 40);
129         assertEquals(DateTimeUtils.toSqlTime(localTime), sqlTime);
130     }
131 
132     //-----------------------------------------------------------------------
test_toLocalDateTime_SqlTimestamp()133     public void test_toLocalDateTime_SqlTimestamp() {
134         @SuppressWarnings("deprecation")
135         java.sql.Timestamp sqlDateTime = new java.sql.Timestamp(2012 - 1900, 6 - 1, 30, 11, 30, 40, 0);
136         LocalDateTime localDateTime = LocalDateTime.of(2012, 6, 30, 11, 30, 40, 0);
137         assertEquals(DateTimeUtils.toLocalDateTime(sqlDateTime), localDateTime);
138     }
139 
test_toSqlTimestamp_LocalDateTime()140     public void test_toSqlTimestamp_LocalDateTime() {
141         @SuppressWarnings("deprecation")
142         java.sql.Timestamp sqlDateTime = new java.sql.Timestamp(2012 - 1900, 6 - 1, 30, 11, 30, 40, 0);
143         LocalDateTime localDateTime = LocalDateTime.of(2012, 6, 30, 11, 30, 40, 0);
144         assertEquals(DateTimeUtils.toSqlTimestamp(localDateTime), sqlDateTime);
145     }
146 
147     //-----------------------------------------------------------------------
test_toInstant_SqlTimestamp()148     public void test_toInstant_SqlTimestamp() {
149         @SuppressWarnings("deprecation")
150         java.sql.Timestamp sqlDateTime = new java.sql.Timestamp(2012 - 1900, 6 - 1, 30, 11, 30, 40, 0);
151         assertEquals(DateTimeUtils.toInstant(sqlDateTime), Instant.ofEpochMilli(sqlDateTime.getTime()));
152     }
153 
test_toSqlTimestamp_Instant()154     public void test_toSqlTimestamp_Instant() {
155         Instant instant = Instant.ofEpochMilli(123456);
156         java.sql.Timestamp sqlDateTime = new java.sql.Timestamp(instant.toEpochMilli());
157         assertEquals(DateTimeUtils.toSqlTimestamp(instant), sqlDateTime);
158     }
159 
160 }
161