如何在Java中获取特定日期对象的年份编号?

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

How to get day of year number in Java for a specific date object?

javadatecalendar

提问by Koray Tugay

Say you have a Date object with day / month and year values.

假设您有一个带有日/月和年值的 Date 对象。

I want to know which date is it.

我想知道是哪一天。

By this I mean, like 5th of March is for example the 65th of the year. Or like 15th of January is the 15th.

我的意思是,例如 3 月 5 日是今年的第 65 日。或者像 1 月 15 日是 15 日。

Please no joda time. ( Not used in the current project. )

请不要乔达时间。(当前项目中未使用。)

采纳答案by isnot2bad

Calendar#get(Calendar.DAY_OF_YEAR);

回答by SudoRahul

You can use the Calendarwhich java provides. Using the get()method and DAY_OF_YEARyou can get what you want.

您可以使用Calendarjava 提供的。使用get()方法和DAY_OF_YEAR你可以得到你想要的。

Ex:-

前任:-

Calendar cal = new GregorianCalendar();
cal.setTime(new Date()); // Give your own date
System.out.println(cal.get(Calendar.DAY_OF_YEAR));

回答by Basil Bourque

tl;dr

tl;博士

LocalDate.now( ZoneId.of( "America/Montreal" ) )  // Today's date in a particular time zone.
         .getDayOfYear()                          // Returns day-of-year (1-366).

Details

细节

The other Answers with Calendarclass are outdated. The troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleTextFormatare now legacy, supplanted by the java.timeclasses.

其他带有Calendar类的答案已过时。麻烦的旧日期,时间类,如java.util.Datejava.util.Calendarjava.text.SimpleTextFormat现在的遗产,由取代java.time类。

Using java.time

使用 java.time

The LocalDateclass represents a date-only value without time-of-day and without time zone.

LocalDate级表示没有时间一天和不同时区的日期,唯一的价值。

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris Franceis a new day while still “yesterday” in Montréal Québec.

时区对于确定日期至关重要。对于任何给定时刻,日期因地区而异。例如,在法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是“昨天” 。

Specify a proper time zone namein the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as ESTor ISTas they are nottrue time zones, not standardized, and not even unique(!).

以、、 或等格式指定正确的时区名称。永远不要使用 3-4 个字母的缩写,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。continent/regionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTIST

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

2017-01-23

2017-01-23

We can ask what day-of-year that is, 1-366.

我们可以问一年中的哪一天,1-366。

int dayOfYear = today.getDayOfYear() ;

23

23

We can adjust that LocalDateinto a specific day-of-year.

我们可以将其调整LocalDate为一年中的特定日期。

LocalDate ld = today.withDayOfYear( 187 ) ;

2017-07-06

2017-07-06

See this code run live at IdeOne.com.

查看此代码在 IdeOne.com 上实时运行



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 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多