将当前时间戳转换为DD MMM YYYY格式

时间:2014-10-07 10:07:35

标签: java date simpledateformat

我正在尝试将当前时间戳转换为DD MMM YYYY格式。

但我不知道为什么它会给我一个无法解决的错误。

代码:

Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());

System.out.println(format.format(fechaNueva));

工作但我想做

SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");

给了我不可解决的错误。

java.text.ParseException:Unparseable date:" 2014-10-07 15:38:29.876"

3 个答案:

答案 0 :(得分:0)

您无法使用ts.toString(),您始终需要使用相同的格式化程序。 只使用:

Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");

System.out.println(format.format(ts));

答案 1 :(得分:0)

以下为我工作

Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(fechaNueva));

输出

07 Oct 2014, 15:46

您试图以您希望格式化的格式解析日期,而不是已经存在的格式。

答案 2 :(得分:0)

你可以做到这一点

Timestamp timestamp = new Timestamp(new Date().getTime());
        System.out.println(timestamp);
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM YYY, HH:mm");
        System.out.println("Formatted "+dateFormat.format(timestamp));

希望这会有所帮助。

输出: -

Formatted 07 Oct 2014, 15:59