Reputation: 5654
I am trying to get the current date and format it however i am getting an invalid month of 59. Under is the code
Code
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Date todayDate = new Date();
String formatDate = df.format(todayDate);
Output is 2013-59-07
Upvotes: 3
Views: 216
Reputation: 340060
LocalDate
.now(
ZoneId.of( "America/Edmonton" )
)
.toString()
2025-01-23
For a date-only value, use LocalDate
class.
Your desired format of YYYY-MM-DD complies with the ISO 8601 standard used by default in the java.time classes for generating/parsing text. So no need to specify a formatting pattern. You can parse directly.
LocalDate ld = LocalDate.parse( "2025-01-23" ) ;
And you can generate text with a simple call to toString
.
LocalDate today = LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) ;
String output = today.toString() ;
2025-01-23
Upvotes: 1
Reputation: 4033
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String dt = dateFormat.format(cal.getTime());
Use MM instead of mm.
Upvotes: 0
Reputation: 13344
You need "MM"
for month. "mm"
is for minutes.
See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Upvotes: 2
Reputation: 12020
You need to use capital MM
and not mm
. Lower case 'mm' is for minutes and not for months.
So, your code would be :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Upvotes: 2
Reputation: 85789
Month is retrieved by MM
, not mm
.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Refer to SimpleDateFormat
JavaDoc:
M Month in year
Upvotes: 1
Reputation: 178313
You have used mm
which means minutes. Use capital MM
instead.
You can find all date and time pattern symbols on the SimpleDateFormat
Javadoc page.
Upvotes: 4