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?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
AndrewVP
  • 73
  • 1
  • 5
  • 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... – Eric B. May 15 '12 at 20:00

2 Answers2

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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. – AndrewVP May 15 '12 at 18:02
  • In simpleDateFormat ,it always return wrong month . What is reason of it ? – Anand Savjani Sep 28 '15 at 12:46
  • @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. – Jon Skeet Sep 28 '15 at 12:47
  • @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 ? – Anand Savjani Sep 28 '15 at 12:53
  • 1
    @AnandSavjani: I've already suggested what you should do: "ask a new question with a short but complete program demonstrating the problem". – Jon Skeet Sep 28 '15 at 12:56
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;
}
M.N
  • 11
  • 5