Java Reference
In-Depth Information
. withDayOfMonth ( 1 ))) {
return Quarter . SECOND ;
} else if ( now . isBefore ( now . with ( Month . NOVEMBER )
. withDayOfMonth ( 1 ))) {
return Quarter . THIRD ;
} else {
return Quarter . FOURTH ;
}
}
}
TemporalQuery objects can be used directly or indirectly. Let's look at an example of
each:
QuarterOfYearQuery q = new QuarterOfYearQuery ();
// Direct
Quarter quarter = q . queryFrom ( LocalDate . now ());
System . out . println ( quarter );
// Indirect
quarter = LocalDate . now (). query ( q );
System . out . println ( quarter );
Under most circumstances, it is better to use the indirect approach, where the query
object is passed as a parameter to query() . This is because it is normally a lot clearer
to read in code.
Adjusters
Adjusters modify date and time objects. Suppose, for example, that we want to
return the first day of a quarter that contains a particular timestamp:
public class FirstDayOfQuarter implements TemporalAdjuster {
@Override
public Temporal adjustInto ( Temporal temporal ) {
final int currentQuarter = YearMonth . from ( temporal )
. get ( IsoFields . QUARTER_OF_YEAR );
switch ( currentQuarter ) {
case 1 :
return LocalDate . from ( temporal )
. with ( TemporalAdjusters . firstDayOfYear ());
case 2 :
return LocalDate . from ( temporal )
. withMonth ( Month . APRIL . getValue ())
. with ( TemporalAdjusters . firstDayOfMonth ());
case 3 :
return LocalDate . from ( temporal )
. withMonth ( Month . JULY . getValue ())
. with ( TemporalAdjusters . firstDayOfMonth ());
case 4 :
m
n
s
a
 
Search WWH ::




Custom Search