/* * Copyright (c) 1997, 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 4143459 * @summary test Date * @library /java/text/testlib
*/
/** * @bug 4143459 * Warning: Use TestDefaultZone() for complete testing of this bug.
*/ publicvoid TestDefaultZoneLite() { // Note: This test is redundant with TestDefaultZone(). It was added by // request to provide a short&sweet test for this bug. It does not test // all cases though, so IF THIS TEST PASSES, THE BUG MAY STILL BE // PRESENT. Use TestDefaultZone() to be sure.
TimeZone save = TimeZone.getDefault(); try {
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
Date d = new Date();
d.setYear(98);
d.setMonth(Calendar.JANUARY);
d.setDate(1);
d.setHours(6);
TimeZone.setDefault(TimeZone.getTimeZone("PST")); if (d.getHours() != 22) {
errln("Fail: Date.setHours()/getHours() ignoring default zone");
}
} finally { TimeZone.setDefault(save); }
}
/** * @bug 4143459
*/ publicvoid TestDefaultZone() { // Various problems can creep up, with the current implementation of Date, // when the default zone is changed.
TimeZone saveZone = TimeZone.getDefault(); try {
Date d = new Date(); // Trigger static init
Date ref = new Date(883634400000L); // This is Thu Jan 1 1998 6:00 am GMT
String refstr = "Jan 1 1998 6:00";
TimeZone GMT = TimeZone.getTimeZone("GMT");
TimeZone PST = TimeZone.getTimeZone("PST");
// There are two cases to consider: a Date object with no Calendar // sub-object (most Date objects), and a Date object with a Calendar // sub-object. We make two passes to cover the two cases. for (int pass=0; pass<2; ++pass) {
logln(pass == 0 ? "Normal Date object" : "Date with Calendar sub-object");
TimeZone.setDefault(GMT);
d = new Date(refstr); if (pass == 1) { // Force creation of Calendar sub-object
d.setYear(d.getYear());
} if (d.getTime() != ref.getTime()) {
errln("FAIL: new Date(\"" + refstr + "\") x GMT -> " + d + " " + d.getTime() + " ms");
}
// Test the performance of Date publicvoid TestPerformance592()
{ int REPS = 500;
// Do timing test with Date long start = new Date().getTime(); for (int i=0; i<REPS; ++i)
{
Date d = new Date(); int y = d.getYear();
} long ms = new Date().getTime() - start;
double perLoop = ((double)ms) / REPS;
logln(REPS + " iterations at " + perLoop + " ms/loop"); if (perLoop > PER_LOOP_LIMIT)
logln("WARNING: Date constructor/getYear slower than " +
PER_LOOP_LIMIT + " ms");
} staticdouble PER_LOOP_LIMIT = 3.0;
/** * Verify that the Date(String) constructor works.
*/ publicvoid TestParseOfGMT()
{
Date OUT = null;
// Check out Date's behavior with large negative year values; bug 664 // As of the fix to bug 4056585, Date should work correctly with // large negative years. publicvoid TestDateNegativeYears()
{
Date d1= new Date(80,-1,2);
logln(d1.toString());
d1= new Date(-80,-1,2);
logln(d1.toString()); boolean e = false; try {
d1= new Date(-800000,-1,2);
logln(d1.toString());
} catch (IllegalArgumentException ex) {
e = true;
} if (e) errln("FAIL: Saw exception for year -800000"); else logln("Pass: No exception for year -800000");
}
// Verify the behavior of Date publicvoid TestDate480()
{
TimeZone save = TimeZone.getDefault(); try {
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
Date d1=new java.util.Date(97,8,13,10,8,13);
logln("d = "+d1);
Date d2=new java.util.Date(97,8,13,30,8,13); // 20 hours later
logln("d+20h = "+d2);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.