将一年中的日期转换为Java中的日期 [英] Convert day of the year to a date in java

查看:70
本文介绍了将一年中的日期转换为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类?





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?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆