tl;dr
org.threeten.extra.YearWeek // Handy class found in the ThreeTen-Extra library added to your project.
.from( // Determine the week number and the week-based year number from the passed `LocalDate` object, according to standard ISO 8601 definition of a week.
myResultSet.getObject( … , LocalDate.class ) // Produces a `LocalDate` object to pass to `YearWeek.from`.
)
.toString() // Generate a String in standard ISO 8601 format: yyyy-Www
2018-W13
Details
When you fetch your Date
type from H2 as a java.sql.Date
, convert to a java.time.LocalDate
.
LocalDate ld = mySqlDate.toLocalDate();
You can interrogate for the ISO 8601 standard definition of a week where week # 1 contains the first Thursday of the year, and runs Monday-Sunday.
int weekNumber = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;
Extract the year.
int year = ld.getYear();
Assemble your standard ISO 8601 string.
String output = year + "-W" + String.format( "%02d ", weekNumber );
Even easier is to use the YearWeek
class from the ThreeTen-Extra project.
String output = YearWeek.from( ld ).toString() ;
JDBC 4.2
As of JDBC 4.2 and later, you can directly exchange java.time objects with your database. No need to over use java.util or java.sql date-time classes again.
LocalDate ld = LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) ; // Capture the current date as seen in the wall-clock used by the people in a certain region (a time zone).
myPreparedStatement.setObject( … , ld ) ;
And retrieval.
LocalDate ld = myResultSet.getObject( … , 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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.