In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Java how to compare the two dates, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Date.compareTo ()
Date implements Comparable, so the two dates can be compared directly with the compareTo method.
If the two dates are equal, the return value is 0.
If Date1 is after the Date2 parameter, the return value is greater than 0.
If Date1 precedes the Date2 parameter, the return value is less than 0.
Package com.wupx.date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateDemo {public static void main (String [] args) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); Date date1 = sdf.parse ("2019-10-01"); Date date2 = sdf.parse ("2019-10-17"); 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("咋到这的?"); } }} 输出结果 date1 : 2019-10-01date2 : 2019-10-17Date1 is before Date2Date.before() Date.after() Date.equals() 可以用 equals、after 和 before 方法比较日期。 如果两个日期在同一时间点,equals方法将返回true。 如果 date1 在 date2 之前,before 返回 true,否则返回 false。 如果 date2 在 date1 之后,after 返回 true,否则返回 false。 package com.wupx.date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateDemo2 { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = sdf.parse("2019-10-01"); Date date2 = sdf.parse("2019-10-17"); 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"); } }} 输出结果 date1 : 2019-10-01date2 : 2019-10-17Date1 is before Date2Calender.before() Calender.after() Calender.equals() Calendar 类也有 compareTo、equals、after 和 before 方法,工作方式与上面描述的 Date 类的方法相同。因此,如果日期信息保存在 Calendar 类中,则不需要提取日期来执行比较。 package com.wupx.date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateDemo3 { 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"); } }} 输出结果 date1 : 2019-10-01date2 : 2019-10-17Date1 is before Date2getTime() 可以直接比较两个日期的时间点。这是对两种原始数据类型的比较,因此可以使用 < 、 >Compare with =.
Before comparing dates, you must use the data in the Date object you created earlier to create a long integer.
Package com.wupx.date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateDemo4 {public static void main (String [] args) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); Date date1 = sdf.parse ("2019-10-01"); Date date2 = sdf.parse ("2019-10-17") System.out.println ("date1:" + sdf.format (date1)); System.out.println ("date2:" + sdf.format (date2)); long time1 = date1.getTime (); long time2 = date2.getTime (); if (time1 > time2) {System.out.println ("Date1 is after Date2");} else if (time1)
< time2) { System.out.println("Date1 is before Date2"); } else if (time1 == time2) { System.out.println("Date1 is equal to Date2"); } else { System.out.println("咋到这的?"); } }} 输出结果 date1 : 2019-10-01date2 : 2019-10-17Date1 is before Date2Java 8 中的 isBefore() isAfter() isEqual() compareTo() 在 Java 8 中,可以使用新的 isBefore()、isAfter()、isEqual() 以及 compareTo() 来比较 LocalDate、LocalTime 和 LocalDateTime。 package com.wupx.date;import java.time.LocalDate;import java.time.format.DateTimeFormatter;public class DateDemo5 { public static void main(String[] args) { DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate date1 = LocalDate.of(2019, 10, 01); LocalDate date2 = LocalDate.of(2019, 10, 17); 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 did you get here?") ;}
Output result
Date1: 2019-10-01date2: 2019-10-17Is...Date1 is before Date2CompareTo...Date1 is before Date2 after reading the above, have you mastered the method of comparing two dates in Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.