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 }