File: DateRegression.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (241 lines) | stat: -rw-r--r-- 7,619 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4023247 4027685 4032037 4072029 4073003 4118010 4120606 4133833 4136916 6274757 6314387
 * @library /java/text/testlib
 */

import java.util.*;

@SuppressWarnings("deprecation")
public class DateRegression extends IntlTest {

    public static void main(String[] args) throws Exception {
        new DateRegression().run(args);
    }

    /**
     * @bug 4023247
     */
    public void Test4023247() {
        Date d1 = new Date(0);
        Date d2 = new Date(0);

        d1.setYear(96);
        d1.setMonth(11);
        d1.setDate(22);
        d1.setHours(0);
        d1.setMinutes(0);
        d1.setSeconds(0);

        d2.setYear(96);
        d2.setMonth(11);
        d2.setDate(22);
        d2.setHours(0);
        d2.setMinutes(0);
        d2.setSeconds(0);

        if (d1.hashCode() != d2.hashCode())
            errln("Fail: Date hashCode misbehaves");
    }

    /**
     * @bug 4027685
     */
    public void Test4027685() {
        // Should be 01/16/97 00:00:00
        Date nite = new Date("16-JAN-97 12:00 AM");
        // Should be 01/16/97 12:00:00
        Date noon = new Date("16-JAN-97 12:00 PM");

        logln("Midnight = " + nite + ", Noon = " + noon);
        if (!nite.equals(new Date(97, Calendar.JANUARY, 16, 0, 0)) ||
            !noon.equals(new Date(97, Calendar.JANUARY, 16, 12, 0)))
            errln("Fail: Nite/Noon confused");
    }

    /**
     * @bug 4032037
     */
    public void Test4032037() {
        Date ref = new Date(97, 1, 10);
        Date d = new Date(Date.parse("2/10/97"));
        logln("Date.parse(2/10/97) => " + d);
        if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);
        d = new Date(Date.parse("10 feb 1997"));
        logln("Date.parse(10 feb 1997) => " + d);
        if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);
        d = new Date("2/10/97");
        logln("Date(2/10/97) => " + d);
        if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);
        d = new Date("10 feb 1997");
        logln("Date(10 feb 1997) => " + d);
        if (!d.equals(ref)) errln("Fail: Want " + ref + " Got " + d);
    }

    /**
     * @bug 4072029
     */
    public void Test4072029() {
        TimeZone saveZone = TimeZone.getDefault();

        try {
            TimeZone.setDefault(TimeZone.getTimeZone("PST"));
            Date now = new Date();
            String s = now.toString();
            Date now2 = new Date(now.toString());
            String s2 = now2.toString(); // An hour's difference

            if (!s.equals(s2) ||
                Math.abs(now.getTime() - now2.getTime()) > 60000 /*one min*/) {
                errln("Fail: Roundtrip toString/parse");
            }
        }
        finally {
            TimeZone.setDefault(saveZone);
        }
    }

    /**
     * @bug 4073003
     */
    public void Test4073003() {
        Date d = new Date(Date.parse("01/02/1984"));
        if (!d.equals(new Date(84, 0, 2)))
            errln("Fail: Want 1/2/1984 Got " + d);
        d = new Date(Date.parse("02/03/2012"));
        if (!d.equals(new Date(112, 1, 3)))
            errln("Fail: Want 2/3/2012 Got " + d);
        d = new Date(Date.parse("03/04/15"));
        if (!d.equals(new Date(115, 2, 4)))
            errln("Fail: Want 3/4/2015 Got " + d);
    }

    /**
     * @bug 4118010
     * Regress bug:
     * Feb. 2000 has 29 days, but Date(2000, 1, 29) returns March 01, 2000
     * NOTE: This turned out to be a user error (passing in 2000 instead
     * of 2000-1900 to the Date constructor).
     */
    public void Test4118010() {
        Date d=new java.util.Date(2000-1900, Calendar.FEBRUARY, 29);
        int m=d.getMonth();
        int date=d.getDate();
        if (m != Calendar.FEBRUARY ||
            date != 29)
            errln("Fail: Want Feb 29, got " + d);
    }

    /**
     * @bug 4120606
     * Date objects share state after cloning.
     */
    public void Test4120606() {
        Date d = new Date(98, Calendar.JUNE, 24);
        d.setMonth(Calendar.MAY);
        Date e = (Date)d.clone();
        d.setMonth(Calendar.FEBRUARY);
        if (e.getMonth() != Calendar.MAY) {
            errln("Cloned Date objects share state");
        }
    }

    /**
     * @bug 4133833
     * Date constructor crashes with parameters out of range, when it should
     * normalize.
     */
    public void Test4133833() {
        Date date = new java.util.Date(12,15,19);
        Date exp  = new Date(1913-1900, Calendar.APRIL, 19);
        if (!date.equals(exp))
            errln("Fail: Want " + exp +
                                "; got " + date);
    }

    /**
     * @bug 4136916
     * Date.toString() throws exception in 1.2b4-E
     * CANNOT REPRODUCE this bug
     */
    public void Test4136916() {
        Date time = new Date();
        logln(time.toString());
    }

    /**
     * @bug 6274757
     * Date getTime and toString interaction for some time values
     */
    public void Test6274757() {
        TimeZone savedTz = TimeZone.getDefault();
        try {
            // Use a time zone west of GMT.
            TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
            TimeZone jdkGMT = TimeZone.getTimeZone("GMT");
            Calendar jdkCal = Calendar.getInstance(jdkGMT);
            jdkCal.clear();
            jdkCal.set(1582, Calendar.OCTOBER, 15);
            logln("JDK time: " + jdkCal.getTime().getTime() );
            logln("JDK time (str): " + jdkCal.getTime() );
            logln("Day of month: " + jdkCal.get(Calendar.DAY_OF_MONTH));
            Date co = jdkCal.getTime();
            logln("Change over (Oct 15 1582) = " + co + " (" +
                  co.getTime() + ")");
            long a = jdkCal.getTime().getTime();
            Date c = jdkCal.getTime();
            c.toString();
            long b = c.getTime();

            if (a != b) {
                errln("ERROR: " + a + " != " + b);
            } else {
                logln(a + " = " + b);
            }
        } finally {
            TimeZone.setDefault(savedTz);
        }
    }

    /**
     * @bug 6314387
     * JCK6.0: api/java_util/Date/index.html#misc fails, mustang
     */
    public void Test6314387() {
        Date d = new Date(Long.MAX_VALUE);
        int y = d.getYear();
        if (y != 292277094) {
            errln("yesr: got " + y + ", expected 292277094");
        }
        d = new Date(Long.MIN_VALUE);
        y = d.getYear();
        if (y != 292267155) {
            errln("yesr: got " + y + ", expected 292267155");
        }
    }
}

//eof