9 Replies Latest reply: Apr 5, 2007 11:15 AM by 807606 RSS

    Date.parse fails


      I have a small java program that I use unce a month to sift through a file in a CSV format to produce a report of the data.

      End of Feb07 I used the program with no problems. Since then there have been absolutely no changes to the code whatsoever. Now I try to execute the program on a file for the end of March where the format remains correct and I find that it does not work.

      The bit of code where things fall over looks like this:
      public int TimeDiff(String StartDate, String EndDate){
      final long ONE_HOUR = 60*60*1000L;
      int datediff = (int)((Date.parse(EndDate) - Date.parse(StartDate))/(24*ONE_HOUR));
      return datediff;
      I pass in 2 dates extracted from the CSV file in a string format, parse the dates, calculate the time between each, cast to an int and pass it back.

      Used to work perfect! and literally overnight has stopped...

      I've tried this on several different machines and thouroughly checked that the strings passed into the functions are correct immediately before the line that begins
      int datediff =  (int)
      by writing their values to the console.

      I'm reluctant to try changing the code to a different approach because I know this code always worked before - it still compiles perfectly too.

      The error I get looks like this:

      java.lang.ArrayIndexOutOfBoundsException: 0
           at ScoreCard.DataPull.readfile(DataPull.java:126)
           at ScoreCard.gui.actionPerformed(gui.java:88)
           at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
           at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
           at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
           at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
           at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
           at java.awt.Component.processMouseEvent(Unknown Source)
           at java.awt.Component.processEvent(Unknown Source)
           at java.awt.Container.processEvent(Unknown Source)
           at java.awt.Component.dispatchEventImpl(Unknown Source)
           at java.awt.Container.dispatchEventImpl(Unknown Source)
           at java.awt.Component.dispatchEvent(Unknown Source)
           at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
           at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
           at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
           at java.awt.Container.dispatchEventImpl(Unknown Source)
           at java.awt.Window.dispatchEventImpl(Unknown Source)
           at java.awt.Component.dispatchEvent(Unknown Source)
           at java.awt.EventQueue.dispatchEvent(Unknown Source)
           at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
           at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.run(Unknown Source)

      Anyone have ideas to the cause - has there been some update to the Java environment or Windows XP during the past month that could have caused this??

      Thanks in advance to anyone who can help!


        • 1. Re: Date.parse fails
          You should change your code to use DateFormat.parse.

          Date.parse is deprecated.

          What are the values of StartDate and EndDate when the exception is thrown?
          • 2. Re: Date.parse fails
            Date.parse() is deprecated..

            Use java.util.SimpleDateFormat to specify the format of the input date.
            Then call SimpleDateFormat.parse(String) which returns the Date object...

            May be the following code helps a little.
            import java.text.ParseException;
            import java.text.SimpleDateFormat;
            import java.util.Date;
            public class DateDifference {
                 public static void main(String args[]) {
                      String startDate = "01-10-2006";
                      String endDate = "02-10-2007";
                      Date stDate = null;
                      Date enDate = null;
                      SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                      try {
                           stDate = dateFormat.parse(startDate);
                           enDate = dateFormat.parse(endDate);
                      }catch(ParseException e) {
                           System.out.println("Exception in parsing the date " + e.toString());
                      System.out.println( (enDate.getTime() - stDate.getTime()) / (24*60*60*1000));
            • 3. Re: Date.parse fails
              Btw, the getTime() methods gets the time in milliseconds (since Epoch time Jan 1, 1970) and then find the difference.. and divide by the no of milliseconds for a day........
              • 4. Re: Date.parse fails

                I�ve made extensive use of Calendar and GregorianCalendar to calculate workload of workers of a company.

                Class Date in my opinion is not the best option, I�d try changing the code(I know it�s hard). But GregorianCalendar supplies many methods to work with Dates. It �s powerfull

                refer to Calendar which is a abstract class, then use its subclass(GregregorianCalendar). You can find it in libray "java.util" at javadocs
                • 5. Re: Date.parse fails
                  Calendar and GregorianCalendar are helpful to manipulate the Date objects.

                  For instance comparing two Dates, adding days, hours, or years to a Date...
                  But to calculate the exact difference btw days, you can do just like the same way I explained before....

                  Message was edited by:
                  • 6. Re: Date.parse fails
                    As it stands the first time the program calls this I have to get the current date:

                    EndDate is in the format Thu Apr 05 14:51:56 BST 2007

                    Date date = new Date();
                         String date2 = String.valueOf(date);
                    Against StartDate in the format 03/20/2007

                    Thereafter the input file uses a combination of formats either like:
                    1/24/2007 5:49:07 AM

                    Unfortunately I have no control over the format of the input file, but until now Date.parse has been able to cope with this complication.

                    Your comment about Date.parse is interesting, I shall try sub'ing that for DateFormat.parse and let you know.

                    Thankyou for your help, and to everyone else also. Please bear with me while I try all this out :O)


                    • 7. Re: Date.parse fails
                      Hello saracgi

                      Thankyou for your help!

                      I have taken your suggested code and dropped it into place with mixed success...

                      Initially I have the problem like this:

                      Exception in parsing the date java.text.ParseException: Unparseable date: "23/3/2007"
                           at ScoreCard.DataPull.TimeDiff(DataPull.java:529)

                      Changing my input to your seggested format of 23-03-2007 it gets past that an now sticks on trying to parse another date. output like this:

                      Exception in parsing the date java.text.ParseException: Unparseable date: "Thu Apr 05 16:04:51 BST 2007"
                           at ScoreCard.DataPull.TimeDiff(DataPull.java:529)

                      Unfortunately this solution where the program requires everything to be in the simple format is going to burn many calories changing the format of the input file to dd-mm-yyyy everywhere.

                      Similar to my previous approach using date.parse this was fairly easy to drop in place, but does not seem to be able to cope with the ambiguity of the input format. :(

                      If you know of any way to tweak this dateFormat.parse to do this, that would be great, but I suspect it wont be that simple.

                      So onwards with the other suggestions, thanks again for your help.


                      • 8. Re: Date.parse fails
                        Hi Again,

                        I understand that this GeorgianCalendar may be the way to go, and as for doing it in the way whay you explained before it should be fairly easy to drop this in place.

                        I'm reading spec for this online now

                        Just one possibly stupid question:

                        How do you read the date (the string in the csv file) into a variable?


                        • 9. Re: Date.parse fails
                          the only was to read a string into a date is via DateFormat.parse(). There is a flag setLenient(true); that will attempt to parse the date leniently, if the format is close. Dates in java have no format, it is just a number of millis since Jan 1, 1970 00:00:00, so when you have
                          Date date2 = new Date()
                          String dateString = date2.toString();
                          That is just using the default date display format from your system. It would be advisable for you to change that line to also use a DateFormat, like so
                          Date date2 = new Date();
                          DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); // or whatever format you choose
                          String dateString = df.format(date2);
                          Hope that helps