0

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?

deHaar
  • 17,687
  • 10
  • 38
  • 51
Ramanuj
  • 123
  • 1
  • 13

4 Answers4

3

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);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Sep 04 '20 at 09:13
  • 2
    @OleV.V. on Android it is still difficult to use `java.time`, when min version of android 8 is required – Vlad Sep 04 '20 at 11:15
  • @Vlad The question wasn’t about Android. For older Android you may use java.time either through [desugaring](https://developer.android.com/studio/write/java8-support-table) or through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). Still, if the answer said *here’s an answer for older Android*, it would be more helpful and have a smaller risk of leading someone else astray. – Ole V.V. Sep 04 '20 at 11:19
3

Just wrap the word in single quotes.

"dd MMM yyyy 'at' hh:mm aaa"
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Vlad
  • 7,997
  • 3
  • 56
  • 43
3

If you use java.time 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:

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
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    It doesn't matter if you use the newer Time API or the old Date API for this. The question is about the `at` in the formatted date string, and the solution of using single-quoted `'at'` is the same for both. Obscuring the relevant part of the answer by preaching the new API is counter-productive. I'm not down-voting, because nothing here is wrong, but I'm not up-voting either, because of the unnecessary preaching that is *immaterial* to the question. – Andreas Sep 04 '20 at 09:03
  • 1
    @Andreas OK, I rephrased the beginning a little to make it less *preaching*. I still feel like telling OPs who use `java.text.SimpleDateFormat` about `java.time` is not totally unnecessary. I admit, they might know about it and use the outdated API for reasons, but in many cases they just don't know `java.time`. – deHaar Sep 04 '20 at 09:10
  • Where in the question do you see any mention of `SimpleDateFormat`? You begin preaching, but the question applies equally to both APIs. How do you know OP isn't already using the Time API? Please control your zealousness when entirely unwarranted. – Andreas Sep 04 '20 at 09:16
  • 1
    @Andreas I don't know it, neither was it mentioned, nor tagged. I just saw answers provided using `SimpleDateFormat`, found no comment stating *... but I am using `java.time`...* and that made me *preach* a solution in `java.time`. – deHaar Sep 04 '20 at 09:19
  • You found no comment from OP because OP is likely **offline** and haven't seen any of the answers using `SimpleDateFormat`. You're making conclusion out of thin air. At this time, there is only one comment from OP *anywhere*, which was left 3 minutes after the question was posted, and the answer with the comment didn't mention `SimpleDateFormat` *at the time* (see answer history). – Andreas Sep 04 '20 at 09:21
  • 1
    @Andreas I hope to find any response when OP gets online again and maybe posts a comment, edits the question or accepts one of the answers. Might take a while, could never happen, we'll or I'll see. Could be a wrong conclusion, but that's not sure so far. – deHaar Sep 04 '20 at 09:30
  • Doesn't really matter, because the answer of single-quoting the `at` word applies equally to both APIs, making [this answer](https://stackoverflow.com/a/63737917/5221149) the **best** of them all. Short and to the point, with unnecessary extra code obfuscating the relevant part. Support clear concise answers and up-vote it. I sure have. – Andreas Sep 04 '20 at 09:36
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));
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
ARLEQUINA
  • 129
  • 1
  • 1
  • 11
  • Can u pls sahre the sample – Ramanuj Sep 04 '20 at 08:50
  • What language is that supposed to be? What is `Convert`, what is `ToString`? – luk2302 Sep 04 '20 at 08:52
  • SimpleDateFormat formatter= new SimpleDateFormat("dd MMM yyyy 'at' HH:mm:ss z"); Date date = new Date(System.currentTimeMillis()); System.out.println(formatter.format(date)); – ARLEQUINA Sep 04 '20 at 08:52
  • Sorry, im currently using c#. But i shared the code above in java. – ARLEQUINA Sep 04 '20 at 08:52
  • 2
    Then you should replace the answer content with that comment, C# answers are useless here. – luk2302 Sep 04 '20 at 08:54
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Sep 04 '20 at 09:13