我正在尝试将字符串转换为sql日期时间值。例如201405021843300400应该变成2014-05-02 18:43:30:400到毫秒。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParsePosition;
public class DateFormat {
public static String decodeDate(String myDate, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
ParsePosition pos = new ParsePosition(0);
Date tmpDateTime = sdf.parse(myDate, pos);
DateFormat dfFinal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
System.out.println("Milliseconds to DateTime: " + dfFinal.format(tmpDateTime));
return dfFinal.format(tmpDateTime);
}
public static void main(String args[]) {
System.out.println(decodeDate("201405021843300400", "yyyyMMddhhmmssSSSS"));
}
}
我尝试从解码日期中返回long,日期,java.sql.Timestamp,但无法这样做。有没有一种方法可以将字符串转换为日期时间戳?
提前致谢。
// java.sql.Date -> java.util.Date
// java.sql.Timestamp -> java.util.Date
// java.sql.Time -> java.util.Date
// e.g. with long value
String target = "201405021843300400";
DateFormat df = new SimpleDateFormat("yyyyMMddhhmmssSSSS");
java.sql.Timestamp date = new java.sql.Timestamp(df.parse(target).getTime());
or
new java.sql.Timestamp(year, month, date, hour, minute, second, nano)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句