时间?

fuyou001 2008-06-10 08:57:15
我想在JSP里输入这样格式的:2008-6-10然后在后台取出:
  try {    
time = format.parse(createTimeStr);
} catch (Exception e) {
out.println("<script>alert('时间参数不对!');history.go(-1);</script>");
}

但打印出来却是这样的结果(Thu Jan 10 00:06:00 CST 2008 ),感觉上不对,不知这样有没有错!
...全文
168 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuyou001 2008-06-11
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 kokobox 的回复:]
我回的帖子已经说了呀,MM大写

为什么不看呢??????????????????????????????????????????????
[/Quote]

koko 我是没注意,把注意放在你代码上了,别气了!哈哈
youzy 2008-06-10
  • 打赏
  • 举报
回复
相信问题已经解决了
xql80329 2008-06-10
  • 打赏
  • 举报
回复
下面是是format这个方法的源码,注释的第一行写的很清楚了.所以输出那种格式是没问题的
    /**
* Formats a Date into a date/time string.
* @param date the time value to be formatted into a time string.
* @return the formatted time string.
*/
public final String format(Date date)
{
return format(date, new StringBuffer(),
DontCareFieldPosition.INSTANCE).toString();
}
lisl2003 2008-06-10
  • 打赏
  • 举报
回复
楼上已经回答清楚了
burningice44 2008-06-10
  • 打赏
  • 举报
回复
format.format(time)
用此方法记得format的参数是Date类型的
否则输入的话会报错,用时将自己输入的日期转化为Date类型
kokobox 2008-06-10
  • 打赏
  • 举报
回复
MM大写

下面的代码是正确的:


import java.io.UnsupportedEncodingException;
public class Main {
public static void main(String[] Args) throws UnsupportedEncodingException{
java.util.Date date = new java.util.Date();
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd",java.util.Locale.US);
String day = format.format(date);
System.out.println(day);
}
}

cxgman 2008-06-10
  • 打赏
  • 举报
回复
time=format.format(createTimeStr);
hfl832 2008-06-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tavor 的回复:]
输出的时候用下面这个,而不是直接toString()

Java codeformat.format(time);
[/Quote]

同意楼上的。
你的输出是没有错的,直接输出time的话.就是"Thu Jan 10 00:06:00 CST 2008"这种格式了!
tavor 2008-06-10
  • 打赏
  • 举报
回复
输出的时候用下面这个,而不是直接toString()
format.format(time);
fuyou001 2008-06-10
  • 打赏
  • 举报
回复
up
fuyou001 2008-06-10
  • 打赏
  • 举报
回复
  DateFormat format = new SimpleDateFormat("yyyy-mm-dd");
java.util.Date time = null;

这个忘了写上了
duzhonghua 2008-06-10
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 kokobox 的回复:]
我回的帖子已经说了呀,MM大写

为什么不看呢??????????????????????????????????????????????
[/Quote]
下次应该用红色字体标记下,,,,,,,,
duzhonghua 2008-06-10
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 kokobox 的回复:]
我回的帖子已经说了呀,MM大写

为什么不看呢??????????????????????????????????????????????
[/Quote]

不要郁闷,我就看到了,哈哈,解决了我的问题,谢谢,

kokobox 2008-06-10
  • 打赏
  • 举报
回复
我回的帖子已经说了呀,MM大写

为什么不看呢??????????????????????????????????????????????

fuyou001 2008-06-10
  • 打赏
  • 举报
回复
下面进来的分不多哈,只能意思下了!
fuyou001 2008-06-10
  • 打赏
  • 举报
回复
问题解决:MM要大写才表月份,小写表分!
zidasine 2008-06-10
  • 打赏
  • 举报
回复
DateFormat format = new SimpleDateFormat("yyyy-m-d");
java.util.Date time = null;
try {
time = format.parse(createTimeStr);
String newTime=format.format(time);
System.out.println(newTime);
} catch (Exception e) {
out.println("<script>alert('时间参数不对!');history.go(-1);</script>");
}
这个newTime应该是你要的那种形式
老紫竹 2008-06-10
  • 打赏
  • 举报
回复
但打印出来却是:Thu Jan 12 00:06:00 CST 2006

你怎么打印的?
fuyou001 2008-06-10
  • 打赏
  • 举报
回复
谢谢楼上几位!但都理解错了!我是要在jsp
<tr>
<td width="100px" align=right class="black12size" valign="top" >
开始时间:
</td>
<td width="300px" class="black12size" style="color:#666666">
<input type="text" name="beginTime" class="bian" ckNull="开始时间不能为空!" style="WIDTH: 150px; HEIGHT: 22px">
</td>
</tr>

输入如:2008-6-12格式的日期,然后在另一页面取出,最后保存到oracle数据库里为date类型的属性!
下面是我解决方法:
 String beginTimeStr = request.getParameter("beginTime");  

DateFormat format = new SimpleDateFormat("yyyy-mm-dd");

try {
beginTime = format.parse(beginTimeStr);

} catch (Exception e) {
out.println("<script>alert('时间参数不对!');history.go(-1);</script>");
return;
}

但在页面打印出bgeinTiem,错是错误的时间:如我在beginTime文本框里输入:2006-6-12但打印出来却是:Thu Jan 12 00:06:00 CST 2006
变成06年1月了,在数据库里也是错误的时间(2006-1-12 0:06:00)
这是什么原因引起了?
各位还有别的方法存入时间到数据库里去?分为够,我明天加一百!

62,615

社区成员

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

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