【问题标题】:Convert Current Timestamp to DD MMM YYYY format将当前时间戳转换为 DD MMM YYYY 格式
【发布时间】:2014-10-07 10:07:34
【问题描述】:

我正在尝试将当前时间戳转换为 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:无法解析的日期:“2014-10-07 15:38:29.876”

【问题讨论】:

    标签: java date simpledateformat


    【解决方案1】:

    以下对我有用

    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】:

      只要你能做到这一点

      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
      

      【讨论】:

        【解决方案3】:

        你不能使用ts.toString(),你总是需要使用相同的格式化程序。 仅使用:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-12
          • 1970-01-01
          • 2012-09-12
          • 1970-01-01
          相关资源
          最近更新 更多