日期:2014-05-20  浏览次数:20851 次

字符串转换日期格式
Date d = new Date();//获得系统当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy--MM--dd");
String str = sdf.format(d);//进行格式化
然后我就想把str换成date的并且不要改变格式

------解决方案--------------------
public static Date getDate(String theDate, String dateFormat) throws Exception {
try {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
format.setLenient(false);
return format.parse(theDate);
} catch (ParseException e) {
throw new AppException("字符串:" + theDate + ",转换为日期错误。");
}
}
------解决方案--------------------
怎么可能,
日期型怎么会有yyyy--MM--dd这种格式的。
也就日期转换成字符串的时候随你用哪种格式罢了。
------解决方案--------------------
是要得到当前日期yyyy-mm-dd?? 

Java code
Calendar calendar=Calendar.getInstance();
String nonceDate=calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONDAY)+1)+"-"+(calendar.get(Calendar.DATE));

------解决方案--------------------
探讨
是要得到当前日期yyyy-mm-dd??


Java code
Calendar calendar=Calendar.getInstance();
String nonceDate=calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONDAY)+1)+"-"+(calendar.get(Calendar.DATE));
……

------解决方案--------------------
探讨
Date d = new Date();//获得系统当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy--MM--dd");
String str = sdf.format(d);//进行格式化
然后我就想把str换成date的并且不要改变格式

------解决方案--------------------
可以在数据库查询sql语句中就转为date型,例如
SQL code
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')    from dual//

------解决方案--------------------
Date d = new Date();//获得系统当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String str = sdf.format(d);//进行格式化
System.out.print("Time:" + str.substring(0, 4) + "--" + str.substring(4, 6) + "--" + str.substring(6, 8));

------解决方案--------------------
探讨
怎么可能,
日期型怎么会有yyyy--MM--dd这种格式的。
也就日期转换成字符串的时候随你用哪种格式罢了。