java 判断日期连续_如何在Java中检查日期是否大于另一个日期?[重复]

小编典典

从这里拍摄:

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class DateDiff {

public static void main( String[] args )

{

compareDates("2017-01-13 00:00:00", "2017-01-14 00:00:00");// output will be Date1 is before Date2

compareDates("2017-01-13 00:00:00", "2017-01-12 00:00:00");//output will be Date1 is after Date2

compareDates("2017-01-13 00:00:00", "2017-01-13 10:20:30");//output will be Date1 is before Date2 because date2 is ahead of date 1 by 10:20:30 hours

compareDates("2017-01-13 00:00:00", "2017-01-13 00:00:00");//output will be Date1 is equal Date2 because both date and time are equal

}

public static void compareDates(String d1,String d2)

{

try{

// If you already have date objects then skip 1

//1

// Create 2 dates starts

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date1 = sdf.parse(d1);

Date date2 = sdf.parse(d2);

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

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

// Create 2 dates ends

//1

// Date object is having 3 methods namely after,before and equals for comparing

// after() will return true if and only if date1 is after date 2

if(date1.after(date2)){

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

}

// before() will return true if and only if date1 is before date2

if(date1.before(date2)){

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

}

//equals() returns true if both the dates are equal

if(date1.equals(date2)){

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

}

System.out.println();

}

catch(ParseException ex){

ex.printStackTrace();

}

}

public static void compareDates(Date date1,Date date2)

{

// if you already have date objects then skip 1

//1

//1

//date object is having 3 methods namely after,before and equals for comparing

//after() will return true if and only if date1 is after date 2

if(date1.after(date2)){

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

}

//before() will return true if and only if date1 is before date2

if(date1.before(date2)){

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

}

//equals() returns true if both the dates are equal

if(date1.equals(date2)){

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

}

System.out.println();

}

}

2020-09-23

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值