Java Date Comparison size

Source: Internet
Author: User

Method One: Dateformat fmt = new simpledateformat ("Yyyy-mm-dd");  string datebegin= Fmt.format (Carriercommand.getdatebegin ());  string dateend=fmt.format (Carriercommand.getdateend ());  //if the date format you obtained is not ' 2008-05-22 ', you must format the date  string datebegin = request.getparameter (" Datebegin "),  string dateend = request.getparameter (" DateEnd ");  if ( Java.sql.Date.valueOf (Datebegin). After (Java.sql.Date.valueOf (dateend))) { //Start date is greater than End date   Errors.rejectvalue ("DateEnd", null,  "Start date must be less than End date!");  } stringbuffer timebegin = new stringbuffer ();  stringbuffer timeend  = new stringbuffer ();  timebegin.append (Request.getparameter ("TimeBegin"));  Timeend.append (Request.getparameter ("Timeend"));  timebegin.append (": xx");  timeend.append (": 00");  if (Java.sql.Date.valueOf (Datebegin). Equals (Java.sql.Date.valueOf (dateend)))  { //start date equals end date  if (JAVA.sql.time.valueof (Timebegin.tostring ()). Equals (java.sql.time.valueof  (timeend.tostring ())))//Time Same  {  errors.rejectvalue ("Timeend", null,  "Start and end date Same, start time must be less than end time!");  } if (Java.sql.Time.valueOf (timebegin.tostring ()). After (java.sql.time.valueof  (timeend.tostring ()))//Time Same  { errors.rejectvalue ("Timeend", null,  "Start and end date Same, start time must be less than end time!");  } } method Two:import java.text.dateformat;import java.text.simpledateformat;import  java.util.date; /****  @author  hp*/public class test {public static  Void main (string args[])  {int i= compare_date ("1995-11-12 15:21",  " 1999-12-11 09:59 "); System.out.println ("i==" +i); Public static int compare_date (STRING&NBSP;DATE1,&NBSP;STRING&NBSP;DATE2)  {DateFormat  df = new simpledateformat ("yyyy-mm-dd hh:mm");try {date dt1 =  Df.parse (DATE1);D ate dT2 = df.parse (DATE2);if  (Dt1.gettime ()  > dt2.gettime ())  {system.out.println (" dt1  in front of DT2 "); return 1;}  else if  (Dt1.gettime ()  < dt2.gettime ())  {system.out.println ("Dt1 after DT2"); Return -1;}  else {return 0;}}  catch  (exception exception)  {exception.printstacktrace ();} return 0;}} Method Three: For example: Now is 2004-03-26 13:31:40 past is: 2004-01-02 11:30:24 I now want to get two date difference, The form of the difference is: xx days xx hours xx minutes xx seconds Java Computing time difference and compare the size of times such as: Now is 2004-03-26 13:31:40 past is: 2004-01-02 11:30:24 I now want to get two date difference, The form of the difference is: xx days xx hours xx minutes xx seconds one: Dateformat df = new simpledateformat ("Yyyy-mm-dd hh:mm:ss"); try{ Date d1 = df.parse ("2004-03-26 13:31:40");D ate d2 = df.parse ("2004-01-02  11:30:24 "); Long diff = d1.gettime ()  - d2.gettime ();long days =  diff /  (1000 * 60 * 60 * 24);} catch  (exception e) {} II:Simpledateformat df = new simpledateformat ("Yyyy-mm-dd hh:mm:ss"); java.util.Date  now = df.parse ("2004-03-26 13:31:40"); Java.util.date date=df.parse ("2004-01-02  11:30:24 "); Long l=now.gettime ()-date.gettime (); long day=l/(24*60*60*1000); Long hour= (l/(60*60* -day*24) long min= ((l/(60*1000)-day*24*60-hour*60); Long s= (l/1000-day*24*60*60-hour*60*60-min* 60); System.out.println ("+day+" Days "+hour+" hours "+min+" minutes "+s+" seconds "); three:simpledateformat dfs = new  SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); Java.util.date begin=dfs.parse ("2004-01-02 11:30:24"); Java.util.date end = dfs.parse ("2004-03-26 13:31:40"); long between= (End.getTime ()- Begin.gettime ())/1000;//divided by 1000 for conversion into seconds long day1=between/(24*3600); long hour1=between% (24*3600)/3600; long minute1=between%3600/60;long second1=between%60/60; System.out.println ("+day1+" Days "+hour1+" hours "+minute1+" minutes "+second1+" seconds ");====================================================java  Compare time Size string s1= "2008-01-25 09:12:09"; String s2= "2008-01-29 09:12:11"; Java.text.dateformat df=new java.text.simpledateformat (" Yyyy-mm-dd hh:mm:ss "); Java.util.calendar c1=java.util.calendar.getinstance (); Java.util.Calendar  c2=java.util.calendar.getinstance (); Try{c1.settime (Df.parse (S1)); C2.settime (Df.parse (S2));} catch (java.text.parseexception e) {System.err.println ("Malformed");} Int result=c1.compareto (C2); if (result==0) System.out.println ("C1 equals C2"); Else if (result<0) SYSTEM.OUT.PRINTLN ("C1 less than C2"); ElseSystem.out.println ("C1 greater than C2");

This article is from the "It Software Development Materials" blog, please be sure to keep this source http://491733638.blog.51cto.com/3913830/1663409

Java Date Comparison size

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.