View | Details | Raw Unified | Return to bug 57512
Collapse All | Expand All

(-)a/src/java/org/apache/poi/ss/usermodel/DateUtil.java (-7 / +7 lines)
Lines 146-152 public class DateUtil { Link Here
146
     *  @return Java representation of the date, or null if date is not a valid Excel date
146
     *  @return Java representation of the date, or null if date is not a valid Excel date
147
     */
147
     */
148
    public static Date getJavaDate(double date, TimeZone tz) {
148
    public static Date getJavaDate(double date, TimeZone tz) {
149
       return getJavaDate(date, false, tz);
149
       return getJavaDate(date, false, tz, false);
150
    }
150
    }
151
    /**
151
    /**
152
     *  Given an Excel date with using 1900 date windowing, and
152
     *  Given an Excel date with using 1900 date windowing, and
Lines 166-174 public class DateUtil { Link Here
166
     *  @see java.util.TimeZone
166
     *  @see java.util.TimeZone
167
     */
167
     */
168
    public static Date getJavaDate(double date) {
168
    public static Date getJavaDate(double date) {
169
        return getJavaDate(date, (TimeZone)null);
169
        return getJavaDate(date, false, null, false);
170
    }
170
    }
171
    
171
172
    /**
172
    /**
173
     *  Given an Excel date with either 1900 or 1904 date windowing,
173
     *  Given an Excel date with either 1900 or 1904 date windowing,
174
     *  converts it to a java.util.Date.
174
     *  converts it to a java.util.Date.
Lines 185-191 public class DateUtil { Link Here
185
     *  @return Java representation of the date, or null if date is not a valid Excel date
185
     *  @return Java representation of the date, or null if date is not a valid Excel date
186
     */
186
     */
187
    public static Date getJavaDate(double date, boolean use1904windowing, TimeZone tz) {
187
    public static Date getJavaDate(double date, boolean use1904windowing, TimeZone tz) {
188
        return getJavaCalendar(date, use1904windowing, tz, false).getTime();
188
        return getJavaDate(date, use1904windowing, tz, false);
189
    }
189
    }
190
    
190
    
191
    /**
191
    /**
Lines 205-211 public class DateUtil { Link Here
205
     *  @return Java representation of the date, or null if date is not a valid Excel date
205
     *  @return Java representation of the date, or null if date is not a valid Excel date
206
     */
206
     */
207
    public static Date getJavaDate(double date, boolean use1904windowing, TimeZone tz, boolean roundSeconds) {
207
    public static Date getJavaDate(double date, boolean use1904windowing, TimeZone tz, boolean roundSeconds) {
208
        return getJavaCalendar(date, use1904windowing, tz, roundSeconds).getTime();
208
        Calendar calendar = getJavaCalendar(date, use1904windowing, tz, roundSeconds);
209
        return calendar == null ? null : calendar.getTime();
209
    }
210
    }
210
    
211
    
211
    /**
212
    /**
Lines 228-237 public class DateUtil { Link Here
228
     *  @see java.util.TimeZone
229
     *  @see java.util.TimeZone
229
     */
230
     */
230
    public static Date getJavaDate(double date, boolean use1904windowing) {
231
    public static Date getJavaDate(double date, boolean use1904windowing) {
231
        return getJavaCalendar(date, use1904windowing, null, false).getTime();
232
        return getJavaDate(date, use1904windowing, null, false);
232
    }
233
    }
233
234
234
235
    public static void setCalendar(Calendar calendar, int wholeDays,
235
    public static void setCalendar(Calendar calendar, int wholeDays,
236
            int millisecondsInDay, boolean use1904windowing, boolean roundSeconds) {
236
            int millisecondsInDay, boolean use1904windowing, boolean roundSeconds) {
237
        int startYear = 1900;
237
        int startYear = 1900;
(-)a/src/testcases/org/apache/poi/ss/usermodel/TestDateUtil.java (+92 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.usermodel;
19
20
import static org.junit.Assert.assertEquals;
21
22
import java.util.Calendar;
23
import java.util.Date;
24
import java.util.TimeZone;
25
26
import org.junit.Test;
27
28
public class TestDateUtil {
29
30
    @Test
31
    public void getJavaDate_InvalidValue() {
32
        double dateValue = -1;
33
        TimeZone tz = TimeZone.getDefault();
34
        boolean use1904windowing = false;
35
        boolean roundSeconds = false;
36
37
        assertEquals(null, DateUtil.getJavaDate(dateValue));
38
        assertEquals(null, DateUtil.getJavaDate(dateValue, tz));
39
        assertEquals(null, DateUtil.getJavaDate(dateValue, use1904windowing));
40
        assertEquals(null, DateUtil.getJavaDate(dateValue, use1904windowing, tz));
41
        assertEquals(null, DateUtil.getJavaDate(dateValue, use1904windowing, tz, roundSeconds));
42
    }
43
44
    @Test
45
    public void getJavaDate_ValidValue() {
46
        double dateValue = 0;
47
        TimeZone tz = TimeZone.getDefault();
48
        boolean use1904windowing = false;
49
        boolean roundSeconds = false;
50
51
        Calendar calendar = Calendar.getInstance(tz);
52
        calendar.set(1900, 0, 0, 0, 0, 0);
53
        calendar.set(Calendar.MILLISECOND, 0);
54
        Date date = calendar.getTime();
55
56
        assertEquals(date, DateUtil.getJavaDate(dateValue));
57
        assertEquals(date, DateUtil.getJavaDate(dateValue, tz));
58
        assertEquals(date, DateUtil.getJavaDate(dateValue, use1904windowing));
59
        assertEquals(date, DateUtil.getJavaDate(dateValue, use1904windowing, tz));
60
        assertEquals(date, DateUtil.getJavaDate(dateValue, use1904windowing, tz, roundSeconds));
61
    }
62
63
    @Test
64
    public void getJavaCalendar_InvalidValue() {
65
        double dateValue = -1;
66
        TimeZone tz = TimeZone.getDefault();
67
        boolean use1904windowing = false;
68
        boolean roundSeconds = false;
69
70
        assertEquals(null, DateUtil.getJavaCalendar(dateValue));
71
        assertEquals(null, DateUtil.getJavaCalendar(dateValue, use1904windowing));
72
        assertEquals(null, DateUtil.getJavaCalendar(dateValue, use1904windowing, tz));
73
        assertEquals(null, DateUtil.getJavaCalendar(dateValue, use1904windowing, tz, roundSeconds));
74
    }
75
76
    @Test
77
    public void getJavaCalendar_ValidValue() {
78
        double dateValue = 0;
79
        TimeZone tz = TimeZone.getDefault();
80
        boolean use1904windowing = false;
81
        boolean roundSeconds = false;
82
83
        Calendar calendar = Calendar.getInstance(tz);
84
        calendar.set(1900, 0, 0, 0, 0, 0);
85
        calendar.set(Calendar.MILLISECOND, 0);
86
87
        assertEquals(calendar, DateUtil.getJavaCalendar(dateValue));
88
        assertEquals(calendar, DateUtil.getJavaCalendar(dateValue, use1904windowing));
89
        assertEquals(calendar, DateUtil.getJavaCalendar(dateValue, use1904windowing, tz));
90
        assertEquals(calendar, DateUtil.getJavaCalendar(dateValue, use1904windowing, tz, roundSeconds));
91
    }
92
}

Return to bug 57512