Path: blob/master/test/jdk/java/util/Calendar/bug4243802.java
41149 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 424380226* @summary confirm that Calendar.setTimeInMillis() and27* getTimeInMillis() can be called from a user program. (They used to28* be protected methods.)29* @library /java/text/testlib30*/3132import java.util.*;3334public class bug4243802 extends IntlTest {3536public static void main(String[] args) throws Exception {37new bug4243802().run(args);38}3940/**41* 4243802: RFE: need way to set the date of a calendar without a Date object42*/43public void Test4243802() {44TimeZone saveZone = TimeZone.getDefault();45Locale saveLocale = Locale.getDefault();46try {47Locale.setDefault(Locale.US);48TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));4950Calendar cal1 = Calendar.getInstance();51Calendar cal2 = Calendar.getInstance();5253cal1.clear();54cal2.clear();55cal1.set(2001, Calendar.JANUARY, 25, 1, 23, 45);56cal2.setTimeInMillis(cal1.getTimeInMillis());57if ((cal2.get(Calendar.YEAR) != 2001) ||58(cal2.get(Calendar.MONTH) != Calendar.JANUARY) ||59(cal2.get(Calendar.DAY_OF_MONTH) != 25) ||60(cal2.get(Calendar.HOUR_OF_DAY) != 1) ||61(cal2.get(Calendar.MINUTE) != 23) ||62(cal2.get(Calendar.SECOND) != 45) ||63(cal2.get(Calendar.MILLISECOND) != 0)) {64errln("Failed: expected 1/25/2001 1:23:45.000" +65", got " + (cal2.get(Calendar.MONTH)+1) + "/" +66cal2.get(Calendar.DAY_OF_MONTH) +"/" +67cal2.get(Calendar.YEAR) + " " +68cal2.get(Calendar.HOUR_OF_DAY) + ":" +69cal2.get(Calendar.MINUTE) + ":" +70cal2.get(Calendar.SECOND) + "." +71toMillis(cal2.get(Calendar.MILLISECOND)));72}73logln("Passed.");74}75finally {76Locale.setDefault(saveLocale);77TimeZone.setDefault(saveZone);78}79}8081private String toMillis(int m) {82StringBuffer sb = new StringBuffer();83if (m < 100) {84sb.append('0');85}86if (m < 10) {87sb.append('0');88}89sb.append(m);90return sb.toString();91}92}939495