日期:2014-05-20 浏览次数:20864 次
String nowTime="2008-01-18"; Date date = java.sql.Date.valueOf(nowTime); System.out.println(date);
String nowTime="2008-01-18 09:10:10"; Date date = java.sql.Date.valueOf(nowTime); System.out.println(date);
public static Date valueOf(String s) { int year; int month; int day; int firstDash; int secondDash; if (s == null) throw new java.lang.IllegalArgumentException(); firstDash = s.indexOf('-'); secondDash = s.indexOf('-', firstDash + 1); if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length() - 1)) { year = Integer.parseInt(s.substring(0, firstDash)) - 1900; month = Integer.parseInt(s.substring(firstDash + 1, secondDash)) - 1; day = Integer.parseInt(s.substring(secondDash + 1)); } else { throw new java.lang.IllegalArgumentException(); } return new Date(year, month, day); }