Java – Check if the date is older than 30 days or 6 months

This article shows Java 8 and legacy date-time APIs example to check if a date is 30 days or 6 months older than the current date. 1. Java 8 isBefore() 2. Java 8 ChronoUnit.{UNIT}.between() 3. Java 8 Period.between() 4. Legacy Calendar and Date 5. References 1. Java 8 isBefore() First minus the current date and …

Read more

Java 8 – How to calculate days between two dates?

In Java 8, we can use ChronoUnit.DAYS.between(from, to) to calculate days between two dates. 1. LocalDate JavaBetweenDays1.java package com.mkyong.java8; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class JavaBetweenDays1 { public static void main(String[] args) { LocalDate from = LocalDate.now(); LocalDate to = from.plusDays(10); long result = ChronoUnit.DAYS.between(from, to); System.out.println(result); // 10 } } Output 10 2. LocalDateTime …

Read more

How to compare dates in Java

This article shows few examples to compare two dates in Java. Updated with Java 8 examples. 1. Compare two date 1.1 Date.compareTo 1.2 Date.before(), Date.after() and Date.equals() 1.3 Check if a date is within a certain range 2. Compare two calendar 3. Compare two date and time (Java 8) 3.1 Compare two LocalDate 3.2 Compare …

Read more