Total Pageviews

2018/06/16

[Java 8] Examples for LocalDate


package test.albert.datetime;

import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestDateTime {

    private static final DateTimeFormatter FROMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static void main(String[] args) {
        TestDateTime test = new TestDateTime();
        DateRange today = test.getToday();
        log.debug("Get today: {} ", today.toString());

        DateRange yesterday = test.getYesterday();
        log.debug("Get yesterday: {} ", yesterday.toString());

        DateRange lastWeek = test.getLastWeek();
        log.debug("Get last week: {} ", lastWeek.toString());

        DateRange withinOneMonth = test.getWithinOneMonth();
        log.debug("Get within one month: {} ", withinOneMonth.toString());

        DateRange lastMonth = test.getLastMonth();
        log.debug("Get last month: {} ", lastMonth.toString());

        DateRange thisYear = test.getThisYear();
        log.debug("Get this year: {} ", thisYear.toString());

        DateRange lastYear = test.getLastYear();
        log.debug("Get last year: {} ", lastYear.toString());
    }

    /**
     * 計算起訖日期: 今天
     * 
     * @return start/end date
     */
    public DateRange getToday() {
        LocalDate now = LocalDate.now();
        DateRange dateRange = new DateRange(formatDate(now), formatDate(now));
        return dateRange;
    }

    /**
     * 計算起訖日期: 今天減去一天
     * 
     * @return start/end date
     */
    public DateRange getYesterday() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);

        DateRange dateRange = new DateRange(formatDate(yesterday), formatDate(yesterday));
        return dateRange;
    }

    /**
     * 計算起訖日期: 今天減去六天到今天
     * 
     * @return start/end date
     */
    public DateRange getLastWeek() {
        LocalDate now = LocalDate.now();
        LocalDate lastWeek = now.minusDays(6);

        DateRange dateRange = new DateRange(formatDate(lastWeek), formatDate(now));
        return dateRange;
    }

    /**
     * 計算起訖日期: 今天減去一個月到今天
     * 
     * @return start/end date
     */
    public DateRange getWithinOneMonth() {
        LocalDate now = LocalDate.now();
        LocalDate lastMonth = now.minusMonths(1);

        DateRange dateRange = new DateRange(formatDate(lastMonth), formatDate(now));
        return dateRange;
    }

    /**
     * 計算起訖日期: 上個月的第一天到上個月的最後一天
     * 
     * @return start/end date
     */
    public DateRange getLastMonth() {
        LocalDate now = LocalDate.now();
        LocalDate lastMonth = now.minusMonths(1);

        DateRange dateRange = new DateRange(formatDate(lastMonth.withDayOfMonth(1)),
                formatDate(lastMonth.withDayOfMonth(lastMonth.lengthOfMonth())));
        return dateRange;
    }

    /**
     * 計算起訖日期: 今年度的第一天到今天
     * 
     * @return start/end date
     */
    public DateRange getThisYear() {
        LocalDate now = LocalDate.now();
        LocalDate from = LocalDate.of(now.getYear(), Month.JANUARY, 1);

        DateRange dateRange = new DateRange(formatDate(from), formatDate(now));
        return dateRange;
    }

    /**
     * 計算起訖日期: 去年度的第一天到去年度的最後一天
     * 
     * @return start/end date
     */
    public DateRange getLastYear() {
        LocalDate now = LocalDate.now();

        LocalDate from = LocalDate.of(now.getYear() - 1, Month.JANUARY, 1);
        LocalDate to = LocalDate.of(now.getYear() - 1, Month.DECEMBER, 31);

        DateRange dateRange = new DateRange(formatDate(from), formatDate(to));
        return dateRange;
    }

    private String formatDate(LocalDate date) {
        return date.format(FROMATTER);
    }

    @Data
    @AllArgsConstructor
    private static class DateRange {
        private String from;
        private String to;
    }

}


Console
2018-05-09 11:27:31.661 - DEBUG- t.a.datetime.TestDateTime - Get today: TestDateTime.DateRange(from=2018-05-09, to=2018-05-09)  
2018-05-09 11:27:31.664 - DEBUG- t.a.datetime.TestDateTime - Get yesterday: TestDateTime.DateRange(from=2018-05-08, to=2018-05-08)  
2018-05-09 11:27:31.664 - DEBUG- t.a.datetime.TestDateTime - Get last week: TestDateTime.DateRange(from=2018-05-03, to=2018-05-09)  
2018-05-09 11:27:31.664 - DEBUG- t.a.datetime.TestDateTime - Get within one month: TestDateTime.DateRange(from=2018-04-09, to=2018-05-09)  
2018-05-09 11:27:31.665 - DEBUG- t.a.datetime.TestDateTime - Get last month: TestDateTime.DateRange(from=2018-04-01, to=2018-04-30)  
2018-05-09 11:27:31.665 - DEBUG- t.a.datetime.TestDateTime - Get this year: TestDateTime.DateRange(from=2018-01-01, to=2018-05-09)  
2018-05-09 11:27:31.665 - DEBUG- t.a.datetime.TestDateTime - Get last year: TestDateTime.DateRange(from=2017-01-01, to=2017-12-31)  


No comments: