Java date, time comparison

Source: Internet
Author: User
Java date, time comparison using the Joda package Date Comparison
String begindate = "2017/07/05";
String enddate = "2017/09/04";
SYSTEM.OUT.PRINTLN (new DateTime (Begindate). Isafter (new DateTime (EndDate)); The result is false

String begindate = "2017/09/04";
String enddate = "2017/09/04";
SYSTEM.OUT.PRINTLN (new DateTime (Begindate). Isafter (new DateTime (EndDate)); The result is false

String begindate = "2017/07/05";
String enddate = "2017/09/04";
SYSTEM.OUT.PRINTLN (new DateTime (Begindate). Isbefore (new DateTime (EndDate)); The result is true

String begindate = "2017/09/04";
String enddate = "2017/09/04";
SYSTEM.OUT.PRINTLN (new DateTime (Begindate). Isbefore (new DateTime (EndDate)); The result is false
Time Comparison
String begintime = "15:02:22";
String endtime = "15:02:22";
System.out.println (New LocalTime (BeginTime). CompareTo (New LocalTime (Endtime)); The result is 0

String begintime = "12:02:22";
String endtime = "15:02:22";
System.out.println (New LocalTime (BeginTime). CompareTo (New LocalTime (Endtime)); The result is-1

String begintime = "12:02:22";
String endtime = "10:02:22";
System.out.println (New LocalTime (BeginTime). CompareTo (New LocalTime (Endtime)); Result is 1
using the SimpleDateFormat package Date and Time comparisons
string begintime = "10:00:00";
String endtime = "3:59:59";
StringBuilder datebegin = new StringBuilder ("2018-08-08");
StringBuilder dateend = new StringBuilder ("2018-08-08");
Date dtbegintime = Df.parse (Datebegin.append (begintime). toString ());

Date dtendtime = Df.parse (Dateend.append (Endtime). toString ()); System.out.println (Dtbegintime.gettime () >= dtendtime.gettime ()); True System.out.println (dtbegintime.gettime () = = Dtendtime.gettime ()); False System.out.println (Dtbegintime.gettime () < Dtendtime.gettime ()); False 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.