How do i get only the year part of a date using DateUtils ? I tried using
DateUtils.FORMAT_SHOW_YEAR
but this seems to return even the month and date.
How do i get only the year part of a date using DateUtils ? I tried using
DateUtils.FORMAT_SHOW_YEAR
but this seems to return even the month and date.
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 constructor SimpleDateFormat("template", Locale)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy", Locale.getDefault());
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date)); // sdf is SimpleDateFormat
System.println(c.get(Calendar.YEAR)));
you can use
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
you can check here
I've used "YearOf". Seems a bit shorter. Hope it helps:
int currentyear = Dateutils::YearOf(Now());