CREATE OR REPLACE FUNCTION day_div(p_day NUMBER) RETURN VARCHAR2 IS i NUMBER(20,14); h INTEGER; m INTEGER; s INTEGER; result VARCHAR2(20) ; d number; BEGIN i:=p_day; --i:=sec/3600; IF trunc(i)>0 THEN d:=trunc(i); i:=i-trunc(i); ELSE d:=0; END IF; IF i*24>0 THEN h:=trunc(i*24); i:=i*24-trunc(i*24); ELSE h:=0; END IF; IF i*60>0 THEN m:=trunc(i*60); i:=i*60-trunc(i*60); ELSE m:=0; end if; if i*60>0 then s:=round(i*60); else s:=0; END IF; result:=to_char(d)||'日'||to_char(h)||'时'||to_char(m)||'分'||to_char(s)||'秒'; RETURN(Result); END day_div; /
long i=date1.getTime(); long j=date2.getTime(); long x=j-i; long diffsecs=x/(1000); long diffmins=x/(60*1000); long diffhours=x/(60*60*1000); long diffdays=x/(24*60*60*1000);
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);
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;
int year = calendar.get(Calendar.YEAR); //得出年分:如2003-8-1就得出2003 int year2 = calendar2.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); //得出月,如上日期得出8 int month2 = calendar2.get(Calendar.MONTH); int day= calendar.get(Calendar.DAY_OF_MONTH); //得出相对于月的日,如上得出1 int day2= calendar2.get(Calendar.DAY_OF_MONTH); int ddd= calendar.get(Calendar.DAY_OF_YEAR); //得出相对于年的日,如上得出213 int ddd2= calendar2.get(Calendar.DAY_OF_YEAR);如果是同一年的日期,如2003-3-3和2003-8-1来比,能得出正确的年、月、日的差数;但如果是隔年的,就会出错,如2002-8-9和2003-8-1比,year - year2、month - month2、day- day2、ddd - ddd2得出的值分别是1、0、-8,这显然就是错的,应该是0(或0.*,至少相差不到一年)年、11月、35*天。其实要实现这个功能,是不难的(我已做出来了),只是不知有没有更好(直接)的方法?因为我用CB时可以直接实现的。
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");
Calendar startTime = new GregorianCalendar(); Calendar endTime = new GregorianCalendar();
RETURN VARCHAR2
IS
i NUMBER(20,14);
h INTEGER;
m INTEGER;
s INTEGER;
result VARCHAR2(20) ;
d number;
BEGIN
i:=p_day;
--i:=sec/3600;
IF trunc(i)>0 THEN
d:=trunc(i);
i:=i-trunc(i);
ELSE
d:=0;
END IF;
IF i*24>0 THEN
h:=trunc(i*24);
i:=i*24-trunc(i*24);
ELSE
h:=0;
END IF;
IF i*60>0 THEN
m:=trunc(i*60);
i:=i*60-trunc(i*60);
ELSE
m:=0;
end if;
if i*60>0 then
s:=round(i*60);
else
s:=0;
END IF;
result:=to_char(d)||'日'||to_char(h)||'时'||to_char(m)||'分'||to_char(s)||'秒';
RETURN(Result);
END day_div;
/
long j=date2.getTime();
long x=j-i;
long diffsecs=x/(1000);
long diffmins=x/(60*1000);
long diffhours=x/(60*60*1000);
long diffdays=x/(24*60*60*1000);
年: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);
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
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
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);
}}
int year = calendar.get(Calendar.YEAR); //得出年分:如2003-8-1就得出2003
int year2 = calendar2.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); //得出月,如上日期得出8
int month2 = calendar2.get(Calendar.MONTH);
int day= calendar.get(Calendar.DAY_OF_MONTH); //得出相对于月的日,如上得出1
int day2= calendar2.get(Calendar.DAY_OF_MONTH);
int ddd= calendar.get(Calendar.DAY_OF_YEAR); //得出相对于年的日,如上得出213
int ddd2= calendar2.get(Calendar.DAY_OF_YEAR);如果是同一年的日期,如2003-3-3和2003-8-1来比,能得出正确的年、月、日的差数;但如果是隔年的,就会出错,如2002-8-9和2003-8-1比,year - year2、month - month2、day- day2、ddd - ddd2得出的值分别是1、0、-8,这显然就是错的,应该是0(或0.*,至少相差不到一年)年、11月、35*天。其实要实现这个功能,是不难的(我已做出来了),只是不知有没有更好(直接)的方法?因为我用CB时可以直接实现的。
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");
Calendar endTime = new GregorianCalendar();
GregorianCalendar.add()
的第一个参数可是年 月 日 等。
所以这么做还是有误差的
-----------
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 ("");
}
}