如何将String转换为TimeStamp
字符串为000600,如何转换为00:06:00的TimeStamp?
------解决方案--------------------import java.sql.Timestamp;
public class Test {
public static void main(String[] args) {
String str = "000600";
int h = Integer.parseInt(str.substring(0, 2));
int m = Integer.parseInt(str.substring(2, 4));
int s = Integer.parseInt(str.substring(4, 6));
Timestamp ts = new Timestamp(2012-1900,11,27,h,m,s,0);
System.out.println(ts);
}
}
------解决方案--------------------
public static synchronized Date toTime(String paramTime)
{
SimpleDateFormat dateFormat;
if ("HHmmss".length() == paramTime.length())
{
dateFormat = new SimpleDateFormat("HHmmss");
}
else
{
dateFormat = new SimpleDateFormat("HHmmssSSS");
}
Date changeTime = null;
try
{
changeTime = dateFormat.parse(paramTime);
}
catch (ParseException e)
{
return null;
}
return changeTime;
}