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");
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");
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
.
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.
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);
Use this one to format datetime.
DateFormat('dd-MMM-yy hh.HH.mm.SSSSSSSSS a').format(DateTime.now());