0

I have a date in the format 16-2-2012.

var k = getTomorrow('16-02-2012',1);
var myTime = k.getDate()+'-'+(k.getMonth()+1)+'-'+k.getFullYear();

Now I want to parse mytime to a date object. I have tried with

SimpleDateFormat parserSDF=new SimpleDateFormat("dd-M-yyyy");
Date dtstr=parserSDF.parse(myTime);

But its not working. Is there any way around?

4
  • 2
    Why is that not working? what do you get? Commented Feb 19, 2012 at 7:37
  • Try DateFormat instead of SimpleDateFormat. Commented Feb 19, 2012 at 7:40
  • Is this for java? Javascript? Or both!? Commented Feb 19, 2012 at 8:32
  • Sometimes the date format changes like to Thu Feb 23 2012 00:00:00 GMT+0530 (IST) then its showing errors Commented Feb 19, 2012 at 11:09

2 Answers 2

5

dd-MM-yyyy is the proper format - M means only one digit for the months

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

2 Comments

what if the format is Thu Feb 23 2012 00:00:00 GMT+0530 (IST)
then you need a different date format. SimpleDateFormat documentation has a table that shows which is which.
0

If variable myTime is String type then print it other wise convert it to String. if myTime = "16-03-2012" follow the format then you can use the code bellow.

try {
String myTime = "16-03-2012";
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("dd-MM-yyyy");
date = (Date) formatter.parse(myTime);
System.out.println("Today is " + date);
} catch (ParseException e) {
System.out.println("Exception :" + e);
}

1 Comment

if the format is Thu Feb 23 2012 00:00:00 GMT+0530 (IST) then?

Your Answer

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