How to get the current year in Android?

I tried

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

but it gives me a compile time error which

The non-static get (int) method cannot refer to a static context.

I call this method from the observable call method.

 Observable.combineLatest(ob1 ob2, ob3, new Func3<String, String, String, Boolean>() { @Override public Boolean call(String a, String b, String c) {... 

I also saw (new Date()).getYear(); but it is out of date.

+8
java android
source share
7 answers

Because you need to create an instance first.

try it

 Calendar.getInstance().get(Calendar.YEAR); 

and you are good to go.

+38
source share

Yes, you get an error because it is not a static method. First you need to instantiate the Calendar class.
i.e.

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

and here you get the year, the current year!

+1
source share
 // get current yearใ€month and day Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); // get current year millis Time time = new Time(Time.TIMEZONE_UTC); calendar.set(year, 0, 0, 0, 0, 0); long year = calendar.getTimeInMillis(); time.set(year); year = time.toMillis(true); 
+1
source share

TL; DR

 Year.now() .getValue() 

java.time

Other answers use nasty old time classes such as Calendar. They are superseded by java.time classes. For older versions of Android, see ThreeTen-Backport and ThreeTenABP Projects.

Year class

Instead of passing only integers to represent the year, go around objects. Namely, the Year class.

To obtain the current year, a time zone is required. At any given moment, the date changes around the world by zone. Thus, it is possible that Pacific/Auckland will be in 2018, and America/Montreal - in 2017 at the same time.

It is better to explicitly convey the desired / expected zone. If you did not specify, you implicitly get the current JVM time zone. This default value can change at any time during runtime, so it is not reliable.

 ZoneId z = ZoneId.now( "Asia/Kolkata" ) ; Year y = Year.now( z ) ; 

When you need an integer, extract the value.

 int yearNumber = y.getValue() ; 

About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

  • Java SE 8 , Java SE 9 , and then
    • Built in.
    • Part of the standard Java API with integrated implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the functionality of java.time is ported back to Java 6 and 7 in ThreeTen-Backport .
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) specifically for Android.
    • See How to use ThreeTenABP ....
+1
source share

you should use

Calendar.getInstance().get(Calendar.YEAR) ;

0
source share

Have you tried the code below

Calendar c = Calendar.getInstance ();

 int seconds = c.get(Calendar.SECOND); int hour = c.get(Calendar.HOUR_OF_DAY); // IF YOU USE HOUR IT WILL GIVE 12 HOUR USE HOUR_OF_DAY TO GET 24 HOUR FORMAT int minutes = c.get(Calendar.MINUTE); int date = c.get(Calendar.DATE); int month = c.get(Calendar.MONTH) + 1; // in java month starts from 0 not from 1 so for december 11+1 = 12 int year = c.get(Calendar.YEAR); 
0
source share

you can use Calendar.getInstance() .get(Calendar.YEAR ) also for Kotlin language purposes

-one
source share

All Articles