String string_Date = "2014-06-11"; //my string date
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
// Date startTimestam = simpleFormat.parse(string_Date);
Timestamp startTimestam = Timestamp.valueOf(string_Date);
Calendar cal = Calendar.getInstance();
cal.setTime(startTimestam);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 1);
starTimeStamp = new Timestamp(cal.getTime().getTime());
// will get output date is "2014-06-11 00:00:01"
illegalArgumentException
,即
时间戳格式必须为yyyy-mm-dd hh:mm:ss [.fffffffff] **
答案 0 :(得分:2)
由于以下原因,您的问题不是很清楚:
1)为什么要避免使用SimpleDateFormat?
2)是否要将String转换为Timestamp或Date to Timestamp?
如果要将String转换为Timestamp,那么它很简单(不使用SimpleDateFormat):
String timeValueStr="00:00:01";
String startTimeStr="2014-06-11" + " " + timeValueStr;
Timestamp startTimestamp = Timestamp.valueOf(startTimeStr);
如果要将String转换为Date,然后将Date转换为Timestamp:
//String to Date conversion
String startTimeStr = "2014-06-11";
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = simpleFormat.parse(startTimeStr);
//Date to Timestamp conversion
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 1);
Timestamp startTimestamp = new Timestamp(cal.getTime().getTime());
当然,如果你必须避免SimpleDateFormat,那么这里是首先将String转换为Timestamp然后转换为Date的代码(没有使用SimpleDateFormat就无法直接将String转换为Date):
String timeValueStr="00:00:01";
String startTimeStr="2014-06-11" + " " + timeValueStr;
Timestamp startTimestamp = Timestamp.valueOf(startTimeStr);
Date d = new Date(startTimestamp.getTime());