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


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


source


use DateFormat "yyyy" you will only get the year



0


source


Calendar c = Calendar.getInstance(); 
c.setTime(sdf.parse(date));   // sdf is SimpleDateFormat
System.println(c.get(Calendar.YEAR)));

      

0


source


you can use

Calendar c = Calendar.getInstance(); 
int year = c.get(Calendar.YEAR);

      

you can check here

0


source







All Articles