Get the date between two dates

Source: Internet
Author: User
Tags date1

[Java]
/**
* TestDay1.java
* Copyright (C) 2012
* Creation: cuiran 2012-09-24 10:18:52
*/
Package com. wpn. test;
 
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
 
/**
* TODO
* @ Author cuiran
* @ Version TODO
*/
Public class TestDay1 {
 
Public static void twoDays (String str1, String str2 ){
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd ");
Date date1;
Date date2;
Try {
Date1 = df. parse (str1 );
Date2 = df. parse (str2 );
Int s = (int) (date2.getTime ()-date1.getTime ()/(24*60*60*1000 ));
If (s> 0 ){
For (int I = 0; I <= s; I ++ ){
Long todayDate = date1.getTime () + I * 24*60*60*1000;
Date tmDate = new Date (todayDate );
/**
* Yyyy-MM-dd E: Wednesday
*/
System. out. println (new SimpleDateFormat ("yyyy-MM-dd"). format (tmDate ));
}
}
} Catch (ParseException e ){
System. out. println ("format error ");
}
}
 
/**
* TODO
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
TestDay1.twoDays ("2012-09-01", "2012-09-24 ");
}
 
}

It is worth noting that the above method is problematic.

In this way, a problem occurs:

TestDay1.twoDays ("2012-02-01", "2012-02-28 ");

The correct method is as follows:

[Java]
/**
* DateTest. java
* Copyright (C) 2012
* Creation: cuiran 2012-09-24 10:26:29
*/
Package com. wpn. test;
 
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
 
/**
* TODO
* @ Author cuiran
* @ Version TODO
*/
Public class DateTest {
 
String dateFormat = "yyyy-MM-dd ";
SimpleDateFormat format = new SimpleDateFormat (dateFormat );

Public static void main (String [] args ){
DateTest t = new DateTest ();

String date1 = "2012-02-01 ";
String date2 = "2012-03-04 ";

T. process (date1, date2 );
}

Private void process (String date1, String date2 ){
If (date1.equals (date2 )){
System. out. println ("two dates are equal! ");
Return;
}

String tmp;
If (date1.compareTo (date2)> 0) {// ensure that the date of date1 is not later than date2
Tmp = date1;
Date1 = date2;
Date2 = tmp;
}

Tmp = format. format (str2Date (date1). getTime () + 3600*24*1000 );

Int num = 0;
While (tmp. compareTo (date2) <0 ){
System. out. println (tmp );
Num ++;
Tmp = format. format (str2Date (tmp). getTime () + 3600*24*1000 );
}

If (num = 0)
System. out. println ("two dates are adjacent! ");
}


Private Date str2Date (String str ){
If (str = null) return null;

Try {
Return format. parse (str );
} Catch (ParseException e ){
E. printStackTrace ();
}
Return null;
}
}

 

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.