Unparseable Date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ganapathidev
    New Member
    • Dec 2007
    • 6

    #1

    Unparseable Date

    Hi,
    can any one solve this problem ..,
    i couldn`t identify why its showing ParseException. .,
    Following the code.....,
    Output:java.tex t.ParseExceptio n: Unparseable date 2008/01/05

    import java.util.*;
    import java.text.*;
    class CompareDates {
    public static void main(String[] args) throws ParseException {

    try
    {


    SimpleDateForma t df = new SimpleDateForma t (" yyyy-MM-dd");
    //System.out.prin tln("it is " + df.format(d1));
    Date d1 = new Date();
    System.out.prin tln("it is " + df.format(d1));
    String s = "2008/01/05";
    //System.out.prin tln(" " + s);
    Date d2 = df.parse(s); // Get Date 2
    System.out.prin tln(" " + df.parse(s));
    String relation;
    if (d1.equals(d2))
    relation = "the same date as";
    //System.out.prin tln(d1 + " is " + relation + " " + d2);}
    else if (d1.before(d2))
    relation = "before";
    //System.out.prin tln(d1 + " is " + relation + " " + d2);
    else
    relation = "after";
    System.out.prin tln(d1 + " is " + relation + " " + d2);

    } catch (ParseException e)
    {
    System.out.prin t(e);
    }

    }

    }
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Originally posted by ganapathidev
    ...
    SimpleDateForma t df = new SimpleDateForma t (" yyyy-MM-dd");
    ...
    String s = "2008/01/05";
    ...
    Date d2 = df.parse(s); // Get Date 2
    I quoted the important part of your source code. Can you see it now?

    Genrally, if you have an error, try to reduce it to the minimum number of lines by deleting all lines that have nothing to do with the problem. You can just do it with deleting line-by-line in a try-and-error style.

    If you still can't see it, here is the solution:
    your date format contains minus signs, therefore you have to separate the data with minus-signs. And it contains a space in front:
    SimpleDateForma t ("_ yyyy-MM-dd");

    If you want to format the given string, you should use:
    SimpleDateForma t ("yyyy/MM/dd");

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      Rather than deleting lines, it's better to comment them out, so that you don't risk forgetting what was writted if you delete whole chunks of code in an attempt to troubleshoot. Only delete if you absolutely know the code is faulty or wrong.

      Comment

      Working...