3 Replies Latest reply: Apr 26, 2013 4:31 PM by alekoc RSS

    01.01.0000 date parse and format

    alekoc
      Could somebody explain is wrong here?
      SimpleDateFormat frm = new SimpleDateFormat("yyyyMMddHHmmss");  // formatter
      frm.setTimeZone(TimeZone.getTimeZone("UTC"));   // UTC time zone
      // --- No problem for the next code ---
      String inDate = "19700101000000";  // 01.01.1970 input date
      long stamp;
      try {
          stamp = frm.parse(inDate).getTime();   // input date in long
      } catch (ParseException ex) {
          System.out.println("Interruption");
          stamp = 0;
      }
      String outDate = frm.format(stamp);  // output date 
      System.out.println("inDate=>" + inDate + "_longDate=>" + stamp + "_outDate=>" + outDate);
      
      // --- No idea why next code is working wrongly ---
      inDate = "00000101000000";  // 01.01.0000 input date
      try {
          stamp = frm.parse(inDate).getTime();   // input date in long
      } catch (ParseException ex) {
          System.out.println("Interruption");
          stamp = 0;
      }
      outDate = frm.format(stamp);  // output date 
      System.out.println("inDate=>" + inDate + "_longDate=>" + stamp + "_outDate=>" + outDate);
      I try to parse and format the time stamp 01.01.0000 00:00:00. On the finish I have 01.01.0001 instead of the correct 01.01.0000
      inDate=>19700101000000_longDate=>0_outDate=>19700101000000
      inDate=>00000101000000_longDate=>-62167392000000_outDate=>00010101000000
        • 1. Re: 01.01.0000 date parse and format
          802158
          You are calling format with a long parameter. The only format that matches that signature that I can see would be Format.format(Object) assuming autoboxing is changing the long to a Long.
          Format.format(Object) eventually calls DateFormat.format(Object, StringBuffer, FieldPosition). I have never used this method so I am not sure what it does.
          Was your intention to actually call this method?

          See the following example:
          System.out.println("Test 1: " + frm.format(new Long(0l), new StringBuffer(), new FieldPosition(0)));   
          System.out.println("Test 2: " + frm.format(new Long(-62167392000000l), new StringBuffer(), new FieldPosition(0)));   
          
          Test 1: 19700101000000
          Test 2: 00010101000000
          Edited by: jbish on Apr 26, 2013 12:43 PM

          Edited by: jbish on Apr 26, 2013 1:09 PM
          • 2. Re: 01.01.0000 date parse and format
            doremifasollatido
            There is no year 0.
            1 A.D. immediately follows 1 B.C.

            Call setLenient(false) on your SimpleDateFormat, and you will see that inputting the year 0 will throw a ParseException.

            0 year should leniently be interpreted as 1 B.C. Include the era in your SimpleDateFormat to see if that's what it is doing:
            SimpleDateFormat eraOnly = new SimpleDateFormat("G");
            • 3. Re: 01.01.0000 date parse and format
              alekoc
              Thanks for the comments