Completion of missing date data JAVA Implementation

Source: Internet
Author: User

Public class test {

// The time ranges from to are missing: 01, 03, 04, 08, and 10)
Private Static string [] db_date =
{"2013-06-02", "2013-06-05", "2013-06-06"
, "2013-06-07", "2013-06-09 "};
// Result set
Private Static arraylist <string> dateresult = new arraylist <string> ();
Public static void main (string ARGs []) {

// Increase the time by one day
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd ");
Calendar yesterday = calendar. getinstance ();
Yesterday. Add (calendar. day_of_month, 1 );
String datestr = SDF. Format (yesterday. gettime ());
System. Out. println (datestr );

// Compare the date (How many days is the difference), and the time can also be compared
Calendar now = calendar. getinstance ();
System. Out. println (yesterday. compareto (now ));

// Convert string to time
Calendar calendar10 = calendar. getinstance ();
Calendar calendar5 = calendar. getinstance ();
// Process date data cyclically and complete the missing date. 10 is the number of days in a time period, and 5 is the number of days in the date set to be processed
Try {
Date = SDF. parse ("2013-06-01 ");
Calendar10.settime (date );
} Catch (parseexception e ){
E. printstacktrace ();
}
For (INT curr = 0; curr <10; curr ++ ){

Boolean dbdataexist = false;
Int Index = 0;
For (INT I = 0; I <5; I ++ ){
Try {
Date date2 = SDF. parse (db_date [I]);
Calendar5.settime (date2 );
} Catch (parseexception e ){
E. printstacktrace ();
}
If (calendar10.compareto (calendar5) = 0 ){
Dbdataexist = true;
Index = I;
Break;
}
}
If (dbdataexist ){
Dateresult. Add (db_date [Index]);
} Else {
Dateresult. Add (SDF. Format (calendar10.gettime ()));
}
// Restore calendar10
Calendar10.add (calendar. day_of_month, 1 );
}

// Print the result
For (string STR: dateresult ){
System. Out. println (STR );
}

}
}

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.