Path: blob/master/test/jdk/java/text/Format/DateFormat/Bug7130335.java
41152 views
/*1* Copyright (c) 2012, 2013, 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 7130335 713033526* @summary Make sure that round-trip conversion (format/parse) works27* with old timestamps in Europe/Moscow and with multiple time zone letters.28*/29import java.text.*;30import java.util.*;31import static java.util.GregorianCalendar.*;3233public class Bug7130335 {34private static final TimeZone MOSCOW = TimeZone.getTimeZone("Europe/Moscow");35private static final TimeZone LONDON = TimeZone.getTimeZone("Europe/London");36private static final TimeZone LA = TimeZone.getTimeZone("America/Los_Angeles");37private static final TimeZone[] ZONES = {38MOSCOW, LONDON, LA39};4041public static void main(String[] args) throws Exception {42SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z", Locale.US);43sdf.setTimeZone(MOSCOW);44Calendar cal = new GregorianCalendar(MOSCOW, Locale.US);45cal.clear();46// Try both +03:00 and +02:0047cal.set(1922, SEPTEMBER, 30);48test(sdf, cal);49cal.add(DAY_OF_YEAR, 1);50test(sdf, cal);51cal.set(1991, MARCH, 31);52// in daylight saving time53test(sdf, cal);54cal.add(DAY_OF_YEAR, 1);55test(sdf, cal);56// Try the current timestamp57cal.setTimeInMillis(System.currentTimeMillis());58test(sdf, cal);5960// tests for multiple time zone letters (8000529)61test8000529("yyyy-MM-dd HH:mm:ss.SSS Z (z)");62test8000529("yyyy-MM-dd HH:mm:ss.SSS Z (zzzz)");63test8000529("yyyy-MM-dd HH:mm:ss.SSS z (Z)");64test8000529("yyyy-MM-dd HH:mm:ss.SSS zzzz (Z)");6566}6768private static void test(SimpleDateFormat sdf, Calendar cal) throws Exception {69Date d = cal.getTime();70String f = sdf.format(d);71System.out.println(f);72Date pd = sdf.parse(f);73String p = sdf.format(pd);74if (!d.equals(pd) || !f.equals(p)) {75throw new RuntimeException("format: " + f + ", parse: " + p);76}77}7879private static void test8000529(String fmt) throws Exception {80for (TimeZone tz : ZONES) {81SimpleDateFormat sdf = new SimpleDateFormat(fmt, Locale.US);82sdf.setTimeZone(tz);83Calendar cal = new GregorianCalendar(tz, Locale.US);84cal.clear();85cal.set(2012, JUNE, 22);86test(sdf, cal);87cal.set(2012, DECEMBER, 22);88test(sdf, cal);89cal.setTimeInMillis(System.currentTimeMillis());90test(sdf, cal);91}92}93}949596