两个日期型变量都有被始值date1,date2,怎样得出它们的时间差:相差几年、相差几个月、相差风天?
先转化成Calender: calc1, calc2
年:year = calc1.get(Calender.YEAR)-calc2.get(Calender.YEAR);
月:month = year*12 + calc1.get(Calender.MONTH)-calc2.get(Calender.MONTH);
日:day = (date1.getTime()-date2.getTime())/(24*60*60*1000);
用 Calender,这才是日历类 !! 用来进行年月日的计算...
------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
public class TestDate {
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar(1992, 10, 15);
Calendar calendar2 = new GregorianCalendar(1982, 9, 14);
int year = calendar.get(Calendar.YEAR);
int year2 = calendar2.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int month2 = calendar2.get(Calendar.MONTH);
int hour = calendar.get(Calendar.DAY_OF_MONTH);
// Calendar.DAY_OF_YEAR 这样就是相差的具体日
int hour2 = calendar2.get(Calendar.DAY_OF_MONTH);
System.out.println("相差 " + String.valueOf(year - year2) + " 年");
System.out.println("相差 " + String.valueOf(month - month2) + " 月");
System.out.println("相差 " + String.valueOf(hour - hour2) + " 日");
}
}
可以用Calendar类中的setTime把Date转为Calendar
agree with the above guey.but
do not have to convert DATE to CALENDER.
for example:(year)
package ddd;
import java.io.*;
import java.util.Date;
public class dddd{
Date d1;
Date d2;
dddd(){
d1=new Date();
d2=new Date();
}
public static void main(String[] args){
dddd t1=new dddd();
int i1=t1.d1.getYear()+1900;//get the year of date ;
int i2=t1.d2.getYear()+1900;
System.out.print(i1);
System.out.println();
System.out.print(i1-i2);
}
}
Date trialTime = new Date();
startTime.setTime(trialTime);
...........
trialTime = new Date();
endTime.setTime(trialTime);
endTime.add(Calendar.MILLISECOND , - startTime.get(Calendar.MILLISECOND));
endTime.add(Calendar.SECOND , - startTime.get(Calendar.SECOND));
endTime.add(Calendar.MINUTE , - startTime.get(Calendar.MINUTE));
endTime.add(Calendar.HOUR , - startTime.get(Calendar.HOUR_OF_DAY));
result = result + (endTime.get(Calendar.HOUR_OF_DAY) == 0
? "" : endTime.get(Calendar.HOUR_OF_DAY)+ "Hours");
result = result + (endTime.get(Calendar.MINUTE) == 0
? "" : endTime.get(Calendar.MINUTE)+ "Mintutes");
result = result + (endTime.get(Calendar.SECOND) == 0
? "" : endTime.get(Calendar.SECOND)+ "s ");
result = result + (endTime.get(Calendar.MILLISECOND) == 0
? "" : endTime.get(Calendar.MILLISECOND)+ "ms");
4 个月之前做的一个,当时也考虑到闰年之类的问题,一直没有找到合适的方法,
所以这么做还是有误差的。。。
-----------
import java.util.*;
import java.text.*;
public class TimeDiff
{
public static void main (String[] args)
{
//java TimeDiff "2003-03-25 00:00:01" "2003-03-24 23:59:59"
if (args.length < 2)
{
System.out.println ("错误:缺少参数!");
ShowUsage ();
return;
}
System.out.println ("");
Date theBaseDate;
Date theDate;
SimpleDateFormat theDateTimeFormat = new SimpleDateFormat ("y-MM-dd HH:mm:ss", Locale.CHINA);
try
{
theBaseDate = theDateTimeFormat.parse (args[0]);
//System.out.println ("基准日期 = " + new java.sql.Timestamp (theBaseDate.getTime()) );
theDate = theDateTimeFormat.parse (args[1]);
//System.out.print ("比较日期 = " + new java.sql.Timestamp (theDate.getTime()) );
//System.out.println ("(" + theDate.getTime() + ")" );
}
catch (ParseException theException)
{
System.out.println ("错误:请检查日期格式是否输入正确!");
System.out.println (theException);
return;
}
Date theDiffDate = new Date ();
//System.out.println ("时间差 = " + new java.sql.Timestamp (theDiffDate.getTime()) );
//System.out.println ("时间差 = " + (theDate.getTime() - theBaseDate.getTime()) );
//ShowTimeDiff (theBaseDate, theDate);
ShowTimeDiff (theDate.getTime() - theBaseDate.getTime());
}
public static void ShowUsage ()
{
System.out.println ("----------------------------------------------");
System.out.println ("* 使用方法: TimeDiff 基准时间 待比较时间");
System.out.println ("***** 日期格式:yyyy-mm-dd hh:mm:ss");
System.out.println ("***** 示例: java TimeDiff \"2003-03-25 00:00:01\" \"2003-03-24 23:59:59\"");
}
public static void ShowTimeDiff (Date theBaseDate, Date theDate)
{
System.out.println ("----------ShowTimeDiff (Date, Date)-----------");
SimpleTimeZone theTimeZone = new SimpleTimeZone (8*3600*1000, "亚洲/中国 山东、北京、上海、广州、深圳");
GregorianCalendar theBaseCalendar = new GregorianCalendar (theTimeZone, Locale.CHINA);
GregorianCalendar theCalendar = new GregorianCalendar (theTimeZone, Locale.CHINA);
theBaseCalendar.setTime (theBaseDate);
theCalendar.setTime (theDate);
}
public static void ShowTimeDiff (long DiffTime)
{
long theDiffTimeInSecond = DiffTime/1000;
long theDiffTimeInSecond_abs = Math.abs (theDiffTimeInSecond);
/*
System.out.println ("----------ShowTimeDiff (long DiffTime)--------");
System.out.println ("时间差(毫秒) = "+DiffTime);
System.out.println ("时间差(秒) = "+theDiffTimeInSecond);
System.out.println ("时间差(秒;绝对值) = "+theDiffTimeInSecond_abs);
System.out.println ("");
System.out.println ("1 年 = " + (365*24*3600) + " 秒");
System.out.println ("30日 = " + (30*24*3600) + " 秒");
System.out.println ("1 日 = " + (1*24*3600) + " 秒");
System.out.println ("");
*/
final int SHOW_LEVEL = 2;
int NowLevel = 0;
if (theDiffTimeInSecond_abs / (365*24*3600) > 0)
{
if (NowLevel < SHOW_LEVEL)
{
if (NowLevel>0) System.out.print ("零 "); // 如果已经输出了上一级(NowLevel>0),则在前面加“0”
System.out.print (theDiffTimeInSecond_abs / (365*24*3600) + " 年");
//theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - theDiffTimeInSecond_abs / (365*24*3600);
theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - (theDiffTimeInSecond_abs/(365*24*3600))*(365*24*3600);
//System.out.println (" " + theDiffTimeInSecond_abs );
NowLevel ++;
}
}
if (theDiffTimeInSecond_abs / (30*24*3600) > 0)
{
if (NowLevel < SHOW_LEVEL)
{
if (NowLevel>0) System.out.print ("零 "); // 如果已经输出了上一级(NowLevel>0),则在前面加“0”
System.out.print (theDiffTimeInSecond_abs / (30*24*3600) + " 个月");
//theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - theDiffTimeInSecond_abs / (30*24*3600);
theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - (theDiffTimeInSecond_abs / (30*24*3600)) * (30*24*3600);
//System.out.println (" " + theDiffTimeInSecond_abs );
NowLevel ++;
}
}
if (theDiffTimeInSecond_abs / (1*24*3600) > 0)
{
if (NowLevel < SHOW_LEVEL)
{
if (NowLevel>0) System.out.print ("零 "); // 如果已经输出了上一级(NowLevel>0),则在前面加“0”
System.out.print (theDiffTimeInSecond_abs / (1*24*3600) + " 天");
//theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - theDiffTimeInSecond_abs / (1*24*3600);
theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - (theDiffTimeInSecond_abs / (1*24*3600))*(1*24*3600);
//System.out.println (" " + theDiffTimeInSecond_abs );
NowLevel ++;
}
}
if (theDiffTimeInSecond_abs / (1*1*3600) > 0)
{
if (NowLevel < SHOW_LEVEL)
{
if (NowLevel>0) System.out.print ("零 "); // 如果已经输出了上一级(NowLevel>0),则在前面加“0”
System.out.print (theDiffTimeInSecond_abs / (1*1*3600) + " 小时");
//theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - theDiffTimeInSecond_abs / (1*1*3600);
theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - (theDiffTimeInSecond_abs / (1*1*3600)) * (1*1*3600);
//System.out.println (" " + theDiffTimeInSecond_abs );
NowLevel ++;
}
}
if (theDiffTimeInSecond_abs / (1*1*60) > 0)
{
if (NowLevel < SHOW_LEVEL)
{
if (NowLevel>0) System.out.print ("零 "); // 如果已经输出了上一级(NowLevel>0),则在前面加“0”
System.out.print (theDiffTimeInSecond_abs / (1*1*60) + " 分");
//theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - theDiffTimeInSecond_abs / (1*1*60);
theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - (theDiffTimeInSecond_abs / (1*1*60)) * (1*1*60);
//System.out.println (" " + theDiffTimeInSecond_abs );
NowLevel ++;
}
}
if (theDiffTimeInSecond_abs / (1*1*1) > 0)
{
if (NowLevel < SHOW_LEVEL)
{
if (NowLevel>0) System.out.print ("零 "); // 如果已经输出了上一级(NowLevel>0),则在前面加“0”
System.out.print (theDiffTimeInSecond_abs / (1*1*1) + " 秒");
//System.out.println (" " + theDiffTimeInSecond_abs );
//theDiffTimeInSecond_abs = theDiffTimeInSecond_abs - theDiffTimeInSecond_abs / (1*1*1);
NowLevel ++;
}
}
if (theDiffTimeInSecond < 0)
System.out.print ("之前");
else if (theDiffTimeInSecond > 0)
System.out.print ("之后");
System.out.println ("");
}
}