英文:
DateTime format needed chnages as expected
问题
我想将时间显示为"02 Sep 2020 at 12:24 AM"
(注意日期和时间之间的 at)。
我目前使用的格式是"dd MMM yyyy hh:mm aaa"
,
它显示的时间格式为"28 Aug 2020 11:32 AM"
。
我如何在时间之前加上 at?
英文:
I want to display time as "02 Sep 2020 at 12:24 AM"
(mind the at between date and time).
The current format I am using is "dd MMM yyyy hh:mm aaa"
,
which displays time as "28 Aug 2020 11:32 AM"
.
How can I put an at before the time?
答案1
得分: 3
你可以通过用单引号 ('
) 将字符串字面值括起来,将其添加到日期格式中:
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy 'at' hh:mm aaa");
// 这里 ---------------------------------------------^--^
String formatted = sdf.format(myDateVariable);
英文:
You can add string literals to a date format by surrounding them with single quotes ('
):
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy 'at' hh:mm aaa");
// Here -------------------------------------------------^--^
String formatted = sdf.format(myDateVariable);
答案2
得分: 3
"dd MMM yyyy 'at' hh:mm aaa"
英文:
Just wrap the word in single quotes.
"dd MMM yyyy 'at' hh:mm aaa"
答案3
得分: 3
如果您使用 [`java.time`](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) 进行此操作,您可以定义一个 `java.time.format.DateTimeFormatter` 来解析一个 `String`,使用它将 `String` 解析为 `java.time.LocalDateTime`,并定义并使用另一个 `DateTimeFormatter`,该格式包括在模式中对 `at` 进行转义,将其括在单引号中:
```Java
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
<details>
<summary>英文:</summary>
If you use [`java.time`](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) for this, you can define a `java.time.format.DateTimeFormatter` to parse a `String`, use it to parse the `String` to a `java.time.LocalDateTime` and define & use another `DateTimeFormatter` that includes the `at` escaping it in the pattern by enclosing it in single-quotes:
```Java
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));
}
This code produces the output
02 Sep 2020 at 12:24 AM
答案4
得分: 0
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
英文:
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论