- Call the new conversion method added to the legacy class,
- Who loves
Instant::atZonemoving on to
But your code example is aimed at quarters. For this, read on.
There is no need to manage the quarters yourself. Use a class that has already been written and tested.
First get yours
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
Date only where it is needed, when interfacing with old code not yet updated to Java.time .
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
YearQuarter And more .