String input = "2022-04-05 21:11:06";
required output:
Date output = 2022-04-05 21:11:06;
My code
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date var4 = df.parse(input);
but I got error
Exception in thread "main" java.text.ParseException: Unparseable date: "2022-04-05 21:11:06"
CodePudding user response:
You shouldn't use the old Date
and SimpleDateFormat
classes. Instead, use LocalDateTime
, ZonedDateTime
, and DateTimeFormatter
LocalDateTime dt = LocalDateTime.parse(
"2022-04-05 21:11:06",
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
);
CodePudding user response:
You are specifying wrong SimpleDateFormat. change this
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
to SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
CodePudding user response:
As Alexey R mentioned your pattern need some correction:
String input = "2022-04-05 21:11:06";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date v = df.parse(input);
System.out.println(v);