一个sql类的Date ,我要怎么在那个日期上加上指定天数,然后获得的还是一个sql类的Date?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个sql类的Date ,我要怎么在那个日期上加上指定天数,然后获得的还是一个sql类的Date?相关的知识,希望对你有一定的参考价值。

一个食品有生产日期,也有保质期,假设从数据库中获得A食品生产日期是2011-11-11日,然后保质期是180天,那么我要得到的是180天后是那年那月那日,并且是一个Date对象。请问这个好怎么做呢?
那SQL的Date和util的Date怎么互相转换了?麻烦了哈

请看清楚是 Java中的 不是数据库

//***************************************************
//名称:strToDate
//功能:将指定的字符串转换成日期
//输入:aStrValue: 要转换的字符串;
// aFmtDate: 转换日期的格式, 默认为: "yyyy/MM/dd "
// aDteRtn: 转换后的日期
//输出:
//返回:TRUE: 是正确的日期格式; FALSE: 是错误的日期格式
//***************************************************
public static boolean strToDate(
String aStrValue,
String aFmtDate,
java.util.Date aDteRtn)

if (aFmtDate.length() == 0)

aFmtDate = "yyyy/MM/dd ";

SimpleDateFormat fmtDate = new SimpleDateFormat(aFmtDate);
try

aDteRtn.setTime(fmtDate.parse(aStrValue).getTime());

catch (Exception e)

return (false);


return (true);


//***************************************************
//名称:dateToStr
//功能:将指定的日期转换成字符串
//输入:aDteValue: 要转换的日期;
// aFmtDate: 转换日期的格式, 默认为: "yyyy/MM/dd "
//输出:
//返回:转换之后的字符串
//***************************************************
public static String dateToStr(java.util.Date aDteValue, String aFmtDate)

String strRtn = null;

if (aFmtDate.length() == 0)

aFmtDate = "yyyy/MM/dd ";

Format fmtDate = new SimpleDateFormat(aFmtDate);
try

strRtn = fmtDate.format(aDteValue);

catch (Exception e)




return (strRtn);


第二种。
import java.util.*;
import java.text.SimpleDateFormat;
class test

public static void main(String[] args)

long nCurrentTime = System.currentTimeMillis();
java.util.Date utilDate = new java.util.Date(nCurrentTime);

GregorianCalendar da = new GregorianCalendar(2004, 11, 23, 11, 45, 50);
java.util.Date time = da.getTime();
java.sql.Date sqlDate = new java.sql.Date(time.getTime());
java.sql.Time sqlTime = new java.sql.Time(time.getTime());
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(nCurrentTime);

System.out.println(time);
System.out.println( "sqlDate:----- "+sqlDate);
System.out.println( "sqlTime:----- "+sqlTime);
System.out.println( "sqlTimestamp:----- "+sqlTimestamp);



你自己选吧。后面的这个比较好。你自己最好写一个公共的util类。
参考技术A 用dateadd函数 参考技术B hql = "select DATEADD(d,180,'2011-11-11') from 表"; 没问题的 HQL 只有配置文件编写的没问题 大部分的ORACLE函数都是可用的 参考技术C SELECT DATEADD(d,180,'2011-11-11')追问

如果是 hibernate的 查询怎么写呢 HQL?

但是我问的是在Java中怎么做。

追答

。。。我不会了,没做过Java的。

sql语句怎么来对日期进行相加减

在sql server里可以使用:
where start_date <=
DateAdd(d,1,to_date(\'2005-12-09\',\'yyyy-mm-dd\'))
and completion_date >=
to_date(\'2005-12-09\', \'yyyy-mm-dd\') ;

oracle中没有定义和sql server中一样的DateAdd函数,
oracle可以通过interval \'n\' year/month/day/hour/minute/second/second(p,s)
的方式来增减时间
下面是自己在oracle中写的DateAdd函数
函数调用基本同sql server一样, 不过datepart部分需要以字符串的方式输入, 即
DateAdd(d,1,to_date(\'2005-12-09\',\'yyyy-mm-dd\'))
要改为
DateAdd(\'d\',1,to_date(\'2005-12-09\',\'yyyy-mm-dd\'))

函数定义如下函数中的注释是datepart的新说明, 与sql server中的略有不同)
create or replace function DATEADD( datepart varchar2, num number, indate date ) return date is
Result date;
v_sql varchar2(1000);
v_datepart varchar2(30);
v_ms varchar2(13);
begin
v_datepart := lower(datepart);
/*
Datepart Abbreviations
year yy, y
quarter qq, q
month mm, m
day dd, d
week wk, w
hour hh, h
minute mi, n
second ss, s
millisecond ms
*/
case
when v_datepart in (\'year\',\'yy\',\'y\') then
v_sql := \'select :1 + interval \'\'\'||num||\'\'\' year from dual\';
when v_datepart in (\'quarter\',\'qq\',\'q\') then
v_sql := \'select :1 + (interval \'\'3\'\' month) * \'||num||\' from dual\';
when v_datepart in (\'month\',\'mm\',\'m\') then
v_sql := \'select :1 + interval \'\'\'||num||\'\'\' month from dual\';
when v_datepart in (\'week\',\'wk\',\'w\') then
v_sql := \'select :1 + (interval \'\'7\'\' day) * \'||num||\' from dual\';
when v_datepart in (\'day\',\'dd\',\'d\') then
v_sql := \'select :1 + interval \'\'\'||num||\'\'\' day from dual\';
when v_datepart in (\'hour\',\'hh\') then
v_sql := \'select :1 + interval \'\'\'||num||\'\'\' hour from dual\';
when v_datepart in (\'minute\',\'mi\',\'n\') then
v_sql := \'select :1 + interval \'\'\'||num||\'\'\' minute from dual\';
when v_datepart in (\'second\',\'ss\',\'s\') then
v_sql := \'select :1 + interval \'\'\'||num||\'\'\' second from dual\';
when v_datepart in (\'millisecond\',\'ms\') then
v_ms := to_char(num/1000,\'fm999999990.000\');
v_sql := \'select :1 + interval \'\'\'||v_ms||\'\'\' second(9,3) from dual\';
else
RAISE_APPLICATION_ERROR(-20001, \'\'\'\'||datepart||\'\'\' is not a recognized dateadd option.\' );
end case;

execute immediate v_sql into Result using indate;

return(Result);

EXCEPTION
WHEN OTHERS THEN
RAISE ;

end DATEADD;
参考技术A 相减是计算二个时间的差值,这个应该有函数,好像是 DateDiff ,具体用法比较简单:DateDiff(计算的时间值,计算的开始时间,计算的结束时间) ,其中“计算的时间值 ”包括秒(s)、分(n)、时(h)、日(d)、月(m)、年(yyyy)。
但二个日期或时间相加是什么?好像没有这样的运算吧?也可能是我孤陋寡闻吧。

以上是关于一个sql类的Date ,我要怎么在那个日期上加上指定天数,然后获得的还是一个sql类的Date?的主要内容,如果未能解决你的问题,请参考以下文章

oracle 日期格式 查询 如有一个字段类型是date的 我要根据这个字段来查询 怎么做 急

日记本上的——date——no,比如我要:2013年10月28日,你在上面日期怎么写

SQL 怎么将日期时间格式转换成日期

SQL中怎么实现时间相加。比如 我要实现 从今天的日期加30天为到期日。

我要获得java date类型的日期对象 怎么搞

sql查询中日期加减的问题