0

I have a program which reads a Dateformat from user once and for all at the beginning of program such as yyyy/MM/dd HH:mm:ss a

Later on the program parses a File and use the Dateformat for all dates in the file. But recently I got a file, where 99% of the dates were 2014/09/01 12:00:04 AM So The user can input yyyy/MM/dd HH:mm:ss a.

However one date in that file is simply 2014/09/01 where the date format yyyy/MM/dd HH:mm:ss a fails.

Why can't yyyy/MM/dd HH:mm:ss a format shorter dates such as in the format yyyy/MM/dd.

What I want is that the java program use the format yyyy/MM/dd HH:mm:ss a to parse the following two dates below:

  1. 2014/09/01 12:00:04 AM
  2. 2014/09/01

Thanks

dds
  • 2,335
  • 1
  • 31
  • 45
Noman K
  • 277
  • 1
  • 5
  • 15
  • How are you parsing the date ? Could you share the code ? – bluelurker Dec 15 '15 at 05:59
  • SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat(USERs INPUT); // "yyyy/MM/dd HH:mm:ss a Later on File is read Buffer Reader Loop d1 = DATE_TIME_FORMAT.parse(current Date in files row); // could be "2014/09/01 12:00:04 AM" Or just "2014/09/01" Variables Binded End Loop – Noman K Dec 15 '15 at 06:02
  • @NomanK Please edit your Question with more info such as example code rather than post as a comment. – Basil Bourque Dec 15 '15 at 07:56

1 Answers1

1

You have to use two formats. Check the example below

DateFormat df = new DateFormat() {
      static final String FORMAT1 = "yyyy/MM/dd HH:mm:ss";
      static final String FORMAT2 = "yyyy/MM/dd";
      final SimpleDateFormat sdf1 = new SimpleDateFormat(FORMAT1);
      final SimpleDateFormat sdf2 = new SimpleDateFormat(FORMAT2);

      @Override
      public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
          return null;
      }

      @Override
      public Date parse(String source, ParsePosition pos) {
           if (source.length() - pos.getIndex() == FORMAT1.length())
                 return sdf1.parse(source, pos);
           return sdf2.parse(source, pos);
      }
};
System.out.println(df.parse("2014/09/01 12:00:04 AM"));
System.out.println(df.parse("2014/09/01"));
  • Thanks Thusitia, But The Format comes from the user and I cannot change the code at the moment for prod issues, I am searching for a generic DateFormat :) – Noman K Dec 15 '15 at 06:10
  • in parse function you have declared two parameters in function one is String source and one is ParsePosition pos but when you are calling this function you are passing only one variable to the function? – Sanjeev Dec 15 '15 at 06:39
  • @L-X that is an overloaded parse method. There are 2 parse methods – Thusitha Thilina Dayaratne Dec 15 '15 at 06:48
  • @NomanK hm then can you do http://stackoverflow.com/questions/6653223/how-to-parse-ambiguous-string-into-date – Thusitha Thilina Dayaratne Dec 15 '15 at 06:50
  • @NomanK This Answer gives you what you asked for, a DateFormat that handles either of two formats. If you are asking for a totally generic DateFormat to handle any and every possibility, that is impossible as you would have learned if you’d searched StackOverflow before posting. – Basil Bourque Dec 15 '15 at 08:02