How to get only year from DateUtils
How can I get only the usable part of a date using DateUtils? I tried to use
DateUtils.FORMAT_SHOW_YEAR
but this seems to even return the month and date.
+3
lokoko
source
to share
4 answers
No need to reinvent the wheel
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String year = sdf.format(new Date());
To take the locale into account, use the SimpleDateFormat ("template", Locale) constructor
SimpleDateFormat sdf = new SimpleDateFormat("yyyy", Locale.getDefault());
+3
Droidman
source
to share
use DateFormat "yyyy" you will only get the year
0
Kapil vats
source
to share
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date)); // sdf is SimpleDateFormat
System.println(c.get(Calendar.YEAR)));
0
Paresh mayani
source
to share
you can use
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
you can check here
0
Chirag Patel
source
to share