how to convert from 10-JUN-02 01.57.07.848594000 PM to yyyy-MM-dd-HH24.MI.SS.FF6 in java
-
4So, your basic question is how to convert a `String` to a date/time value and then format it to another `String` - both questions are relatively common and have been asked and answered may times before. I might suggest starting with [Date and Time Classes](https://docs.oracle.com/javase/tutorial/datetime/iso/datetime.html) and [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) as a starting point of your research – MadProgrammer Oct 02 '18 at 23:30
-
i have done it with oracle but never with java – kari Oct 02 '18 at 23:48
-
You have to recognise that while you might not have done, many other people before you have. It's not an uncommon problem. I will, however, give you some leeway as the input is unusual – MadProgrammer Oct 02 '18 at 23:50
-
What does `HH24.MI.SS.FF6` mean? Are you trying to truncate the nanoseconds to microseconds? – Basil Bourque Oct 03 '18 at 06:50
1 Answers
Start by taking a look at
- Date and Time Classes
- Java string to date conversion
- Failed to parse single digit hour and lowercase am-pm of day into Java 8 LocalTime
You question is not unique nor uncommon and some due diligence in researching your issue will be more highly rewarded.
You basic question is actually two questions:
- How to parse a
String
to a date/time object - How to format a date/time object
I appreciate that in this day and age there are at least 3 distinct methods you might approach these problems. However, the Date/Time API introduced in Java 8 should be your preferred approach.
Having said that, you will probably have two major stumbling blocks in getting something to work.
Both relate to the uncommon case of your input (JUN
and pm
).
Java 8 introduced the DateTimeFormatterBuilder
class which can be used to overcome this issue.
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS a")
.toFormatter(Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse("10-JUN-02 01.57.07.848594000 pm", dtf);
This will provide you with a date/time representation of your input.
Then, you just need another DateTimeFormatter
to format the output as desired...
DateTimeFormatter output = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS", Locale.ENGLISH);
System.out.println(output.format(ldt));
which outputs...
2002-06-10-13.57.07.848594
Make sure you have a read through the JavaDocs for DateTimeFormatter
to better understand the formatter specifications

- 343,457
- 22
- 230
- 366
-
@kari if that’s important to you, then use an Instant instead of LocalDateTime. You’ll also forgive me if I don’t understand your comment, as the date/time api supports daylight savings – MadProgrammer Feb 05 '19 at 18:10