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
android date-format android-date


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 to share


use DateFormat "yyyy" you will only get the year



0


source to share


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

      

0


source to share


you can use

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

      

you can check here

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics