Is there an format in java for parsing date such as 'Jun 3, 2020 5:04:05 PM' because i'm creating an app in JEE and i got ths message in my log file when persisting data
Asked
Active
Viewed 833 times
0
-
2Does this answer your question? [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Kukanani Jun 05 '20 at 14:19
2 Answers
2
I recommend you use java.time.LocalDateTime
and DateTimeFormatter.ofPattern
instead of using the outdated java.util.Date
and SimpleDateFormat
. Check this to learn more about the modern date/time API.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Define format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy h:mm:ss a", Locale.US);
// Date/time string
String strDate = "Jun 3, 2020 5:04:05 PM";
// Parse the date/time string into LocalDateTime
LocalDateTime ldt = LocalDateTime.parse(strDate, formatter);
// Display ldt.toString()
System.out.println(ldt);
// Display `ldt` in the specified format
System.out.println(formatter.format(ldt));
}
}
Output:
2020-06-03T17:04:05
Jun 3, 2020 5:04:05 PM

Basil Bourque
- 303,325
- 100
- 852
- 1,154

Arvind Kumar Avinash
- 71,965
- 6
- 74
- 110
1
If I understand you correctly:
String dString = "Jun 3, 2020 5:04:05 PM"
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss aa");
Date date = formater.parse(dString);
EDIT: Dealing with locale:
new SimpleDateFormat("MMM dd, yyyy HH:mm:ss aa", Locale.US)
You can also look at LocalDateTime
and DateTimeFormatter

Pearl
- 392
- 2
- 12
-
-
Do not forget to take Local's into account depending on your situation it can cause errors – Pearl Jun 05 '20 at 13:42
-
_The problem is really when i want to persist data from android application to JEE glassfish server in mysql now i'm getting that problem. thanks in advance_ – Paluku gratien Jun 05 '20 at 13:44
-
_Ok i will try to take into account Local's date, may it will solve my case thank you_ – Paluku gratien Jun 05 '20 at 13:47
-
1Please 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. Jun 05 '20 at 16:32