我正在尝试使用此代码:
private static Date getTimeStamp() {
return new Timestamp(new Date().getTime());
}
我得到的是2013-03-13 12:46:22.011
但我需要的是时间戳的格式应为mm-dd-yyyy hh:mm
。
答案 0 :(得分:5)
java.sql.Timestamp
个对象(就像java.util.Date
和java.sql.Date
)本身没有格式,因此您不能“以[whatever]格式设置时间戳。”
仅在将对象转换为字符串进行显示时才适用格式。您可以使用SimpleDateFormat
将Timestamp
转换为字符串,使用您想要的格式。
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm");
String text = dateFormat.format(timestamp);
System.out.println(text);
答案 1 :(得分:2)
希望这有帮助
String date1="2013-03-13 12:46:22.011";
DateFormat userDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
DateFormat dateFormatNeeded = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Date date = userDateFormat.parse(date1);
String finaldate = dateFormatNeeded.format(date);
答案 2 :(得分:1)
您可以使用SimpleDateFormat http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html来实现目标。
例如: 新的SimpleDateFormat(" MM-dd-yyyy HH:mm")。format(timestamp));