0

What format the below date is in?

21-MAY-11 12.00.00.000000000 AM

i tried below but did not work. any clue?

DateTimeFormatter formatter_1 = DateTimeFormatter.ofPattern("dd-MMM-yy hh.HH.mm.SSSSSSSSS a");
user2555212
  • 165
  • 1
  • 14
  • 7
    It's (presumably) hours.minutes.seconds.fractionOfSecond, not hours.hours.minutes.fractionOfSecond. – Andy Turner Jun 02 '21 at 05:39
  • 1
    Related: [Converting java.sql.Timestamp to Instant Time](https://stackoverflow.com/questions/50986138/converting-java-sql-timestamp-to-instant-time). Also related: [How can I make this date with a Java Calendar: 31-DEC-99 12.00.00.000000000 AM](https://stackoverflow.com/questions/14403306/how-can-i-make-this-date-with-a-java-calendar-31-dec-99-12-00-00-000000000-am). Apparently the same format, only the old and outdated date and time API asked about. – Ole V.V. Jun 02 '21 at 06:10
  • 1
    Is that May 11, 2021, or is it 21 May 2011? – Ole V.V. Jun 02 '21 at 06:13

3 Answers3

3

There is no in-built pattern that you can use for it. You can build a case-insensitive DateTimeFormatter with Locale.ENGLISH to parse this string.

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "21-MAY-11 12.00.00.000000000 AM";
        
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("uu-MMM-dd hh.mm.ss.SSSSSSSSS a")
                .toFormatter(Locale.ENGLISH);
        
        LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
        System.out.println(ldt);
    }
}

Output:

2021-05-11T00:00

Here, you can use yy instead of uu but I prefer u to y.

ONLINE DEMO

Learn more about java.time, the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Andy Turner in the comments is right.

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd-MM-yy hh:mm:ss.SSSSSSSSS a", Locale.ROOT);
LocalDateTime localDateTime = LocalDateTime.now();
String format = fmt.format(localDateTime);
System.out.println(format);
  • 1
    I think that you want `dd-MMM-yy` as in the question. You also want to supply a locale to the formatter. Otherwise a very fine answer. – Ole V.V. Jun 02 '21 at 06:14
  • Is it faction-of-second or nano-of-seconds? S -> fraction-of-second (e.g. 978) and n -> nano-of-second (e.g. 987654321) https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html – Gaurav kumar Singh Jun 02 '21 at 06:16
  • @GauravkumarSingh As long as there are exactly 9 decimals, either `SSSSSSSSS` or `nnnnnnnnn` will work. For fewer than 9 picking the right letter is of importance, you are right. – Ole V.V. Jun 02 '21 at 06:47
  • 2
    Apart from the problems pointed out by @OleV.V. , there is another problem with your pattern. Instead of `hh` (the symbol for the 12-hour or AM/PM time format), you have used `HH` (the symbol for the 24-Hour time format). – Arvind Kumar Avinash Jun 02 '21 at 07:57
-2

Use this one to format datetime.

DateFormat('dd-MMM-yy hh.HH.mm.SSSSSSSSS a').format(DateTime.now());
  • 1
    Which `DateFormat` class and which language are those? The question wisely asked about Java and `DateTimeFormatter` (from [java.time, the modern Java date and time API)](https://docs.oracle.com/javase/tutorial/datetime/), so if you wanted to suggest something else, I think you should tell us what would be wrong with the asker’s choice or whether there are any advantages of your suggestion? I also invite to publish an example result from your code line, also because I am not from the outset 100 % assured that your code is correct. – Ole V.V. Jun 02 '21 at 10:56