Java在线运行

版本:
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
实现计数排序以及线性计数排序 发布于:2023-11-30 11:47 线性计数排序 发布于:2023-11-30 09:23 计数排序算法(统计数组)-20052244 发布于:2023-11-29 23:46 线性计数排序 (Radix Sort) 发布于:2023-11-29 23:24 计数排序 (Counting Sort) 发布于:2023-11-29 23:23 # 计数排序 20052295 郑思思 发布于:2023-11-29 22:54 计数排序算法 发布于:2023-11-29 19:59 计数排序CountSort-20052244 发布于:2023-11-29 20:56 计数排序。 发布于:2023-11-29 20:44 堆排序实现 发布于:2023-11-29 18:50 最长公共子序列 发布于:2023-11-29 17:03 Size balance tree java实现 发布于:2023-11-29 14:35 推排序算法 发布于:2023-11-29 14:05 java堆排序 发布于:2023-11-28 23:50 2023-11-28-堆排序 发布于:2023-11-28 23:19 堆排序算法 发布于:2023-11-28 20:51 线性计数排序 发布于:2023-11-28 23:13 计数排序的实现 发布于:2023-11-28 16:39 计数排序的实现(线性) 发布于:2023-11-28 16:26 计数排序的实现Java 发布于:2023-11-28 16:24 计数排序算法 发布于:2023-11-28 15:34 基数排序。 发布于:2023-11-28 16:40 从控制台输出 发布于:2023-11-28 12:09 从控制台输出 发布于:2023-11-28 10:27 IoException Test find user 发布于:2023-11-28 10:19 堆排序实验 发布于:2023-11-27 22:42 堆排序代码 发布于:2023-11-27 23:06 堆排序的实现 发布于:2023-11-27 22:30 堆排序实验 发布于:2023-11-27 22:16 大根堆排序 发布于:2023-11-27 22:12 堆排序实验 发布于:2023-11-27 22:06 堆排序实验 发布于:2023-11-27 19:31 堆排序的实现Java版 发布于:2023-11-27 21:08 堆排序算法 发布于:2023-11-27 10:50 11 27 fghjfgjfghj 发布于:2023-11-27 08:48 11 27题目一代码 发布于:2023-11-27 08:17 堆排序代码 发布于:2023-11-27 00:25 java zuoye 发布于:2023-11-26 13:41 堆排序算法分析 发布于:2023-11-25 23:41 堆排序的简单实现 发布于:2023-11-25 15:29 java程序设计求交集! 发布于:2023-11-24 13:49 堆排序的实现 发布于:2023-11-23 23:14 堆排序实现 发布于:2023-11-22 23:38 堆排序代码 发布于:2023-11-22 20:49 堆排序算法 发布于:2023-11-22 21:21 自顶向下构造堆 发布于:2023-11-22 19:39 堆优先队列——自顶向下 发布于:2023-11-22 20:49 堆的自顶向下 发布于:2023-11-21 16:48 堆排序算法 发布于:2023-11-25 16:24 堆排序。。 发布于:2023-11-28 00:24 [更多]
显示目录

How to compare dates in Java



Few examples show you how to compare two dates in Java. Updated with Java 8 examples.

1. Date.compareTo()

A classic method to compare two java.util.Date in Java.

  1. Return value is 0 if both dates are equal.
  2. Return value is greater than 0 , if Date is after the date argument.
  3. Return value is less than 0, if Date is before the date argument.

TestDate.java

package com.mkyong.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDate {

    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2009-12-31");
        Date date2 = sdf.parse("2010-01-31");

        System.out.println("date1 : " + sdf.format(date1));
        System.out.println("date2 : " + sdf.format(date2));

        if (date1.compareTo(date2) > 0) {
            System.out.println("Date1 is after Date2");
        } else if (date1.compareTo(date2) < 0) {
            System.out.println("Date1 is before Date2");
        } else if (date1.compareTo(date2) == 0) {
            System.out.println("Date1 is equal to Date2");
        } else {
            System.out.println("How to get here?");
        }

    }

}

Output

date1 : 2009-12-31
date2 : 2010-01-31
Date1 is before Date2

2. Date.before(), Date.after() and Date.equals()

A more user friendly method to compare two java.util.Date

TestDate2.java

package com.mkyong.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDate2 {

    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2009-12-31");
        Date date2 = sdf.parse("2010-01-31");

        System.out.println("date1 : " + sdf.format(date1));
        System.out.println("date2 : " + sdf.format(date2));

        if (date1.after(date2)) {
            System.out.println("Date1 is after Date2");
        }

        if (date1.before(date2)) {
            System.out.println("Date1 is before Date2");
        }

        if (date1.equals(date2)) {
            System.out.println("Date1 is equal Date2");
        }

    }

}

Output

date1 : 2009-12-31
date2 : 2010-01-31
Date1 is before Date2

3. Calender.before(), Calender.after() and Calender.equals()

Example to compare two java.util.Calendar

TestDate3.java

package com.mkyong.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TestDate3 {

    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2009-12-31");
        Date date2 = sdf.parse("2010-01-31");

        System.out.println("date1 : " + sdf.format(date1));
        System.out.println("date2 : " + sdf.format(date2));

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);

        if (cal1.after(cal2)) {
            System.out.println("Date1 is after Date2");
        }

        if (cal1.before(cal2)) {
            System.out.println("Date1 is before Date2");
        }

        if (cal1.equals(cal2)) {
            System.out.println("Date1 is equal Date2");
        }
    }

}

Output

date1 : 2009-12-31
date2 : 2010-01-31
Date1 is before Date2

4. Java 8

In Java 8, you can use the new isBefore(), isAfter(), isEqual() and compareTo() to compare LocalDate, LocalTime and LocalDateTime.

Review the following example to compare two java.time.LocalDate

TestDate4.java

package com.mkyong.date;

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

public class TestDate4 {

    public static void main(String[] args) {

        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date1 = LocalDate.of(2009, 12, 31);
        LocalDate date2 = LocalDate.of(2010, 01, 31);

        System.out.println("date1 : " + sdf.format(date1));
        System.out.println("date2 : " + sdf.format(date2));

        System.out.println("Is...");
        if (date1.isAfter(date2)) {
            System.out.println("Date1 is after Date2");
        }

        if (date1.isBefore(date2)) {
            System.out.println("Date1 is before Date2");
        }

        if (date1.isEqual(date2)) {
            System.out.println("Date1 is equal Date2");
        }

        System.out.println("CompareTo...");
        if (date1.compareTo(date2) > 0) {

            System.out.println("Date1 is after Date2");

        } else if (date1.compareTo(date2) < 0) {

            System.out.println("Date1 is before Date2");

        } else if (date1.compareTo(date2) == 0) {

            System.out.println("Date1 is equal to Date2");

        } else {

            System.out.println("How to get here?");

        }
    }

}

Output

date1 : 2009-12-31
date2 : 2010-01-31
Is...
Date1 is before Date2
CompareTo...
Date1 is before Date2

References

  1. Date CompareTo JavaDoc
  2. Calendar before after JavaDoc
  3. LocalDate JavaDoc
由JSRUN为你提供的Java在线运行、在线编译工具
        JSRUN提供的Java 在线运行,Java 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。