您的位置:首页 > 编程语言 > Java开发

java中计算两个日期之间差的天数

2012-12-06 16:57 816 查看
比如两个日期:2005-12-31和2006-01-05,怎么才能精确的计算出这两个日期之间差的天数?

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

class Test2 {

public static String getDate(){

SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");

Date dd = new Date();

return ft.format(dd);

}

public static long getQuot(String time1, String time2){

long quot = 0;

SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");

try {

Date date1 = ft.parse( time1 );

Date date2 = ft.parse( time2 );

quot = date1.getTime() - date2.getTime();

quot = quot / 1000 / 60 / 60 / 24;

} catch (ParseException e) {

e.printStackTrace();

}

return quot;

}

public static void main(String[] args) throws Exception {

String date1 = "2008/8/8";

String date2 = getDate();

long day = getQuot(date1,date2);

System.out.println( "距离 "+date1+" 还有 "+day+" 天" );

}

}

你看一下这段代码,是从今天到2008/8/8的天数。

Date的getTime()方法返回自1970年1月1日午夜(通用时间)以来的毫秒数。

用2008/8/8的getTime()减去今天的getTime()就是这两天相差的毫秒数,1秒=1000毫秒,1分钟=60秒,1小时=60分钟,1天=24小时,然后除除除就得到天数了。

java怎么计算出 某年某月 到 某年某月之间的总月数 ?

//思路 (不考虑开发语言的因素)

// 月份只差其实就是,年份只差 乘以12 然后再加上 月份只差, 比如 2009-09 ----2012-02

// 这个就应该 (2012-2009 )*12 + (2-9)=36 +(-7)=29

//思路有了直接使用java套就可以了

public static void main(String[] args) throws ParseException {

//字符串格式化

DateFormat df = new SimpleDateFormat("yyyy-MM");

//字符串装换为Calendar

Calendar c= Calendar.getInstance();

c.setTime(df.parse("2009-01"));

//字符串装换为Calendar

Calendar d= Calendar.getInstance();

d.setTime(df.parse("2012-01"));

//计算年份只差 乘以12 然后加上月份之差

int m=(d.get(Calendar.YEAR)-c.get(Calendar.YEAR))*12+d.get(Calendar.MONTH)-

c.get(Calendar.MONTH);

System.out.println(m);

}

另外怎么 计算某年某月的天数 ?

题目:计算某年某月的天数

要求:通过键盘接收指定的年和月,判断该月的天数。

要点:月份可分为大月和小月,而二月份的天数跟是否闰年有关,所以对二月份要特殊处理。

class DayTest {
public static void main(String[] args) throws IOException {
int year = -1;
int month = -1;
Scanner in = new Scanner(System.in);
System.out.println("请输入年");
year = in.nextInt();
System.out.println("请输入月");
month = in.nextInt();
System.out.println(year + "年" + month + "月有" + days(year, month) + "天");
}
public static int days(int year, int month) {
int days = 0;
if (month != 2) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
}
} else {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
days = 29;
else
days = 28;
}
return days;
}
}

另附一简单方法:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/** * @author crazy.j * @since 2007-12-17 * @version 1.0 */
public class CalendarTester {
private static SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");

/** * @param args */
public static void main(String[] args) {
String source = "2007年2月";
try {
Date date = format.parse(source);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
System.out
.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  日期