Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQuery;

public class Main {
    public static void main(String[] argv) {
        LocalDate date = LocalDate.parse("2013-01-12");
        TemporalQuery<Quarter> quarterOfYearQuery = new QuarterOfYearQuery();

        System.out.println(date.query(quarterOfYearQuery));
    }
}

enum Quarter {
    FIRST, SECOND, THIRD, FOURTH;
}

class QuarterOfYearQuery implements TemporalQuery<Quarter> {
    @Override
    public Quarter queryFrom(TemporalAccessor temporal) {
        LocalDate now = LocalDate.from(temporal);

        if (now.isBefore(now.with(Month.APRIL).withDayOfMonth(1))) {
            return Quarter.FIRST;
        } else if (now.isBefore(now.with(Month.JULY).withDayOfMonth(1))) {
            return Quarter.SECOND;
        } else if (now.isBefore(now.with(Month.NOVEMBER).withDayOfMonth(1))) {
            return Quarter.THIRD;
        } else {
            return Quarter.FOURTH;
        }
    }
}