集册 Java实例教程 使用查询方法在LocalDate上查询日期

使用查询方法在LocalDate上查询日期

欢马劈雪     最近更新时间:2020-01-02 10:19:05

586
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用查询方法在LocalDate上查询日期

import static java.time.DayOfWeek.FRIDAY;//来 自 nowjava - 时  代  Java

import static java.time.temporal.ChronoField.DAY_OF_MONTH;

import static java.time.temporal.ChronoField.DAY_OF_WEEK;

import java.time.DayOfWeek;

import java.time.LocalDate;

import java.time.LocalTime;

import java.time.temporal.TemporalAccessor;


public class Main {

  public static void main(String[] args) {

    LocalDate ld1 = LocalDate.of(2013, 12, 13);

    Boolean isFriday13 = ld1.query(Main::isFriday13);

    System.out.println("Date: " + ld1 + ", isFriday13: " + isFriday13);


    LocalDate ld2 = LocalDate.of(2017, 1, 10);

    isFriday13 = ld2.query(Main::isFriday13);

    System.out.println("Date: " + ld2 + ", isFriday13: " + isFriday13);

    /**
     from
    * 时代Java公众号 - nowjava.com 
    **/

    LocalTime lt = LocalTime.of(7, 30, 45);

    isFriday13 = lt.query(Main::isFriday13);

    System.out.println("Time: " + lt + ", isFriday13: " + isFriday13);

  }


  public static Boolean isFriday13(TemporalAccessor temporal) {

    if (temporal.isSupported(DAY_OF_MONTH) && temporal.isSupported(DAY_OF_WEEK)) {

      int dayOfMonth = temporal.get(DAY_OF_MONTH);

      
展开阅读全文