将日期格式更改为mm-dd-yyyy hh:mm

Lit*_*ird 1 java formatting date

我正在尝试使用此代码:

private static Date getTimeStamp() {
    return new Timestamp(new Date().getTime());
}
Run Code Online (Sandbox Code Playgroud)

我得到的是 2013-03-13 12:46:22.011

但我需要的是时间戳应采用格式mm-dd-yyyy hh:mm.

Jes*_*per 5

java.sql.Timestamp对象(就像java.util.Datejava.sql.Date)本身没有格式,因此你不能"以[whatever]格式的时间戳".

仅当将对象转换为字符串以进行显示时,才适用格式.您可以使用所需的格式SimpleDateFormat将a转换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);
Run Code Online (Sandbox Code Playgroud)