Get current quarter of the year Tag(s): Date and Time
The ISO-8601 standard is based on the standard civic 12 month year. This is commonly divided into four quarters, often abbreviated as Q1, Q2, Q3 and Q4.
January, February and March are in Q1.
April, May and June are in Q2.
July, August and September are in Q3.
October, November and December are in Q4.
[JDK8]
import java.time.LocalDate; import java.time.temporal.IsoFields; import java.time.temporal.TemporalAdjusters; public class QuarterOfTheYear { public static void main(String[] args) { System.out.println("Current Quarter : " + LocalDate.now().get(IsoFields.QUARTER_OF_YEAR) ); System.out.println("2017-01-01 Quarter : " + LocalDate.of(2017, 04, 01).get(IsoFields.QUARTER_OF_YEAR) ); // get the start/end date of a quarter // LocalDate localDate = LocalDate.parse("2019-09-04"); LocalDate localDate = LocalDate.now(); LocalDate firstDayOfQuarter = localDate.with(IsoFields.DAY_OF_QUARTER, 1L);; LocalDate lastDayOfQuarter = firstDayOfQuarter.plusMonths(2) .with(TemporalAdjusters.lastDayOfMonth()); System.out.println( "Date : " + localDate + "\n" + "Quarter : " + LocalDate.now().get(IsoFields.QUARTER_OF_YEAR) + "\n" + "Start : " + firstDayOfQuarter + "\n" + "End : " + lastDayOfQuarter ); } }
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com