---
title: "java中关于时间日期操作的常用函数"
author: "abeetle"
date: "2007-05-16T16:30:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2007-05-16T16:30:00Z"
id: 233863
url: "https://abeetle.iteye.com/blog/233863"
---

1.计算某一月份的最大天数  
Calendar time=Calendar.getInstance();  
time.clear();  
time.set(Calendar.YEAR,year);  
time.set(Calendar.MONTH,i-1);//注意,Calendar对象默认一月为0              
int day=time.getActualMaximum(Calendar.DAY\_OF\_MONTH);//本月份的天数  
注：在使用set方法之前，必须先clear一下，否则很多信息会继承自系统当前时间  
2.Calendar和Date的转化  
(1) Calendar转化为Date  
Calendar cal=Calendar.getInstance();  
Date date=cal.getTime();  
(2) Date转化为Calendar  
Date date=new Date();  
Calendar cal=Calendar.getInstance();  
cal.setTime(date);  
3.格式化输出日期时间  
Date date=new Date();  
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
System.out.println(df.format(date));  
4.计算一年中的第几星期  
(1)计算某一天是一年中的第几星期  
Calendar cal=Calendar.getInstance();  
cal.set(Calendar.YEAR, 2006);  
cal.set(Calendar.MONTH, 8);  
cal.set(Calendar.DAY\_OF\_MONTH, 3);  
int weekno=cal.get(Calendar.WEEK\_OF\_YEAR);  
(2)计算一年中的第几星期是几号  
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
Calendar cal=Calendar.getInstance();  
cal.set(Calendar.YEAR, 2006);  
cal.set(Calendar.WEEK\_OF\_YEAR, 1);  
cal.set(Calendar.DAY\_OF\_WEEK, Calendar.MONDAY);  
System.out.println(df.format(cal.getTime()));  
输出:  
2006-01-02  
5.add()和roll()的用法  
(1)add()方法  
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
Calendar cal=Calendar.getInstance();  
cal.set(Calendar.YEAR, 2006);  
cal.set(Calendar.MONTH, 8);  
cal.set(Calendar.DAY\_OF\_MONTH, 3);  
cal.add(Calendar.DATE, -4);  
Date date=cal.getTime();  
System.out.println(df.format(date));  
cal.add(Calendar.DATE, 4);  
date=cal.getTime();  
System.out.println(df.format(date));  
输出：  
    2006-08-30  
    2006-09-03  
(2)roll方法  
cal.set(Calendar.YEAR, 2006);  
cal.set(Calendar.MONTH, 8);  
cal.set(Calendar.DAY\_OF\_MONTH, 3);  
cal.roll(Calendar.DATE, -4);  
date=cal.getTime();  
System.out.println(df.format(date));  
cal.roll(Calendar.DATE, 4);  
date=cal.getTime();  
System.out.println(df.format(date));  
输出：  
    2006-09-29  
    2006-09-03  
可见，roll()方法在本月内循环，一般使用add()方法；  
6.计算两个任意时间中间的间隔天数  
(1)传进Calendar对象

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\*\*计算两个时间之间相隔天数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param startday  开始时间  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param endday 结束时间  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    public int getIntervalDays(Calendar startday,Calendar endday)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //确保startday在endday之前  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if(startday.after(endday))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Calendar cal=startday;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            startday=endday;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            endday=cal;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //分别得到两个时间的毫秒数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        long sl=startday.getTimeInMillis();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        long el=endday.getTimeInMillis();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        long ei=el-sl;      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //根据毫秒数计算间隔天数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return (int)(ei/(1000\*60\*60\*24));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }

(2)传进Date对象

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\*\*计算两个时间之间相隔天数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param startday  开始时间  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param endday 结束时间  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    public int getIntervalDays(Date startday,Date endday)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //确保startday在endday之前  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if(startday.after(endday))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Date cal=startday;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            startday=endday;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            endday=cal;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //分别得到两个时间的毫秒数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        long sl=startday.getTime();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        long el=endday.getTime();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        long ei=el-sl;      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //根据毫秒数计算间隔天数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return (int)(ei/(1000\*60\*60\*24));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }

同理，可以用相同的方法计算出任意两个时间相隔的小时数，分钟数，秒钟数等  
注：以上方法是完全按时间计算，有时并不能令人满意，如：  
startday="2006-10-11 20:00:00"  
endday="2006-10-12 8:00:00"  
计算结果为0，但是我们也许相让计算结果变为1，此时可以用如下方法实现：  
在传参之前，先设定endday的时间，如：  
        endday.set(Calendar.HOUR\_OF\_DAY, 23);  
         endday.set(Calendar.MINUTE, 59);  
        endday.set(Calendar.SECOND, 59);  
        endday.set(Calendar.MILLISECOND, 59);  
这样再传进去startday,endday，则结果就如我们所愿了。不过，如果嫌以上方法麻烦，可以参考以下方法：  
(3)改进精确计算相隔天数的方法

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    public int getDaysBetween (Calendar d1, Calendar d2) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (d1.after(d2)) ...{  // swap dates so that d1 is start and d2 is end  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            java.util.Calendar swap = d1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            d1 = d2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            d2 = swap;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int days = d2.get(Calendar.DAY\_OF\_YEAR) - d1.get(Calendar.DAY\_OF\_YEAR);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        int y2 = d2.get(Calendar.YEAR);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (d1.get(Calendar.YEAR) != y2) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            d1 = (Calendar) d1.clone();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            do ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                days += d1.getActualMaximum(Calendar.DAY\_OF\_YEAR);//得到当年的实际天数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                d1.add(Calendar.YEAR, 1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            } while (d1.get(Calendar.YEAR) != y2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return days;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }

注：getDaysBetween方法参考自：http://java.ccidnet.com/art/3741/20050611/535727\_1.html