getDate();
getYear();
getMonth(); 重新组合
getYear();
getMonth(); 重新组合
解决方案 »
- 為什麼我的javascript能夠在IE運行但是不能在firefox
- 如何看懂公司的search enginee
- 推荐基本学习JSP的好书,或者是学习JSP的基本方法
- servlet forward问题(再次提问)
- 浏览器上输入固定网址,重定向到首页!(网站java+servlet+jsp)
- jsp连接SQL sever 2000时出现了“[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket”!求救!
- FCKeditor单引号的问题
- 很简单的问题:在DOS下javac 一个*.java的问题,解决就给分。
- 如何判断 ResultSet Rs 为空?
- JSP动作标签
- 各位高抬贵手
- 请各位指导一下使用weblogic6.1的要点.
在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就是只有日期了。
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-");
String mDateTime1=formatter.format(d);//d就是时间
String mDateTime1=formatter.format(d);//d就是时间
用java.sql.date da = new (smalldatetime);
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"
现在我只想要年,月,日,怎么办?
如果数据库字段类型不可改:
char[] tempchar={'a'};
tempmodtime.getChars(0, 10, tempchar, 0);
String da = mew(tempchar, 0, 10);
da中就是你想要的
String da = mew(tempchar, 0, 10);
这条语法对吗?
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把上面程序包起来比较好。