如何在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
How to get day of year number in Java for a specific date object?
提问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 Calendar
which java provides. Using the get()
method and DAY_OF_YEARyou can get what you want.
您可以使用Calendar
java 提供的。使用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 Calendar
class are outdated. The troublesome old date-time classes such as java.util.Date
, java.util.Calendar
, and java.text.SimpleTextFormat
are now legacy, supplanted by the java.timeclasses.
其他带有Calendar
类的答案已过时。麻烦的旧日期,时间类,如java.util.Date
,java.util.Calendar
和java.text.SimpleTextFormat
现在的遗产,由取代java.time类。
Using java.time
使用 java.time
The LocalDate
class 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 EST
or IST
as they are nottrue time zones, not standardized, and not even unique(!).
以、、 或等格式指定正确的时区名称。永远不要使用 3-4 个字母的缩写,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。continent/region
America/Montreal
Africa/Casablanca
Pacific/Auckland
EST
IST
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 LocalDate
into 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.
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 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABPproject adapts ThreeTen-Backport(mentioned above) for Android specifically.
- See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 所述ThreeTenABP项目适应ThreeTen-反向移植(上述)为Android特异性。
- 请参阅如何使用ThreeTenABP ...。
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 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。