Java 如何在android中获取当前年份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41356279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:19:02  来源:igfitidea点击:

How to get current year in android?

javaandroid

提问by Shubh

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.

不能从静态上下文中引用非静态方法“get(int)”。

I am calling this method from call method of observable.

我是从 observable 的 call 方法调用这个方法的。

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.

我也看过,(new Date()).getYear();但它已被弃用。

采纳答案by dsncode

Because you need to create an instance first.

因为你需要先创建一个实例。

try this

尝试这个

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

and you are good to go.

你很高兴去。

回答by Rajen Raiyarela

you should use

你应该使用

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

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

回答by Wajid Ali

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 类的实例。
IE

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

and here you get year, current year!

在这里你得到年份,当前年份!

回答by Wrong Chao

// 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);

回答by Mustafa Karim

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

您也Calendar.getInstance() .get(Calendar.YEAR )可以用于Kotlin语言目的

回答by Zidane

Have you tried the code below

你有没有试过下面的代码

Calendar c = Calendar.getInstance();

日历 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);

回答by Basil Bourque

tl;dr

tl;博士

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.

其他答案使用麻烦的旧日期时间类,例如日历。这些被 java.time 类取代。对于旧版本的 Android,请参阅 ThreeTen-Backport 和 ThreeTenABP 项目。

Yearclass

Year班级

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

而不是传递仅仅代表一年的整数,而是传递对象。即Year类。

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/Aucklandto be on 2018 while America/Montrealis in 2017 simultaneously.

获取当前年份需要时区。对于任何给定时刻,日期因地区而异。所以有可能Pacific/Auckland在 2018 年同时America/Montreal在 2017 年。

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.

最好明确地通过所需/预期的区域。如果省略,则隐式获取 JVM 的当前默认时区。该默认值可以在运行时随时更改,因此它不可靠。

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

关于 java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

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

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?