高手急救!怎样处理日期函数???

lchickenwing 2003-09-29 11:51:51
有一个smalldatetime的变量,现在我只想要日期而不要时间。请问怎么办?
用substring函数不能截取。
...全文
22 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
reason2008 2003-09-29
  • 打赏
  • 举报
回复
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String mDateTime1=formatter.format(d);//d就是时间
reason2008 2003-09-29
  • 打赏
  • 举报
回复

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-");
String mDateTime1=formatter.format(d);//d就是时间
builder666666 2003-09-29
  • 打赏
  • 举报
回复
你要采用日期标准--日期格式化
在JSP文件里输入
<%@ page import="java.text.*"%>
<%
Locale locale=new Locale("zh","CN");
Date date=new Date();
String smalldatetime=new String();
smalldatetime=DateFormat.getDateInstance(DateFormat.SHORT,locale).format(date));
out.println("Date="+smalldatetime);
%>
这样smalldatetime就是只有日期了。
flyshp 2003-09-29
  • 打赏
  • 举报
回复
getDate();
getYear();
getMonth(); 重新组合
mtou 2003-09-29
  • 打赏
  • 举报
回复
rs.getDate(字段名);
kaibinsj 2003-09-29
  • 打赏
  • 举报
回复
date(字段名)
djws 2003-09-29
  • 打赏
  • 举报
回复
抱歉,写错了,应是
String da = new String(tempchar, 0, 10);
不过,我刚注意到,你的日期字符串月份和日期不会自动补零,程序改一下

<%
String tempmodtime = "2003-5-11 00:00:00";
char[] ch = {'a','a'};
int i = 0;
StringBuffer sb = new StringBuffer(tempmodtime);
while(ch[0] != ' ') {
tempmodtime.getChars(i, i+1, ch, 0);
i ++;
}
sb.delete(i-1, tempmodtime.length());
String str = new String(sb); //现在str中是日期字串
sb.append('-');
String[] ymd = {" "," "," "};
int end = 0;
int index = 0;
Integer strint = new Integer(1);
int[] ymdint = {0, 0, 0};
for(i=0; i<=str.length(); i++) {
sb.getChars(i, i+1, ch, 0);
if(ch[0] == '-') {
//out.print("a<br>"+i+"<br>a");
ymd[index] = new String(sb.substring(end, i));
strint = new Integer(ymd[index]);
ymdint[index] = strint.intValue();
index ++;
end = i+1;
}
}
for(i=0; i<3; i++) { //测试用
out.print(ymd[i]+"<br>");
out.print(ymdint[i]);
out.print("<br>");
}
%>

你想要的都有了
方法很笨,不过我没别的办法,我用的是mysql,对这类东西都用Timestamp保存,读取时用rs.getDate("xxx")得到日期,用rs.getTime("xxx")得到时间。
另:字符串格式不对会出错的,用try把上面程序包起来比较好。
lchickenwing 2003-09-29
  • 打赏
  • 举报
回复
请问djws:
String da = mew(tempchar, 0, 10);
这条语法对吗?
clare0peng 2003-09-29
  • 打赏
  • 举报
回复
如果是oracle,直接用to_char很好解決這個問題
djws 2003-09-29
  • 打赏
  • 举报
回复
数据库的字段类型用Timestamp,用java.sql.Date tempmodtime = rs.getDate("MONTIME")读取,需要字符串的话,tempmodtime.toString()
如果数据库字段类型不可改:
char[] tempchar={'a'};
tempmodtime.getChars(0, 10, tempchar, 0);
String da = mew(tempchar, 0, 10);
da中就是你想要的
lchickenwing 2003-09-29
  • 打赏
  • 举报
回复
java.util.Date now= new java.util.Date();
SimpleDateFormat formatter=new SimpleDateFormat ("yyyyMMdd");
String Get_date=formatter.format(now);
String modtime=Get_date.substring(0,4)+"-"+Get_date.substring(4,6)+"-"+Get_date.substring(6,8);
我把modtime的值放入数据库中的一个表,字段名叫MODTIME,然后我从表中把这个MODTIME对应的值取出,放入String tempmodtime中,tempmodtime=rs.getString("MODTIME");显示出来的是“2003-9-29 00:00:00"
现在我只想要年,月,日,怎么办?
djws 2003-09-29
  • 打赏
  • 举报
回复
如果java.util.date smalldatetime
用java.sql.date da = new (smalldatetime);
lchickenwing 2003-09-29
  • 打赏
  • 举报
回复
可是我已经用一个String类型的变量来保存那个smalldatetime的值,现在怎么从String型的变量中获得smalldatetime中的年,月,日?我试过用substring()不行!!!!

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧