java – Java8 Java.util.Date conversion to Java.time.ZonedDateTime

The answer by assylias and the answer by JB Nizet are both correct:

  1. Call the new conversion method added to the legacy class, Java.util.Date::toInstant .
  2. Who loves Instant::atZone moving on to ZoneId resulting in ZonedDateTime .

enter image description here

But your code example is aimed at quarters. For this, read on.

Accommodation

There is no need to manage the quarters yourself. Use a class that has already been written and tested.

org.threeten.extra.YearQuarter

The classes Java.time are extended by the project ThreeTen-Extra . Among the many useful classes provided in that library you will find Quarter And YearQuarter .

First get yours ZonedDateTime .

ZonedId z = ZoneID.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = myJavaUtilDate.toInstant().atZone( z ) ;

Determine the quarter of the year for that particular date.

YearQuarter yq = YearQuarter.from( zdt ) ;

Next we need the start date of that quarter.

LocalDate quarterStart = yq.atDay( 1 ) ;

While I don’t necessarily recommend doing this, you could use a single line of code rather than implement a method.

LocalDate quarterStart =                    // Represent a date-only, without time-of-day and without time zone.
    YearQuarter                             // Represent a specific quarter using the ThreeTen-Extra class `org.threeten.extra.YearQuarter`. 
    .from(                                  // Given a moment, determine its year-quarter.
        myJavaUtilDate                      // Terrible legacy class `Java.util.Date` represents a moment in UTC as a count of milliseconds since the Epoch of 1970-01-01T00:00:00Z. Avoid using this class if at all possible.
        .toInstant()                        // New method on old class to convert from legacy to modern. `Instant` represents a moment in UTC as a count of nanoseconds since the Epoch of 1970-01-01T00:00:00Z. 
        .atZone(                            // Adjust from UTC to the wall-clock time used by the people of a particular region (a time zone). Same moment, same point on the timeline, different wall-clock time.
            ZoneID.of( "Africa/Tunis" )     // Specify a time zone using proper `Continent/Region` format. Never use 2-4 letter pseudo-zone such as `PST` or `EST` or `IST`. 
        )                                   // Returns a `ZonedDateTime` object.
    )                                       // Returns a `YearQuarter` object.
    .atDay( 1 )                             // Returns a `LocalDate` object, the first day of the quarter. 
;

By the way, if you can phase out the use of Java.util.Date overall, do it. It is a class terrible along with his brothers like Calendar. Use Date only where it is needed, when interfacing with old code not yet updated to Java.time .


Approximately Java.time

The framework Java.time it is embedded in Java 8 and later. These classes supplant the annoying old legacy date-time classes such as Java.util.Date , Calendar & SimpleDateFormat .

To learn more, see Oracle tutorial . And search Stack Overflow for many examples and explanations. The specification is JSR 310 .

The project Joda-Time now in maintenance mode recommends migrating to the classes Java.time .

You can trade items Java.time directly with your database. Use a driver JDBC compliant with JDBC 4.2 or later. No need for strings, no need for Java.sql.* classes.

Where to get the Java.time classes?

The project ThreeTen-Extra extends Java.time with additional classes. This project is a test bed for possible future additions to Java.time. You can find some useful classes here like Interval , YearWeek , YearQuarter And more .

Leave a comment