new date 闹到是当前时间 怎么插入到数据库就遍历
展开全部
public static Date dateTimeString2Date(String date_str) {
try {
Calendar cal = Calendar.getInstance();
java.sql.Timestamp timestampnow = new java.sql.Timestamp(cal.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
java.util.Date current = formatter.parse(date_str, pos);
timestampnow = new java.sql.Timestamp(current.getTime());
return timestampnow;
}
catch (NullPointerException e) {
return null;
}
}
//格式以上面不一样yyyy-MM-dd
public static Date dateString2Date(String date_str) {
try {
Calendar cal = Calendar.getInstance();
java.sql.Timestamp timestampnow = new java.sql.Timestamp(cal.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
java.util.Date current = formatter.parse(date_str, pos);
return current;
}
catch (NullPointerException e) {
return null;
}
}
插入数据库,用java.sql.PreparedStatement即可。
try {
Calendar cal = Calendar.getInstance();
java.sql.Timestamp timestampnow = new java.sql.Timestamp(cal.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
java.util.Date current = formatter.parse(date_str, pos);
timestampnow = new java.sql.Timestamp(current.getTime());
return timestampnow;
}
catch (NullPointerException e) {
return null;
}
}
//格式以上面不一样yyyy-MM-dd
public static Date dateString2Date(String date_str) {
try {
Calendar cal = Calendar.getInstance();
java.sql.Timestamp timestampnow = new java.sql.Timestamp(cal.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
java.util.Date current = formatter.parse(date_str, pos);
return current;
}
catch (NullPointerException e) {
return null;
}
}
插入数据库,用java.sql.PreparedStatement即可。
展开全部
插入带时分秒的时间不能用java.sql.date,需要使用java.sql.TimeStamp,你可以这样改写:
ps.setDate(3,new java.sql.TimeStamp(login.getLogDate().getTime()) );
ps.setDate(3,new java.sql.TimeStamp(login.getLogDate().getTime()) );
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-06-20
展开全部
给他个 格式就好啦
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(new Data());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(new Data());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询