Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8261009

SimpleDateFormat parse and format mismatch

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10

      A DESCRIPTION OF THE PROBLEM :
      the format method produces an unexpected result

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      When we parse the string "2015-03-29 02:00:00" with the time zone Europe/Paris, with SimpleDateFormat and we format the date again, we do not get the input string.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      input:2015-03-29 02:00:00
      Sun Mar 29 03:00:00 CEST 2015
      Sun Mar 29 03:00:00 CEST 2015
      output:2015-03-29 02:00:00
      ACTUAL -
      input:2015-03-29 02:00:00
      Sun Mar 29 03:00:00 CEST 2015
      Sun Mar 29 03:00:00 CEST 2015
      output:2015-03-29 03:00:00

      ---------- BEGIN SOURCE ----------
      import java.text.SimpleDateFormat;
      import java.util.Date;
      import java.util.TimeZone;

      public class TestSimpleDateFormat{
          static SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

           public static void main(String []args){
               FORMATTER.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
               try {
                  String inputString = "2015-03-29 02:00:00";
                  System.out.println("input:" + inputString);
                  Date date = FORMATTER.parse(inputString);
                  System.out.println(date);
                  String outputString = FORMATTER.format(date);
                  System.out.println(date);
                  System.out.println("output:" + outputString);
               }
               catch (Exception e)
               {
                   
               }
               
           }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            tongwan Andrew Wang
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: