我想在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 ),感觉上不对,不知这样有没有错!
time = format.parse(createTimeStr);
} catch (Exception e) {
out.println("<script>alert('时间参数不对!');history.go(-1);</script>");
}
但打印出来却是这样的结果(Thu Jan 10 00:06:00 CST 2008 ),感觉上不对,不知这样有没有错!
java.util.Date time = null;
这个忘了写上了
format.format(time);
同意楼上的。
你的输出是没有错的,直接输出time的话.就是"Thu Jan 10 00:06:00 CST 2008"这种格式了!
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);
}
}
用此方法记得format的参数是Date类型的
否则输入的话会报错,用时将自己输入的日期转化为Date类型
/**
* 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();
}
<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)
这是什么原因引起了?
各位还有别的方法存入时间到数据库里去?分为够,我明天加一百!
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应该是你要的那种形式
koko 我是没注意,把注意放在你代码上了,别气了!哈哈