将一年中的日期转换为Java中的日期 [英] Convert day of the year to a date in java
问题描述
我一年中有几天。 (今天的日期为308。)我想将此数字转换为SQL格式的日期( YYYY / MM / DD
)。
谢谢。
< h1> tl; dr
Year.of(2018)
.atDay(308)
2018-11-04
java.time
现代方法使用Java 8及更高版本中内置的java.time类。
关于java.time
java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date
, Calendar
,& SimpleDateFormat
。
现在处于维护模式的Joda-Time 项目建议迁移到 java.time 类。
要了解更多信息,请参见 Oracle教程。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310 。
从哪里获取java.time类?
- Java SE 8 , Java SE 9 ,然后是
- 内置。
- 具有捆绑实现的标准Java API的一部分。
- Java 9添加了一些次要功能和修复。
- Java SE 6 和 Java SE 7
- 许多java.time功能都反向移植到Java 6&
nofollow noreferrer> ThreeTen-Backport 。
- 许多java.time功能都反向移植到Java 6&
- Android
- ThreeTenABP 项目专为Android改编了 ThreeTen-Backport (如上所述)。
- 请参阅 如何使用ThreeTenABP… 。
ThreeTen-Extra 项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容打下了基础。您可能会在这里找到一些有用的类,例如 时间间隔
, YearWeek
, YearQuarter
和更多。
I have a number of day of the year. (Today's number of the day is 308.) I want to convert this number to a date in SQL format (YYYY/MM/DD
). Can I do it in java?
Thank you in advance.
tl;dr
Year.of( 2018 )
.atDay( 308 )
2018-11-04
java.time
The modern approach uses the java.time classes built into Java 8 and later.
The LocalDate
class represents a date-only value without time-of-day and without time zone.
The Year
class represents, well, a year. And includes a atDay( dayOfYear )
method where you pass your ordinal day of the year.
int dayOfYear = 308 ;
Year y = Year.of( 2018 ) ;
LocalDate ld = y.atDay( dayOfYear ) ;
Dump to console.
System.out.println( "dayOfYear: " + dayOfYear + " at year: " + y + " = " + ld ) ;
See this code run live at IdeOne.com.
dayOfYear: 308 at year: 2018 = 2018-11-04
As for generating a string in a particular format, search for DateTimeFormatter
class as that has been covered many many times already.
And by the way, your particular format is not at all a "SQL format". Regardless, you should be exchanging objects with your database rather than mere strings. Look at the getObject
and setObject
methods in JDBC when using a driver compliant with JDBC 4.2 or later.
Database
For SQL access to a database column of SQL-standard type DATE
, use JDBC 4.2 or later to pass the LocalDate
object directly. Do not use mere strings to exchange date-time values.
myPreparedStatement.setObject( … , ld ) ;
Retrieval.
LocalDate ld = myResultSet.getDate( … , LocalDate.class ) ;
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- 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 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project 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.
这篇关于将一年中的日期转换为Java中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!