3

I am trying to parse a String to Date Object.Previously I used parse(String) definition but a date like 4/1/2004 2:00:00asasasas was getting correctly parsed which was wrong.So i used the ParsePosition signature parse(String,parsePosition) and checked if the index is not equal to the length then its an invalid Date.But above logic fails for a String as "4/1/2004 2:00:00 AM.Although it's a valid date but due to index and length logic it says invalid date when I try to parse it in "M/dd/YYYY" format. the parser parses the correct date part and does not take whole String into consideration. Any way to achieve it ? formatStr can be any format pattern.

Please advise.

public static void main(String[] args) {
        String formatStr="M/dd/YYYY";
    SimpleDateFormat sd = new SimpleDateFormat(formatStr, Locale.getDefault());
        String str = "4/1/2004 2:00:00 AM";

        ParsePosition pp1 = new ParsePosition(0);

        Date retDate = sd.parse(str, pp1);
        if(retDate==null ||pp1.getIndex()!=str.length() ){
            System.out.println("I have a invalid Date");

        }

    }
3
  • 1
    " it says invalid date when I try to parse it in "M/dd/YYYY" format" - well of course it does, because that format doesn't include the time, whereas your string value does. You want yyyy rather than YYYY mind you... Commented May 15, 2015 at 12:06
  • Your format should be M/d/y h:m:s a Commented May 15, 2015 at 12:09
  • Thanks for the reply.Current API implementation accepts the String Text and the Date format pattern(can be M/d/y h:m:s a or M/dd/YYYY) .It depends on user as to what date format he wants . Current API parses the string 4/1/2004 2:00:00 AM perfectly using M/dd/YYYY pattern.So I was looking for a way that is Backward compatible which means it passes for 4/1/2004 2:00:00 AM but fails for 4/1/2004 2:00:00 abcd using the pattern "M/dd/YYYY" .I know its kind of lame but any direction would be helpful Commented May 15, 2015 at 12:30

2 Answers 2

1

Use:

public static void main(String[] args) {
        String formatStr="MM/dd/yyyy hh:mm:ss aa";
    SimpleDateFormat sd = new SimpleDateFormat(formatStr, Locale.getDefault());
        String str = "04/01/2004 02:00:00 AM";

        ParsePosition pp1 = new ParsePosition(0);

        Date retDate = sd.parse(str, pp1);
        if(retDate==null ||pp1.getIndex()!=str.length() ){
            System.out.println("I have a invalid Date");

        }

    }

Correct the formatStr to include hour in am/pm and year in minus. Also use two digits for month and day.

If you want to parse with abcd instead of AM / PM you can use regular expression split.

public static void main(String[] args) {
    String formatStr="M/dd/yyyy h:m:s";

    SimpleDateFormat sd = new SimpleDateFormat(formatStr, Locale.getDefault());
        String str = "04/01/2004 2:00:00 abcd";

        String[] strDate = str.split(" .[a-zA-Z]");

        ParsePosition pp1 = new ParsePosition(0);

        Date retDate = sd.parse(strDate[0], pp1);
        if(retDate==null ||pp1.getIndex()!=strDate[0].length() ){
            System.out.println("I have a invalid Date");

        }
        else
        {
           System.out.println("I have a valid Date");

        }

    }

See: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I know that the correct date format would be MM/dd/yyyy hh:mm:ss aa But my current implementation parses 04/01/2004 02:00:00 AM successfully using the M/dd/YYYY pattern. try String formatStr="M/dd/YYYY"; String str = "4/1/2004 2:00:00 AM"; SimpleDateFormat sd = new SimpleDateFormat(formatStr, Locale.getDefault()); Date retDate = sd.parse(str)
So, you want this work for : String str = "4/1/2004 2:00:00 abcd" ?
0

Two ways you can fix your problem.

Parse just the date out of the String and use your format

public static void main(String[] args) throws Exception {
    String formatStr = "MM/dd/yyyy";
    SimpleDateFormat sd = new SimpleDateFormat(formatStr, Locale.getDefault());
    String str = "4/1/2004 2:00:00 AM";

    ParsePosition pp1 = new ParsePosition(0);

    String justDate = str.substring(0, str.indexOf(" "));
    Date retDate = sd.parse(justDate, pp1);
    if (retDate == null || pp1.getIndex() != justDate.length()) {
        System.out.println("I have a invalid Date");
    } else {
        System.out.println("I have a valid Date");
    }
}

Or use the correct format for the String you're testing

public static void main(String[] args) throws Exception {
    String formatStr = "MM/dd/yyyy hh:mm:ss aa";
    SimpleDateFormat sd = new SimpleDateFormat(formatStr, Locale.getDefault());
    String str = "4/1/2004 2:00:00 AM";

    ParsePosition pp1 = new ParsePosition(0);

    Date retDate = sd.parse(str, pp1);
    if (retDate == null || pp1.getIndex() != str.length()) {
        System.out.println("I have a invalid Date");
    } else {
        System.out.println("I have a valid Date");
    }
}

Results:

I have a valid Date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.