你的分享就是我们的动力 ---﹥

使用Calendar类计算日期间隔天数

时间:2013-06-07 15:56来源:www.chengxuyuans.com 点击:

代码简介

项目中需要计算两个日期间天数时使用,且可以用来制作相关报表展示

代码片段

/*
 * java.util.Calendar 类学习
 */
package com.itcc.icer;

import java.text.SimpleDateFormat;

/**
 *
 * @author Icer
 */
public class TCalendar {
    
    private static SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMdd");
    private String dayInfo[][];
    private int dayCount;//间隔天数

    
    public static void main(String[] args) {
        String startDate = "20120101";
        String endDate = "20131211";
        TCalendar tCalendar = new TCalendar();
        tCalendar.initDayInfo(startDate, endDate);
        System.out.println("天数: " + tCalendar.getDayCount());
    }
    
    public  void initDayInfo(String start,String end)
    {
    	//初始化日期信息
    	java.util.Calendar cal1=java.util.Calendar.getInstance();
    	java.util.Calendar cal2=java.util.Calendar.getInstance();
    	java.util.Calendar cal3=java.util.Calendar.getInstance();
    	int year,month,day;
    	int i=0;
    	year=Integer.parseInt(start.substring(0,4));
    	month=Integer.parseInt(start.substring(4,6));
    	day=Integer.parseInt(start.substring(6,8));
    	cal1.set(year, month-1, day);
    	cal3.set(year, month-1, day);
    	year=Integer.parseInt(end.substring(0,4));
    	month=Integer.parseInt(end.substring(4,6));
    	day=Integer.parseInt(end.substring(6,8));
    	cal2.set(year, month-1, day);
    	while(!cal2.before(cal3))
    	{	
    		i++;
    		cal3.add(java.util.Calendar.DAY_OF_MONTH, 1);//日期时间+1
    	}
    	//每日数据列表
    	dayInfo=new String[i+1][3];
    	i=0;
    	while(!cal2.before(cal1))
    	{	
                System.out.println("==" + cal1.getTime());
    		dayInfo[i][0]=sDateFormat.format(cal1.getTime());
    		i++;
    		cal1.add(java.util.Calendar.DAY_OF_MONTH, 1);
    	}
    	
    	this.dayCount=i;
    	for (int j=0;j<i;j++)
    	{
    		this.dayInfo[j][1]="0";
    		this.dayInfo[j][2]="0";
    	}
    	
    }
    public int getDayCount() {
        return dayCount;
    }

    public void setDayCount(int dayCount) {
        this.dayCount = dayCount;
    }

    public String[][] getDayInfo() {
        return dayInfo;
    }

    public void setDayInfo(String[][] dayInfo) {
        this.dayInfo = dayInfo;
    }
}

转载注明地址:http://www.chengxuyuans.com/code/android/64194.html