日期时间格式需要按预期更改。

huangapple go评论66阅读模式
英文:

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 &amp; 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 = &quot;02 Sep 2020 12:24 AM&quot;;
	DateTimeFormatter parserDtf = DateTimeFormatter.ofPattern(&quot;dd MMM uuuu hh:mm a&quot;,
																Locale.ENGLISH);
	DateTimeFormatter outputDtf = DateTimeFormatter.ofPattern(&quot;dd MMM uuuu &#39;at&#39; hh:mm a&quot;,
																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(&quot;dd MMM yyyy &#39;at&#39; HH:mm:ss z&quot;); 
Date date = new Date(System.currentTimeMillis()); 
System.out.println(formatter.format(date));

huangapple
  • 本文由 发表于 2020年9月4日 16:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63737832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定