[code]package aaaaaTest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DateTest1 {
/**
* @param args
*/
public static void main(String[] args) {
Map month = new HashMap();
Map day = new HashMap();
List listYear = new ArrayList();
String time = "2006-02-1";
String time2 = "2007-05-2";
DateFormat dfYear = new SimpleDateFormat("yyyy");
DateFormat dfMonth = new SimpleDateFormat("yyyy-MM");
DateFormat dfDay = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dfDay2 = new SimpleDateFormat("dd");
Calendar cDay1 = Calendar.getInstance();
Calendar cDay2 = Calendar.getInstance();
try {
cDay1.setTime(dfDay.parse(time));// 开始日
cDay2.setTime(dfDay.parse(time2));// 结束日
} catch (Exception e) {
e.printStackTrace();
}
// 逆向思考,由天得到月,由月得到年
String monthTemp = ""; //月
String yearTemp = "";// 年
List listDayTemp = null;
List listMonthTemp = null;
while (!cDay1.after(cDay2)) {
Date dayTemp = cDay1.getTime();// 一天
if (dfYear.format(dayTemp).equals(yearTemp)) {// 同一年
if (dfMonth.format(dayTemp).equals(monthTemp)) {// 同一个月
listDayTemp.add(dfDay2.format(dayTemp)); // 增加天
} else {// 不同月
listDayTemp = new ArrayList();// 增加天的列表
listDayTemp.add(dfDay2.format(dayTemp)); // 增加天
monthTemp = dfMonth.format(dayTemp);
listMonthTemp.add(monthTemp);// 增加月
day.put(monthTemp, listDayTemp);
}
} else {// 不同年
yearTemp = dfYear.format(dayTemp);// 设置年
listYear.add(yearTemp);// 增加年=======================================
monthTemp = dfMonth.format(dayTemp);// 设置月
listMonthTemp = new ArrayList();// 增加月列表
listMonthTemp.add(monthTemp);// 增加月
listDayTemp = new ArrayList();// 增加天列表
listDayTemp.add(dfDay2.format(dayTemp));// 增加天
month.put(yearTemp, listMonthTemp);//
day.put(monthTemp, listDayTemp);
}
cDay1.add(Calendar.DAY_OF_MONTH, 1);// 增加一天
}
System.out.println("<table border='0' cellpadding='0' cellspacing='0'>");
System.out.println("<tr>");
for(int i=0;i<listYear.size();i++){
//System.out.println("年:"+listYear.get(i));
List monthList =(List)month.get(listYear.get(i));
for(int j=0;j<monthList.size();j++){//月
System.out.println("<td><table border='1' cellpadding='0' cellspacing='0'>");//一个月一个table(放在外层table中同一行的不同列中),月占一行,日占一行
List dayList =(List)day.get(monthList.get(j));
System.out.println("<tr><td nowrap align='center' colspan='"+dayList.size()+"'>"+monthList.get(j)+"</td></tr>");//月所在的行
System.out.println("<tr>");//日所在的行
for(int k=0;k<dayList.size();k++){//日
System.out.println("<td>"+dayList.get(k)+"</td>");
}
System.out.println("</tr>");//日所在的行结束
System.out.println("</table></td>");
}
}
System.out.println("</tr>");
System.out.println("</table>");
}
}
[/code]
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DateTest1 {
/**
* @param args
*/
public static void main(String[] args) {
Map month = new HashMap();
Map day = new HashMap();
List listYear = new ArrayList();
String time = "2006-02-1";
String time2 = "2007-05-2";
DateFormat dfYear = new SimpleDateFormat("yyyy");
DateFormat dfMonth = new SimpleDateFormat("yyyy-MM");
DateFormat dfDay = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dfDay2 = new SimpleDateFormat("dd");
Calendar cDay1 = Calendar.getInstance();
Calendar cDay2 = Calendar.getInstance();
try {
cDay1.setTime(dfDay.parse(time));// 开始日
cDay2.setTime(dfDay.parse(time2));// 结束日
} catch (Exception e) {
e.printStackTrace();
}
// 逆向思考,由天得到月,由月得到年
String monthTemp = ""; //月
String yearTemp = "";// 年
List listDayTemp = null;
List listMonthTemp = null;
while (!cDay1.after(cDay2)) {
Date dayTemp = cDay1.getTime();// 一天
if (dfYear.format(dayTemp).equals(yearTemp)) {// 同一年
if (dfMonth.format(dayTemp).equals(monthTemp)) {// 同一个月
listDayTemp.add(dfDay2.format(dayTemp)); // 增加天
} else {// 不同月
listDayTemp = new ArrayList();// 增加天的列表
listDayTemp.add(dfDay2.format(dayTemp)); // 增加天
monthTemp = dfMonth.format(dayTemp);
listMonthTemp.add(monthTemp);// 增加月
day.put(monthTemp, listDayTemp);
}
} else {// 不同年
yearTemp = dfYear.format(dayTemp);// 设置年
listYear.add(yearTemp);// 增加年=======================================
monthTemp = dfMonth.format(dayTemp);// 设置月
listMonthTemp = new ArrayList();// 增加月列表
listMonthTemp.add(monthTemp);// 增加月
listDayTemp = new ArrayList();// 增加天列表
listDayTemp.add(dfDay2.format(dayTemp));// 增加天
month.put(yearTemp, listMonthTemp);//
day.put(monthTemp, listDayTemp);
}
cDay1.add(Calendar.DAY_OF_MONTH, 1);// 增加一天
}
System.out.println("<table border='0' cellpadding='0' cellspacing='0'>");
System.out.println("<tr>");
for(int i=0;i<listYear.size();i++){
//System.out.println("年:"+listYear.get(i));
List monthList =(List)month.get(listYear.get(i));
for(int j=0;j<monthList.size();j++){//月
System.out.println("<td><table border='1' cellpadding='0' cellspacing='0'>");//一个月一个table(放在外层table中同一行的不同列中),月占一行,日占一行
List dayList =(List)day.get(monthList.get(j));
System.out.println("<tr><td nowrap align='center' colspan='"+dayList.size()+"'>"+monthList.get(j)+"</td></tr>");//月所在的行
System.out.println("<tr>");//日所在的行
for(int k=0;k<dayList.size();k++){//日
System.out.println("<td>"+dayList.get(k)+"</td>");
}
System.out.println("</tr>");//日所在的行结束
System.out.println("</table></td>");
}
}
System.out.println("</tr>");
System.out.println("</table>");
}
}
[/code]
本文介绍了一个Java程序,该程序能够生成指定日期范围内的年、月、日数据,并以HTML表格形式展示。通过使用SimpleDateFormat进行日期格式化,程序能够处理日期的增减,并将结果按年、月、日组织。
1584

被折叠的 条评论
为什么被折叠?



