< prev index next >

test/jdk/java/util/Calendar/bug4243802.java

Print this page

  1 /*
  2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 4243802
 27  * @summary confirm that Calendar.setTimeInMillis() and
 28  * getTimeInMillis() can be called from a user program. (They used to
 29  * be protected methods.)
 30  * @library /java/text/testlib
 31  */
 32 
 33 import java.util.*;


 34 
 35 public class bug4243802 extends IntlTest {


 36 
 37     public static void main(String[] args) throws Exception {
 38         new bug4243802().run(args);


















 39     }
 40 
 41     /**
 42      * 4243802: RFE: need way to set the date of a calendar without a Date object


 43      */
 44     public void Test4243802() {
 45         TimeZone saveZone = TimeZone.getDefault();
 46         Locale saveLocale = Locale.getDefault();
 47         try {
 48             Locale.setDefault(Locale.US);
 49             TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));



 50 
 51             Calendar cal1 = Calendar.getInstance();
 52             Calendar cal2 = Calendar.getInstance();






 53 
 54             cal1.clear();
 55             cal2.clear();
 56             cal1.set(2001, Calendar.JANUARY, 25, 1, 23, 45);
 57             cal2.setTimeInMillis(cal1.getTimeInMillis());
 58             if ((cal2.get(Calendar.YEAR) != 2001) ||
 59                 (cal2.get(Calendar.MONTH) != Calendar.JANUARY) ||
 60                 (cal2.get(Calendar.DAY_OF_MONTH) != 25) ||
 61                 (cal2.get(Calendar.HOUR_OF_DAY) != 1) ||
 62                 (cal2.get(Calendar.MINUTE) != 23) ||
 63                 (cal2.get(Calendar.SECOND) != 45) ||
 64                 (cal2.get(Calendar.MILLISECOND) != 0)) {
 65                  errln("Failed: expected 1/25/2001 1:23:45.000" +
 66                        ", got " + (cal2.get(Calendar.MONTH)+1) + "/" +
 67                        cal2.get(Calendar.DAY_OF_MONTH) +"/" +
 68                        cal2.get(Calendar.YEAR) + " " +
 69                        cal2.get(Calendar.HOUR_OF_DAY) + ":" +
 70                        cal2.get(Calendar.MINUTE) + ":" +
 71                        cal2.get(Calendar.SECOND) + "." +
 72                        toMillis(cal2.get(Calendar.MILLISECOND)));
 73             }
 74             logln("Passed.");
 75         }
 76         finally {
 77             Locale.setDefault(saveLocale);
 78             TimeZone.setDefault(saveZone);
 79         }
 80     }
 81 
 82     private String toMillis(int m) {
 83         StringBuffer sb = new StringBuffer();

 84         if (m < 100) {
 85             sb.append('0');
 86         }
 87         if (m < 10) {
 88             sb.append('0');
 89         }
 90         sb.append(m);
 91         return sb.toString();
 92     }
 93 }

  1 /*
  2  * Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 4243802
 27  * @summary confirm that Calendar.setTimeInMillis() and
 28  *          getTimeInMillis() can be called from a user program. (They used to
 29  *          be protected methods.)
 30  * @run junit bug4243802
 31  */
 32 
 33 import java.util.Calendar;
 34 import java.util.Locale;
 35 import java.util.TimeZone;
 36 
 37 import org.junit.jupiter.api.AfterAll;
 38 import org.junit.jupiter.api.BeforeAll;
 39 import org.junit.jupiter.api.Test;
 40 
 41 import static org.junit.jupiter.api.Assertions.assertEquals;
 42 
 43 public class bug4243802 {
 44 
 45     // Save JVM default Locale and TimeZone
 46     private static final TimeZone savedTz = TimeZone.getDefault();
 47     private static final Locale savedLocale = Locale.getDefault();
 48 
 49     // Set custom JVM default Locale and TimeZone
 50     @BeforeAll
 51     static void initAll() {
 52         Locale.setDefault(Locale.US);
 53         TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
 54     }
 55 
 56     // Restore JVM default Locale and TimeZone
 57     @AfterAll
 58     static void tearDownAll() {
 59         Locale.setDefault(savedLocale);
 60         TimeZone.setDefault(savedTz);
 61     }
 62 
 63     /*
 64      * Test getTimeInMillis() and setTimeInMillis(). Compare a Calendar
 65      * set with a traditional date to one set using setTimeInMillis(),
 66      * where both Calendars should be of equal times.
 67      */
 68     @Test
 69     public void setCalendarWithoutDateTest() {
 70         Calendar cal1 = Calendar.getInstance();
 71         Calendar cal2 = Calendar.getInstance();
 72         cal1.clear();
 73         cal2.clear();
 74         cal1.set(2001, Calendar.JANUARY, 25, 1, 23, 45);
 75         // Build the second calendar using the getTimeInMillis of the first
 76         cal2.setTimeInMillis(cal1.getTimeInMillis());
 77 
 78         assertEquals(2001, cal2.get(Calendar.YEAR), getErrMsg(cal1));
 79         assertEquals(Calendar.JANUARY, cal2.get(Calendar.MONTH), getErrMsg(cal1));
 80         assertEquals(25, cal2.get(Calendar.DAY_OF_MONTH), getErrMsg(cal1));
 81         assertEquals(1, cal2.get(Calendar.HOUR_OF_DAY), getErrMsg(cal1));
 82         assertEquals(23, cal2.get(Calendar.MINUTE), getErrMsg(cal1));
 83         assertEquals(45, cal2.get(Calendar.SECOND), getErrMsg(cal1));
 84         assertEquals(0, cal2.get(Calendar.MILLISECOND), getErrMsg(cal1));
 85     }
 86 
 87     // Utility to build a long error message
 88     private static String getErrMsg(Calendar cal) {
 89         return "Failed: expected 1/25/2001 1:23:45.000" +
 90                 ", got " + (cal.get(Calendar.MONTH)+1) + "/" +
 91                 cal.get(Calendar.DAY_OF_MONTH) +"/" +
 92                 cal.get(Calendar.YEAR) + " " +
 93                 cal.get(Calendar.HOUR_OF_DAY) + ":" +
 94                 cal.get(Calendar.MINUTE) + ":" +
 95                 cal.get(Calendar.SECOND) + "." +
 96                 toMillis(cal.get(Calendar.MILLISECOND));
















 97     }
 98 
 99     // Utility to convert value to format of expected milisecond value
100     private static String toMillis(int m) {
101         StringBuilder sb = new StringBuilder();
102         if (m < 100) {
103             sb.append('0');
104         }
105         if (m < 10) {
106             sb.append('0');
107         }
108         return sb.append(m).toString();

109     }
110 }
< prev index next >