54

I tried

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

but it is giving me compile time error that

Non-static method 'get(int)' cannot be referenced from a static context.

I am calling this method from call method of observable.

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

I had also seen (new Date()).getYear(); but it is deprecated.

Shunan
  • 2,986
  • 6
  • 26
  • 47
  • FYI, `Date` & `Calendar` are both legacy as of the adoption of JSR 310 years ago. Supplanted by the modern *java.time* classes. For early Android, see the *ThreeTen-Backport* library and the *ThreeTenABP* wrapper. – Basil Bourque Dec 05 '19 at 11:49

7 Answers7

158

Because you need to create an instance first.

try this

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

and you are good to go.

dsncode
  • 2,214
  • 2
  • 22
  • 34
12

Yeah, you get an error because this is not a static method. First you need to create an instance of the Calendar class.
i.e.

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

If your min API version is <26, you can do a shorthand as well:

val yearInt = Year.now().value
Wajid
  • 2,033
  • 16
  • 26
12

#tl;dr

Year.now()
    .getValue()

java.time

The other Answers use the troublesome old date-time classes such as Calendar. These are supplanted by the java.time classes. For older versions of Android, see the ThreeTen-Backport and ThreeTenABP projects.

Year class

Rather than pass around mere integers to represent a year, pass around objects. Namely, the Year class.

Getting the current year requires a time zone. For any given moment, the date varies around the globe by zone. So it is possible for Pacific/Auckland to be on 2018 while America/Montreal is in 2017 simultaneously.

Better to pass explicitly the desired/expected zone. If omitted, you implicitly get the JVM’s current default time zone. That default can change at any moment during runtime, so it is not reliable.

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

When you do 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 supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Basil Bourque
  • 262,936
  • 84
  • 758
  • 1,028
  • 6
    `Year` requires Android API 26 or Java 1.8. – CoolMind Aug 27 '18 at 14:40
  • @CoolMind Reread my Answer more carefully for solution. Look for the word “Android” mentioned two times near the bottom. – Basil Bourque Aug 27 '18 at 15:22
  • The question contains a tag `android`. So I decided to add that comment for Android programmers. – CoolMind Aug 28 '18 at 07:51
  • This will throw a runtime exception on Android versions under 26. – Big McLargeHuge Feb 27 '19 at 15:11
  • @DavidKennedy Reread the comments, the first paragraph of my Answer, and the bullets found at the end of my Answer. Look for the words “Android” and “back-port”. – Basil Bourque Feb 27 '19 at 15:32
  • True, I was just trying to make it more explicit. I was surprised the IDE didn't throw a warning or anything on my project where min SDK is 23. – Big McLargeHuge Feb 27 '19 at 18:28
  • @CoolMind & Big McLargeHuge, in the latest tooling for Android, much of the *java.time* functionality is now available to earlier versions of Android via "API desugaring". See links in my updates to the "About java.time" section at end of my Answer. – Basil Bourque May 06 '21 at 04:35
3
// 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);
Wrong Chao
  • 43
  • 1
  • 5
1

you should use

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

Rajen Raiyarela
  • 5,388
  • 4
  • 19
  • 39
1

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

Nicomedes E.
  • 1,326
  • 5
  • 17
  • 27
0

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);
Zidane
  • 1,472
  • 3
  • 19
  • 35