Java 获取当前年份值到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19198029/
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-12 14:58:17  来源:igfitidea点击:

get present year value to string

javacalendaroperating-systemdate-format

提问by Frank

I need to get the present year value in string so I did:

我需要在字符串中获取当前年份值,所以我做了:

Calendar now = Calendar.getInstance();
DateFormat date = new SimpleDateFormat("yyyy");
String year = date.format(now);

It works on ubuntu but it's not working on windows 7.

它适用于 ubuntu,但不适用于 Windows 7。

Do you know why? Is there a safer way to do that?

你知道为什么吗?有没有更安全的方法来做到这一点?

Thanks

谢谢

采纳答案by Rohit Jain

You can simple get the year from Calendarinstance using Calendar#get(int field)method:

您可以Calendar使用Calendar#get(int field)方法简单地从实例中获取年份:

Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
String yearInString = String.valueOf(year);

回答by ssantos

What about

关于什么

Date currentDate = new Date();
String currentYear = String.valueOf(currentDate.getYear());

回答by sunysen

try

尝试

Calendar now = Calendar.getInstance();
String year = String.valueOf(now.get(Calendar.YEAR));

回答by jdev

You can also do it like this:

你也可以这样做:

String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));

回答by aianitro

String thisYear = new SimpleDateFormat("yyyy").format(new Date());

回答by Basil Bourque

Time Zone

时区

Both the question and accepted answer ignore the question of time zone. At the time of a new year, the year number varies depending on your time zone.

问题和接受的答案都忽略了时区问题。在新年的时候,年数会因您所在的时区而异。

Joda-Time

乔达时间

The bundled java.util.Date and .Calendar classes are notoriously troublesome. Avoid them. Use either Joda-Time or the new java.time package in Java 8.

捆绑的 java.util.Date 和 .Calendar 类是出了名的麻烦。避开它们。在 Java 8 中使用 Joda-Time 或新的 java.time 包。

Example in Joda-Time.

Joda-Time 中的示例。

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTime = DateTime.now( timeZone );
int year = dateTime.year().get();

回答by kizanlik

Java 8:

爪哇 8:

java.time.Year.now();

here is the link.

这是链接

回答by Gemtastic

In Java 8 there's a collection called java.timein which you easily can obtain the current year from your computer's clock.

在 Java 8 中有一个名为的集合java.time,您可以在其中轻松地从计算机的时钟中获取当前年份。

To get the current year as an integer you can simply write:

要将当前年份作为整数,您可以简单地编写:

int thisYear = Year.now().getValue();

To get it as a String:

将其作为字符串获取:

String thisYear = Year.now().toString();

回答by Nandkishor Gokhe

  //  enter code here
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy");
String year = format.format(date);

回答by Rok T.

In Java versions prior Java 8 you could also use java.sql.Date class. Convert to String and substring the year:

在 Java 8 之前的 Java 版本中,您还可以使用 java.sql.Date 类。转换为字符串并将年份子字符串化:

String currentYear = new java.sql.Date(System.currentTimeMillis()).toString().substring(0, 4);