0

I'm trying to parse String in format MMM-dd-yyyy to Timestamp. I'm doing this like below:

String dateto = "Dec-01-2013";
try{
    DateFormat datetoDF = new SimpleDateFormat("MMM-dd-yyyy");
    Date datetoD = (Date)datetoDF.parse(dateto);
    datetoTS = new Timestamp(datetoD.getTime());
}
catch(ParseException e)
{
    log.error("RegisteredUserDataProvider:getFilteredUsers:ParseException: " + e.getMessage());
    String stackTrace = ExceptionUtils.getStackTrace(e);
    log.error(stackTrace);
}

Why it works only with March (for example Mar-01-2013)? When I give any other date with other month I get

java.text.ParseException: Unparseable date: "Dec-01-2013"

I've checked it for set of variables with every month: "Dec-01-2013", "Nov-01-2013", "Oct-01-2013", "Sep-01-2013", "Aug-01-2013", "Jul-01-2013", "Jun-01-2013", "May-01-2013", "Apr-01-2013", "Feb-01-2013", "Jan-01-2013" and "Mar-04-2013". It works only for Mar. Any idea why?

3
  • 3
    worked for me. Your exact code. sysout(datetoTS) gave 2013-12-01 00:00:00.0 Commented Mar 7, 2013 at 11:21
  • Strange. So I have to dig deeper. Commented Mar 7, 2013 at 11:27
  • 1
    @R.J Its his Locale causing the issue. Your default Locale may be English. Commented Mar 7, 2013 at 11:29

3 Answers 3

4

Looks like your Default Locale is different.

DateFormat datetoDF = new SimpleDateFormat("MMM-dd-yyyy", Locale.ENGLISH);

Your default Locale may not recognize the words "Jan", "Feb" etc.

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

Comments

1

All about Local or you are passing date Dec -01-2013 with space. Provide proper date without space such as Dec-01-2013,and try to print Locale.getDefault() if your default is set something like Korea then definitely you will get this exception then try to use like

DateFormat datetoDF = new SimpleDateFormat("MMM-dd-yyyy",Locale.ENGLISH);

Comments

0

You can use this.

 String dateto = "Dec-01-2013";
 Date datetoD = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(dateto );
 datetoTS = new Timestamp(datetoD.getTime());

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.