Home > Java > JavaBase > Java method to determine whether a time is today

Java method to determine whether a time is today

Release: 2019-12-04 14:44:45
Original
7257 people have browsed it

Java method to determine whether a time is today

java determines whether the time is today:

public class Test {
    /**
     * java 判断一个时间是不是今天的时间范围内
     * @param args
     */
    public static void main(String[] args) {
        String time = "2017-09-27 11:20:45";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
        LocalDateTime localTime = LocalDateTime.parse(time, dtf);
        LocalDateTime startTime = LocalDate.now().atTime(0, 0, 0);
        LocalDateTime endTime = LocalDate.now().atTime(23, 59, 59);
        //如果小于今天的开始日期
        if (localTime.isBefore(startTime)) {
            System.out.println("时间是过去");
        }
        //如果大于今天的开始日期,小于今天的结束日期
        if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
            System.out.println("时间是今天");
        }
        //如果大于今天的结束日期
        if (localTime.isAfter(endTime)) {
            System.out.println("时间是未来");
        }
    }
}
Copy after login

java.time.LocalTime.isAfter(LocalTime other) method checks whether this time is after the specified time.

Declaration:

public boolean isAfter(LocalTime other)
Copy after login

Parameters: other - Another time to compare, not null.

Return value: true if this time is after the specified time.

java.time.LocalTime.isBefore(LocalTime other) method checks whether this time is before the specified time.

Declaration:

public boolean isBefore(LocalTime other)
Copy after login

Parameters: other - Another time to compare, not null.

Return value: true if this time is earlier than the specified time.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Java method to determine whether a time is today. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template