代码之家  ›  专栏  ›  技术社区  ›  Dejell

爪哇格式日期

  •  4
  • Dejell  · 技术社区  · 15 年前

    我有以下字符串:

    Mon Sep 14 15:24:40 UTC 2009
    

    我需要把它格式化成这样的字符串:

    14/9/2009
    

    如何在Java中实现?

    4 回复  |  直到 9 年前
        1
  •  7
  •   BalusC    15 年前

    使用 SimpleDateFormat (单击JavaDoc链接查看模式)将一个模式中的字符串解析为完全值 Date 并使用另一个来格式化解析的 日期 在另一个模式中的字符串。

    String string1 = "Mon Sep 14 15:24:40 UTC 2009";
    Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy").parse(string1);
    String string2 = new SimpleDateFormat("d/M/yyyy").format(date);
    System.out.println(string2); // 14/9/2009
    
        2
  •  3
  •   Khaled    15 年前

    可以使用simpledateformat类将必须的字符串转换为日期对象。日期格式可以在构造函数中给出。Format方法将字符串转换为日期对象。

    获取日期对象后,可以按所需方式对其进行格式化。

        3
  •  0
  •   Ashish Lohia    9 年前

    Java 8和以上的一个内衬。

    String localDateTime= LocalDateTime.parse("Mon Sep 14 15:24:40 UTC 2009", DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss z yyyy")).format(DateTimeFormatter.ofPattern("d/M/yyyy"));
    
        4
  •  -1
  •   chris    15 年前
    Date d = new Date("Mon Sep 14 15:24:40 UTC 2009");
    SimpleDateFormat f = new SimpleDateFormat("dd/M/yyyy");
    String s = new String(f.format(d));