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

Incorrect date formatting - randomly

XMLWordPrintable



      Name: rm29839 Date: 11/25/97


      I have a multi-threaded app which is creating dates using a simpledateformat.
      Occasionally 2 extra characters appear in my date format. Usually they are "00", and
      can appear anywhere in the formatted string. Calling format again with the same date object
      corrects the string. I can only reproduce it in a multi-threaded situation, and it occurs about
      1 time out of 500. Here is a snippet of code where I am creating the date. I check in here to make sure tha thte
      date is correct before returning the result.

      package p2k;
      import java.util.*;
      import java.text.*;

      public class TimeManager {
        protected SimpleDateFormat formatter;
        protected Calendar cal;
        protected TimeZone tz;
        public TimeManager() {
          this.formatter = new SimpleDateFormat("HH:mm:ss M-dd-yyyy");
          this.tz = TimeZone.getDefault();
          // Debug.p("In timezone: " +tz.getID());
          
          this.cal = Calendar.getInstance(tz);
          this.formatter.setCalendar(this.cal);
          this.formatter.setTimeZone(tz);
          // Debug.p("Local Time: "+cal.getTime().toString());
          // Debug.p("Formatted time: "+this.getTime());
        }

        public String getTime() {
          cal = Calendar.getInstance(tz);
          formatter.setCalendar(cal);
          formatter.setTimeZone(tz);
          Date d = cal.getTime();
          String dt = formatter.format(d);
          int x = 0;
          while(dt.length() != 19 && x<5) {
            x++;
            Debug.p("TimeLength Error #"+x+" -> "+dt);
            dt = formatter.format(d);
          }
        
          return(dt);
        }

      }


      Here is some sample output:

      tail -f debuglog.log | egrep TimeLength
      Sun Nov 23 16:46:07 EST 1997 TimeLength Error #1 -> 16:0046:07 11-23-1997
      Sun Nov 23 16:48:00 EST 1997 TimeLength Error #1 -> 0016:48:00 11-23-1997
      Sun Nov 23 16:48:18 EST 1997 TimeLength Error #1 -> 16:48.97:18 11-23-1997
      Sun Nov 23 16:49:40 EST 1997 TimeLength Error #1 -> 16:0049:40 11-23-1997
      Sun Nov 23 16:53:33 EST 1997 TimeLength Error #1 -> 16:0053:33 11-23-1997
      Sun Nov 23 17:01:22 EST 1997 TimeLength Error #1 -> 0017:01:22 11-23-1997
      Sun Nov 23 17:02:29 EST 1997 TimeLength Error #1 -> 17:02:29 11-29.97-1997
      Sun Nov 23 17:03:26 EST 1997 TimeLength Error #1 -> 17:03:26 0011-23-1997
      Sun Nov 23 17:11:04 EST 1997 TimeLength Error #1 -> 17:11:0004 11-23-1997
      Sun Nov 23 17:15:06 EST 1997 TimeLength Error #1 -> 17:0015:06 11-23-1997
      (Review ID: 20798)
      ======================================================================

            aliusunw Alan Liu (Inactive)
            rmandelsunw Ronan Mandel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: