changeset 8863:67d742c75971

8028185: XMLFormatter.format emits incorrect year Summary: Fixes a regression where the year in the date was increased by 1900. Reviewed-by: alanb, mchung
author dfuchs
date Tue, 19 Nov 2013 20:10:58 +0100
parents 3f47e393e1dd
children f496565c4eec
files src/share/classes/java/util/logging/XMLFormatter.java test/java/util/logging/XMLFormatterDate.java
diffstat 2 files changed, 103 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/logging/XMLFormatter.java	Tue Nov 19 13:20:07 2013 -0500
+++ b/src/share/classes/java/util/logging/XMLFormatter.java	Tue Nov 19 20:10:58 2013 +0100
@@ -58,7 +58,7 @@
     private void appendISO8601(StringBuilder sb, long millis) {
         GregorianCalendar cal = new GregorianCalendar();
         cal.setTimeInMillis(millis);
-        sb.append(cal.get(Calendar.YEAR) + 1900);
+        sb.append(cal.get(Calendar.YEAR));
         sb.append('-');
         a2(sb, cal.get(Calendar.MONTH) + 1);
         sb.append('-');
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/util/logging/XMLFormatterDate.java	Tue Nov 19 20:10:58 2013 +0100
@@ -0,0 +1,102 @@
+
+/*
+ * Copyright (c) 2013, 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.
+ */
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.XMLFormatter;
+
+/**
+ * @test
+ * @bug 8028185
+ * @summary XMLFormatter.format emits incorrect year (year + 1900)
+ * @author dfuchs
+ */
+public class XMLFormatterDate {
+
+    /**
+     * Before the fix, JDK8 prints: {@code
+     * <record>
+     *   <date>3913-11-18T17:35:40</date>
+     *   <millis>1384792540403</millis>
+     *   <sequence>0</sequence>
+     *   <level>INFO</level>
+     *   <thread>1</thread>
+     *   <message>test</message>
+     * </record>
+     * }
+     * After the fix, it should print: {@code
+     * <record>
+     *   <date>2013-11-18T17:35:40</date>
+     *   <millis>1384792696519</millis>
+     *   <sequence>0</sequence>
+     *   <level>INFO</level>
+     *   <thread>1</thread>
+     *   <message>test</message>
+     * </record>
+     * }
+     * @param args the command line arguments
+     */
+    public static void main(String[] args) {
+        Locale locale = Locale.getDefault();
+        try {
+            Locale.setDefault(Locale.ENGLISH);
+
+            final GregorianCalendar cal1 = new GregorianCalendar();
+            final int year1 = cal1.get(Calendar.YEAR);
+
+            LogRecord record = new LogRecord(Level.INFO, "test");
+            XMLFormatter formatter = new XMLFormatter();
+            final String formatted = formatter.format(record);
+            System.out.println(formatted);
+
+            final GregorianCalendar cal2 = new GregorianCalendar();
+            final int year2 = cal2.get(Calendar.YEAR);
+            if (year2 < 1900) {
+                throw new Error("Invalid system year: " + year2);
+            }
+
+            StringBuilder buf2 = new StringBuilder()
+                    .append("<date>").append(year2).append("-");
+            if (!formatted.contains(buf2.toString())) {
+                StringBuilder buf1 = new StringBuilder()
+                        .append("<date>").append(year1).append("-");
+                if (formatted.contains(buf1)
+                        && year2 == year1 + 1
+                        && cal2.get(Calendar.MONTH) == Calendar.JANUARY
+                        && cal2.get(Calendar.DAY_OF_MONTH) == 1) {
+                    // Oh! The year just switched in the midst of the test...
+                    System.out.println("Happy new year!");
+                } else {
+                    throw new Error("Expected year " + year2
+                            + " not found in log:\n" + formatted);
+                }
+            }
+        } finally {
+            Locale.setDefault(locale);
+        }
+    }
+
+}