【问题标题】:DateTime format needed chnages as expected [duplicate]DateTime 格式需要按预期更改[重复]
【发布时间】:2020-09-04 08:47:46
【问题描述】:

我想将时间显示为"02 Sep 2020 at 12:24 AM"(注意日期和时间之间的at)。

我目前使用的格式是"dd MMM yyyy hh:mm aaa",
将时间显示为"28 Aug 2020 11:32 AM"

如何在时间前加上at

【问题讨论】:

    标签: java date datetime format


    【解决方案1】:

    您可以将字符串文字添加到日期格式中,方法是用单引号 (') 将它们括起来:

    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy 'at' hh:mm aaa");
    // Here -------------------------------------------------^--^
    
    String formatted = sdf.format(myDateVariable);
    

    【讨论】:

    • 请不要教年轻人使用早已过时且臭名昭著的SimpleDateFormat类。至少不是第一选择。而且不是没有任何保留。今天我们在java.time, the modern Java date and time API, 和它的DateTimeFormatter 中做得更好。
    • @OleV.V.在Android上仍然很难使用java.time,当需要最低版本的android 8时
    • @Vlad 问题与 Android 无关。对于较旧的 Android,您可以通过 desugaringThreeTenABP 使用 java.time。不过,如果答案说这里有一个针对旧版 Android 的答案,它会更有帮助,并且引导他人误入歧途的风险也会更小。
    【解决方案2】:

    如果为此使用java.time,则可以定义java.time.format.DateTimeFormatter 来解析String,使用它将String 解析为java.time.LocalDateTime,并定义和使用另一个DateTimeFormatter,其中包括at 通过将其括在单引号中来将其转义:

    public static void main(String[] args) {
        String dateTime = "02 Sep 2020 12:24 AM";
        DateTimeFormatter parserDtf = DateTimeFormatter.ofPattern("dd MMM uuuu hh:mm a",
                                                                    Locale.ENGLISH);
        DateTimeFormatter outputDtf = DateTimeFormatter.ofPattern("dd MMM uuuu 'at' hh:mm a",
                                                                    Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse(dateTime, parserDtf);
        System.out.println(ldt.format(outputDtf));
    }
    

    此代码产生输出

    02 Sep 2020 at 12:24 AM
    

    【讨论】:

    • 使用新的 Time API 或旧的 Date API 都没有关系。问题是关于格式化日期字符串中的at,并且使用单引号'at' 的解决方案对于两者都是相同的。通过宣传新 API 来掩盖答案的相关部分会适得其反。我没有投反对票,因为这里没有任何问题,但我也没有投赞成票,因为不必要的说教对问题无关紧要
    • @Andreas 好的,我稍微改写了开头,以减少讲道。我仍然觉得告诉使用java.text.SimpleDateFormat 的操作员java.time 并不是完全没有必要的。我承认,他们可能知道并使用过时的 API 是有原因的,但在许多情况下,他们只是不知道 java.time
    • 您在问题的什么地方看到提到了SimpleDateFormat?你开始讲道,但这个问题同样适用于两个 API。你怎么知道 OP 还没有使用 Time API?请在完全没有根据的情况下控制你的热情。
    • @Andreas 我不知道,既没有提到,也没有标记。我刚刚看到使用SimpleDateFormat 提供的答案,发现没有评论说明 ... 但我使用的是java.time... 这让我preach 成为@ 中的解决方案987654339@.
    • @Andreas 我希望在 OP 再次上线时能找到任何回复,并可能发表评论、编辑问题或接受其中一个答案。可能需要一段时间,永远不会发生,我们会或我会看到。可能是一个错误的结论,但目前还不确定。
    【解决方案3】:

    只需将单词用单引号括起来。

    "dd MMM yyyy 'at' hh:mm aaa"
    

    【讨论】:

      【解决方案4】:
      SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy 'at' HH:mm:ss z"); 
      Date date = new Date(System.currentTimeMillis()); 
      System.out.println(formatter.format(date));
      

      【讨论】:

      • 你能提供样品吗
      • 那应该是什么语言?什么是Convert,什么是ToString
      • SimpleDateFormat formatter= new SimpleDateFormat("dd MMM yyyy 'at' HH:mm:ss z");日期 date = new Date(System.currentTimeMillis()); System.out.println(formatter.format(date));
      • 那你应该用那个评论替换答案内容,C#答案在这里没用。
      • 请不要教年轻人使用早已过时且臭名昭著的麻烦SimpleDateFormat类。至少不是第一选择。而且不是没有任何保留。今天我们在java.time, the modern Java date and time API, 和它的DateTimeFormatter 中做得更好。
      猜你喜欢
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2017-04-12
      • 2021-04-09
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多