7

I need to parse a date from input string using date pattern "yyyy-MM-dd", and if date will come in any other format, throw an error.

This is my piece of code where I parse the date:

private void validateDate() throws MyException {
  Date parsedDate;
  String DATE_FORMAT = "yyyy-MM-dd";
  try{
    parsedDate = new SimpleDateFormat(DATE_FORMAT).parse(getMyDate());
    System.out.println(parsedDate);
  } catch (ParseException e) {
    throw new MyException(“Error occurred while processing date:” + getMyDate());
  }

}

When I have string like "2011-06-12" as input in myDate I will get output "Thu Sep 29 00:00:00 EEST 2011", which is good.

When I sent an incorrect string like “2011-0612”, I’m getting error as expected.

Problems start when I’m trying to pass a string which still has two “hyphens”, but number of digits is wrong. Example:

input string “2011-06-1211” result "Tue Sep 23 00:00:00 EEST 2014".

input string “2011-1106-12” result "Mon Feb 12 00:00:00 EET 2103".

I can't change input format of string date.

How I can avoid it?

1
  • 2
    I'm not sure how "2011-06-12" as input in myDate I will get output "Thu Sep 29 00:00:00 EEST 2011" is good. You should be getting June 12 as output - not Sep 29... Commented May 15, 2012 at 20:00

2 Answers 2

10

Have you tried calling setLenient(false) on your SimpleDateFormat?

import java.util.*;
import java.text.*;

public class Test {

    public static void main(String[] args) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setLenient(false);
        Date date = format.parse("2011-06-1211"); // Throws...
        System.out.println(date);
    }
}

Note that I'd also suggest setting the time zone and locale of your SimpleDateFormat. (Alternatively, use Joda Time instead...)

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

5 Comments

Thank you for your answer. I will run the tests and give you the feedback. But at the moment it looks like problem is solved.
In simpleDateFormat ,it always return wrong month . What is reason of it ?
@AnandSavjani: No it doesn't. I can't tell what you're doing wrong with so little information, but SimpleDateFormat works pretty reasonably. If you think it's broken, I suggest you ask a new question with a short but complete program demonstrating the problem.
@JonSkeet : In my case i am try to parsing sep 28,2015 using simpleDateFormat but it returns me 2015-22-28 as date. Please suggest me what i do for it ?
@AnandSavjani: I've already suggested what you should do: "ask a new question with a short but complete program demonstrating the problem".
0

Same issue ,, I noticed that when I use this code directly in the main method, it returns the correct date and when I use the same code through a separated method it returns incorrect date

String string = "03/01/2015";
DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date1 = format.parse(string);
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(date));

code within a function that returns incorrect date

public Date isValidDate(String dateToValidate, String dateFormat){

  if(dateToValidate == null){
     return null;
  }

  DateFormat format = new SimpleDateFormat(dateFormat,Locale.ENGLISH);
  //format.setLenient(false);

  Date date;
  try {

      date = format.parse(dateToValidate);

  } catch (ParseException e) {

      e.printStackTrace();
      return null;
  }

  return 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.