I've got a simple method that should get the current date, put it into a certain format and then return it as a String. Up to this point it's been fine (last tried it on about 31st Jan) but for some reason when I tried it today it returns the String "2013-02-43".
Now obviously there aren't 43 days in February and I have no idea why it's returning this. I've searched everywhere I can for a solution but none of them seem to fit the specific problem I am having. Here is the code:
public String getDate(){
DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD");
Date date = new Date();
return dateFormat.format(date);
}
Just for the record I've tried using Calendar.getInstance() etc. with the same result. Interestingly when I try
get(Calendar.DAY_OF_MONTH)
it comes back with 12, so somewhere the numbers are right but something's going wrong in between.
Thanks
DD
means Day of Year, while dd
means Day of Month. Also, Y
means Week Year, while y
means Year. You want yyyy-MM-dd
(case-sensitive).
public String getDate(){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return dateFormat.format(date);
}
Capital D in a DateFormat
is date in year. You want date in month:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With