public class Test {
//缺少01、03、04、08、10三个时间(从2013-06-01到2013-06-10)
private static String [] db_date =
{"2013-06-02","2013-06-05","2013-06-06"
,"2013-06-07","2013-06-09"};
//结果集合
private static ArrayList<String> dateResult = new ArrayList<String>();
public static void main(String args[]){
//时间增加一天
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);
//日期比较(求差多少天),时间也可以比较
Calendar now = Calendar.getInstance();
System.out.println(yesterday.compareTo(now));
//字符串转化为时间
Calendar calendar10 = Calendar.getInstance();
Calendar calendar5 = Calendar.getInstance();
//循环处理日期数据,把缺失的日期补全。10是时间段内的天数,5是要处理的日期集合的天数
try {
Date 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()));
}
//还原calendar10
calendar10.add(Calendar.DAY_OF_MONTH, 1 );
}
//打印结果
for(String str : dateResult){
System.out.println(str);
}
}
}
//缺少01、03、04、08、10三个时间(从2013-06-01到2013-06-10)
private static String [] db_date =
{"2013-06-02","2013-06-05","2013-06-06"
,"2013-06-07","2013-06-09"};
//结果集合
private static ArrayList<String> dateResult = new ArrayList<String>();
public static void main(String args[]){
//时间增加一天
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);
//日期比较(求差多少天),时间也可以比较
Calendar now = Calendar.getInstance();
System.out.println(yesterday.compareTo(now));
//字符串转化为时间
Calendar calendar10 = Calendar.getInstance();
Calendar calendar5 = Calendar.getInstance();
//循环处理日期数据,把缺失的日期补全。10是时间段内的天数,5是要处理的日期集合的天数
try {
Date 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()));
}
//还原calendar10
calendar10.add(Calendar.DAY_OF_MONTH, 1 );
}
//打印结果
for(String str : dateResult){
System.out.println(str);
}
}
}