[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) {
// // TODO Auto-generated method stub
// int beginYear = 2007;
// int beginMonth = 2;
// int beginDay = 7;
//
// int endYear = 2007;
// int endMonth = 8;
// int endDay = 17;
// GregorianCalendar b = new
// GregorianCalendar(beginYear,beginMonth,beginDay);
// GregorianCalendar e = new GregorianCalendar(endYear,endMonth,endDay);
Map year = new HashMap();
Map month = new HashMap();
Map day = new HashMap();
List listYear = new ArrayList();
List listMonth = new ArrayList();
List listDay = new ArrayList();
String time = "2006-02-1";
String time2 = "2008-05-2";
DateFormat dfYear = new SimpleDateFormat("yyyy");
DateFormat dfMonth = new SimpleDateFormat("yyyy-MM");
DateFormat dfDay = new SimpleDateFormat("yyyy-MM-dd");
/** ***************************************************************************** */
Calendar cYear1 = Calendar.getInstance();
Calendar cYear2 = Calendar.getInstance();
Calendar cMonth1 = Calendar.getInstance();
Calendar cMonth2 = Calendar.getInstance();
Calendar cDay1 = Calendar.getInstance();
Calendar cDay2 = Calendar.getInstance();
try {
// cYear1.setTime(dfDay.parse(time));
// cYear2.setTime(dfDay.parse(time2));
//
// cMonth1.setTime(dfDay.parse(time));
// cMonth2.setTime(dfDay.parse(time2));
cDay1.setTime(dfDay.parse(time));// 开始日
cDay2.setTime(dfDay.parse(time2));// 结束日
} catch (Exception e) {
System.out.println("异常发生!!!111111111");
e.printStackTrace();
}
// 逆向思考,由天得到月,由月得到年
String monthTemp = ""; // dfMonth.format(cDay1.getTime());//月
String yearTemp = "";// dfYear.format(cDay1.getTime());//年
List listDayTemp = null;// new ArrayList();
List listMonthTemp = null;// new ArrayList();
while (!cDay1.after(cDay2)) {
// listDay.add(dfDay.format(cDay1.getTime()));//天列表
Date dayTemp = cDay1.getTime();// 一天
if (dfYear.format(dayTemp).equals(yearTemp)) {// 同一年
if (dfMonth.format(dayTemp).equals(monthTemp)) {// 同一个月
listDayTemp.add(dfDay.format(dayTemp)); // 增加天
} else {// 不同月
listDayTemp = new ArrayList();// 增加天的列表
monthTemp = dfMonth.format(dayTemp);
// if (!day.containsKey(monthTemp)) {
// day.remove(monthTemp);
// day.put(monthTemp, listDayTemp);
// }
// if(!month.containsKey(yearTemp)){
// month.remove(yearTemp);
// month.put(yearTemp, listMonthTemp);
// }
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(dfDay.format(dayTemp));// 增加天
month.put(yearTemp, listMonthTemp);//
day.put(monthTemp, listDayTemp);
}
cDay1.add(Calendar.DAY_OF_MONTH, 1);// 增加一天
}
//System.out.println("年:\t" + listYear);
for(int i=0;i<listYear.size();i++){
System.out.println();
System.out.println();
System.out.println("年:"+listYear.get(i));
System.out.println();
System.out.println();
List monthList =(List)month.get(listYear.get(i));
System.out.println();
System.out.println();
for(int j=0;j<monthList.size();j++){
System.out.print("月:"+monthList.get(j));
if((j+1)%9==0){
System.out.println();
}
System.out.println();
System.out.println();
List dayList =(List)day.get(monthList.get(j));
for(int k=0;k<dayList.size();k++){
System.out.print("日:"+dayList.get(k));
if((k+1)%10==0){
System.out.println();
}
}
System.out.println();
System.out.println();
}
System.out.println();
System.out.println();
System.out.println();
}
// for(int i=0;i<listMonth.size();i++){
// System.out.println("月:"+listMonth.get(i));
//
// }
// for(int i=0;i<listDay.size();i++){
// System.out.println("日:"+listDay.get(i));
// }
// if(dfYear.format(dayTemp).equals(yearTemp)){//同一年
// //listMonthTemp.add();//增加月
// if (dfMonth.format(dayTemp).equals(monthTemp)) {//同一个月
// listDayTemp.add(dfDay.format(dayTemp)); //增加天
// }else{//不同月
// listDayTemp = new ArrayList();//增加天的列表
// listMonthTemp.add(yearTemp);//增加月
// }
// }else{//不同年
// yearTemp = dfYear.format(dayTemp);//设置年
// listYear.add(yearTemp);//增加年
// monthTemp = dfMonth.format(dayTemp);//设置月
// listMonthTemp.add(monthTemp);//增加月
// listDayTemp.add(dfDay.format(dayTemp));//增加天
// }
// //-------------------------------------------
// if (dfMonth.format(dayTemp).equals(monthTemp)) {//同一个月
// listDayTemp.add(dfDay.format(dayTemp)); //增加天
// }else{//不同月
// listDayTemp = new ArrayList();//增加天的列表
// }
// dfDay.format(cDay1.getTime());
// oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
// while (!cYear1.after(cYear2)) {
// listYear.add(dfYear.format(cYear1.getTime()));// 得到月年
// cYear1.add(Calendar.YEAR, 1);// 每次增加一年
// }
// //处理月=========================================================================================================================
// int lenYear = listYear.size();//年数
// if(lenYear==1){//一年
// try {
// cMonth1.setTime(dfDay.parse(time));
// cMonth2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// while (!cMonth1.after(cMonth2)) {//得到月的列表
// listMonth.add(dfMonth.format(cMonth1.getTime()));
// cMonth1.add(Calendar.MONTH, 1);// 每次增加一月
// }
// year.put(listYear.get(0), listMonth);//
// //处理天-----------------------------------------------------------------------------
// int lenMonth = listMonth.size();//月数
// if(lenMonth==1){//只有一个月
// try {
// cDay1.setTime(dfDay.parse(time));
// cDay2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// while(!cDay1.after(cDay2)){
// listDay.add(dfDay.format(cDay1.getTime()));
// cDay1.add(Calendar.DAY_OF_MONTH, 1);// 每次增加一天
// }
// }else{//多个月
//
// }
//
// }else{//多年
// for (int i = 0; i < lenYear; i++) {
// if (i == 0) {
// try {
// cMonth1.setTime(dfDay.parse(time));
//
// cMonth2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// } else if (i == lenYear - 1) {
// try {
// cMonth1.setTime(dfDay.parse(time));
// cMonth2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// } else {
// try {
// cMonth1.setTime(dfDay.parse(time));
// cMonth2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// }
// }
// }
// while(!cYear1.after(cYear2)){
// listYear.add(dfYear.format(cYear1.getTime()));//得到月年
// cYear1.add(Calendar.YEAR, 1);//每次增加一年
// }
//
// while(!cMonth1.after(cMonth2)){
// listMonth.add(dfMonth.format(cMonth1.getTime()));//得到月年
// cMonth1.add(Calendar.MONTH, 1);//每次增加一月
// }
// while(!cDay1.after(cDay2)){
// listDay.add(dfDay.format(cDay1.getTime()));//得到月年
// cDay1.add(Calendar.DAY_OF_MONTH, 1);//每次增加一天
// }
// System.out.println("年份如下:");
// System.out.println();
// for(int i = 0;i<listYear.size();i++){
// System.out.print(" "+listYear.get(i));
// if((i+1)%5==0)System.out.println();
// }
// System.out.println();
// System.out.println();
// System.out.println("月份如下:");
// System.out.println();
// for(int i = 0;i<listMonth.size();i++){
// System.out.print(" "+listMonth.get(i));
// if((i+1)%5==0)System.out.println();
// }
// System.out.println();
// System.out.println();
// System.out.println("天如下:");
// for(int i = 0;i<listDay.size();i++){
// System.out.print(" "+listDay.get(i));
// if((i+1)%5==0)System.out.println();
// }
// for(int i =0;i<listYear.size();i++){
// month.put(listYear.get(i), listMonth);
// }
/** ******************************************************************************* */
//
//
// Calendar c1 = Calendar.getInstance();
// Calendar c2 = Calendar.getInstance();
// try{
// System.out.println("00000000000000 "+dfDay.parse(time));
// c1.setTime(dfDay.parse(time));
// c2.setTime(dfDay.parse(time2));
// }catch(Exception e3){
// System.out.println("wrong occured");
// }
// //**********************************************************
//
// while(!c1.after(c2)){
// //listYear.add(dfDay.);
// listMonth.add(dfMonth.format(c1.getTime()));//得到月年
// c1.add(Calendar.MONTH, 1);//每次增加一月
// }
//
// //*********************************************************
//
// List list = new ArrayList();
// while(!c1.after(c2)){
// list.add(dfDay.format(c1.getTime()));
// //c1.add(Calendar.MONTH, 1);
// c1.add(Calendar.DAY_OF_MONTH, 1);//控制增加的单位
//
// }
// for(int i=0;i<list.size();i++){
// System.out.print(" "+list.get(i));
// if((i+1)%5==0)System.out.println();
// }
// System.out.println(list);
}
}
[/code]
[color=red]修改后,主要是去掉多余代码[/color]
[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 = "2008-05-2";
DateFormat dfYear = new SimpleDateFormat("yyyy");
DateFormat dfMonth = new SimpleDateFormat("yyyy-MM");
DateFormat dfDay = new SimpleDateFormat("yyyy-MM-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(dfDay.format(dayTemp)); // 增加天
} else {// 不同月
listDayTemp = new ArrayList();// 增加天的列表
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(dfDay.format(dayTemp));// 增加天
month.put(yearTemp, listMonthTemp);//
day.put(monthTemp, listDayTemp);
}
cDay1.add(Calendar.DAY_OF_MONTH, 1);// 增加一天
}
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.print("月:"+monthList.get(j));
if((j+1)%5==0){
System.out.println("\n\n\n");
}
System.out.println("\n\n\n");
List dayList =(List)day.get(monthList.get(j));
for(int k=0;k<dayList.size();k++){
System.out.print("日:"+dayList.get(k));
if((k+1)%5==0){
System.out.println("\n\n\n");
}
}
}
}
}
}
[/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) {
// // TODO Auto-generated method stub
// int beginYear = 2007;
// int beginMonth = 2;
// int beginDay = 7;
//
// int endYear = 2007;
// int endMonth = 8;
// int endDay = 17;
// GregorianCalendar b = new
// GregorianCalendar(beginYear,beginMonth,beginDay);
// GregorianCalendar e = new GregorianCalendar(endYear,endMonth,endDay);
Map year = new HashMap();
Map month = new HashMap();
Map day = new HashMap();
List listYear = new ArrayList();
List listMonth = new ArrayList();
List listDay = new ArrayList();
String time = "2006-02-1";
String time2 = "2008-05-2";
DateFormat dfYear = new SimpleDateFormat("yyyy");
DateFormat dfMonth = new SimpleDateFormat("yyyy-MM");
DateFormat dfDay = new SimpleDateFormat("yyyy-MM-dd");
/** ***************************************************************************** */
Calendar cYear1 = Calendar.getInstance();
Calendar cYear2 = Calendar.getInstance();
Calendar cMonth1 = Calendar.getInstance();
Calendar cMonth2 = Calendar.getInstance();
Calendar cDay1 = Calendar.getInstance();
Calendar cDay2 = Calendar.getInstance();
try {
// cYear1.setTime(dfDay.parse(time));
// cYear2.setTime(dfDay.parse(time2));
//
// cMonth1.setTime(dfDay.parse(time));
// cMonth2.setTime(dfDay.parse(time2));
cDay1.setTime(dfDay.parse(time));// 开始日
cDay2.setTime(dfDay.parse(time2));// 结束日
} catch (Exception e) {
System.out.println("异常发生!!!111111111");
e.printStackTrace();
}
// 逆向思考,由天得到月,由月得到年
String monthTemp = ""; // dfMonth.format(cDay1.getTime());//月
String yearTemp = "";// dfYear.format(cDay1.getTime());//年
List listDayTemp = null;// new ArrayList();
List listMonthTemp = null;// new ArrayList();
while (!cDay1.after(cDay2)) {
// listDay.add(dfDay.format(cDay1.getTime()));//天列表
Date dayTemp = cDay1.getTime();// 一天
if (dfYear.format(dayTemp).equals(yearTemp)) {// 同一年
if (dfMonth.format(dayTemp).equals(monthTemp)) {// 同一个月
listDayTemp.add(dfDay.format(dayTemp)); // 增加天
} else {// 不同月
listDayTemp = new ArrayList();// 增加天的列表
monthTemp = dfMonth.format(dayTemp);
// if (!day.containsKey(monthTemp)) {
// day.remove(monthTemp);
// day.put(monthTemp, listDayTemp);
// }
// if(!month.containsKey(yearTemp)){
// month.remove(yearTemp);
// month.put(yearTemp, listMonthTemp);
// }
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(dfDay.format(dayTemp));// 增加天
month.put(yearTemp, listMonthTemp);//
day.put(monthTemp, listDayTemp);
}
cDay1.add(Calendar.DAY_OF_MONTH, 1);// 增加一天
}
//System.out.println("年:\t" + listYear);
for(int i=0;i<listYear.size();i++){
System.out.println();
System.out.println();
System.out.println("年:"+listYear.get(i));
System.out.println();
System.out.println();
List monthList =(List)month.get(listYear.get(i));
System.out.println();
System.out.println();
for(int j=0;j<monthList.size();j++){
System.out.print("月:"+monthList.get(j));
if((j+1)%9==0){
System.out.println();
}
System.out.println();
System.out.println();
List dayList =(List)day.get(monthList.get(j));
for(int k=0;k<dayList.size();k++){
System.out.print("日:"+dayList.get(k));
if((k+1)%10==0){
System.out.println();
}
}
System.out.println();
System.out.println();
}
System.out.println();
System.out.println();
System.out.println();
}
// for(int i=0;i<listMonth.size();i++){
// System.out.println("月:"+listMonth.get(i));
//
// }
// for(int i=0;i<listDay.size();i++){
// System.out.println("日:"+listDay.get(i));
// }
// if(dfYear.format(dayTemp).equals(yearTemp)){//同一年
// //listMonthTemp.add();//增加月
// if (dfMonth.format(dayTemp).equals(monthTemp)) {//同一个月
// listDayTemp.add(dfDay.format(dayTemp)); //增加天
// }else{//不同月
// listDayTemp = new ArrayList();//增加天的列表
// listMonthTemp.add(yearTemp);//增加月
// }
// }else{//不同年
// yearTemp = dfYear.format(dayTemp);//设置年
// listYear.add(yearTemp);//增加年
// monthTemp = dfMonth.format(dayTemp);//设置月
// listMonthTemp.add(monthTemp);//增加月
// listDayTemp.add(dfDay.format(dayTemp));//增加天
// }
// //-------------------------------------------
// if (dfMonth.format(dayTemp).equals(monthTemp)) {//同一个月
// listDayTemp.add(dfDay.format(dayTemp)); //增加天
// }else{//不同月
// listDayTemp = new ArrayList();//增加天的列表
// }
// dfDay.format(cDay1.getTime());
// oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
// while (!cYear1.after(cYear2)) {
// listYear.add(dfYear.format(cYear1.getTime()));// 得到月年
// cYear1.add(Calendar.YEAR, 1);// 每次增加一年
// }
// //处理月=========================================================================================================================
// int lenYear = listYear.size();//年数
// if(lenYear==1){//一年
// try {
// cMonth1.setTime(dfDay.parse(time));
// cMonth2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// while (!cMonth1.after(cMonth2)) {//得到月的列表
// listMonth.add(dfMonth.format(cMonth1.getTime()));
// cMonth1.add(Calendar.MONTH, 1);// 每次增加一月
// }
// year.put(listYear.get(0), listMonth);//
// //处理天-----------------------------------------------------------------------------
// int lenMonth = listMonth.size();//月数
// if(lenMonth==1){//只有一个月
// try {
// cDay1.setTime(dfDay.parse(time));
// cDay2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// while(!cDay1.after(cDay2)){
// listDay.add(dfDay.format(cDay1.getTime()));
// cDay1.add(Calendar.DAY_OF_MONTH, 1);// 每次增加一天
// }
// }else{//多个月
//
// }
//
// }else{//多年
// for (int i = 0; i < lenYear; i++) {
// if (i == 0) {
// try {
// cMonth1.setTime(dfDay.parse(time));
//
// cMonth2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// } else if (i == lenYear - 1) {
// try {
// cMonth1.setTime(dfDay.parse(time));
// cMonth2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// } else {
// try {
// cMonth1.setTime(dfDay.parse(time));
// cMonth2.setTime(dfDay.parse(time2));
// } catch (Exception e) {
// }
// }
// }
// }
// while(!cYear1.after(cYear2)){
// listYear.add(dfYear.format(cYear1.getTime()));//得到月年
// cYear1.add(Calendar.YEAR, 1);//每次增加一年
// }
//
// while(!cMonth1.after(cMonth2)){
// listMonth.add(dfMonth.format(cMonth1.getTime()));//得到月年
// cMonth1.add(Calendar.MONTH, 1);//每次增加一月
// }
// while(!cDay1.after(cDay2)){
// listDay.add(dfDay.format(cDay1.getTime()));//得到月年
// cDay1.add(Calendar.DAY_OF_MONTH, 1);//每次增加一天
// }
// System.out.println("年份如下:");
// System.out.println();
// for(int i = 0;i<listYear.size();i++){
// System.out.print(" "+listYear.get(i));
// if((i+1)%5==0)System.out.println();
// }
// System.out.println();
// System.out.println();
// System.out.println("月份如下:");
// System.out.println();
// for(int i = 0;i<listMonth.size();i++){
// System.out.print(" "+listMonth.get(i));
// if((i+1)%5==0)System.out.println();
// }
// System.out.println();
// System.out.println();
// System.out.println("天如下:");
// for(int i = 0;i<listDay.size();i++){
// System.out.print(" "+listDay.get(i));
// if((i+1)%5==0)System.out.println();
// }
// for(int i =0;i<listYear.size();i++){
// month.put(listYear.get(i), listMonth);
// }
/** ******************************************************************************* */
//
//
// Calendar c1 = Calendar.getInstance();
// Calendar c2 = Calendar.getInstance();
// try{
// System.out.println("00000000000000 "+dfDay.parse(time));
// c1.setTime(dfDay.parse(time));
// c2.setTime(dfDay.parse(time2));
// }catch(Exception e3){
// System.out.println("wrong occured");
// }
// //**********************************************************
//
// while(!c1.after(c2)){
// //listYear.add(dfDay.);
// listMonth.add(dfMonth.format(c1.getTime()));//得到月年
// c1.add(Calendar.MONTH, 1);//每次增加一月
// }
//
// //*********************************************************
//
// List list = new ArrayList();
// while(!c1.after(c2)){
// list.add(dfDay.format(c1.getTime()));
// //c1.add(Calendar.MONTH, 1);
// c1.add(Calendar.DAY_OF_MONTH, 1);//控制增加的单位
//
// }
// for(int i=0;i<list.size();i++){
// System.out.print(" "+list.get(i));
// if((i+1)%5==0)System.out.println();
// }
// System.out.println(list);
}
}
[/code]
[color=red]修改后,主要是去掉多余代码[/color]
[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 = "2008-05-2";
DateFormat dfYear = new SimpleDateFormat("yyyy");
DateFormat dfMonth = new SimpleDateFormat("yyyy-MM");
DateFormat dfDay = new SimpleDateFormat("yyyy-MM-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(dfDay.format(dayTemp)); // 增加天
} else {// 不同月
listDayTemp = new ArrayList();// 增加天的列表
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(dfDay.format(dayTemp));// 增加天
month.put(yearTemp, listMonthTemp);//
day.put(monthTemp, listDayTemp);
}
cDay1.add(Calendar.DAY_OF_MONTH, 1);// 增加一天
}
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.print("月:"+monthList.get(j));
if((j+1)%5==0){
System.out.println("\n\n\n");
}
System.out.println("\n\n\n");
List dayList =(List)day.get(monthList.get(j));
for(int k=0;k<dayList.size();k++){
System.out.print("日:"+dayList.get(k));
if((k+1)%5==0){
System.out.println("\n\n\n");
}
}
}
}
}
}
[/code]
本文介绍了一种使用Java遍历两个日期之间的所有日期的方法,并按年、月、日进行组织显示。通过Calendar和SimpleDateFormat类实现了从起始日期到结束日期的逐日遍历,同时记录并打印出遍历过程中的年份、月份和具体日期。

863

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



