Replace String with Date in Java

Asked 2 years ago, Updated 2 years ago, 140 views

If there is a string format like January 2, 2010, I want to know how to change it to Date type I want to split that string into years, months, and days

string java date time

2022-09-22 22:35

1 Answers

Date is deprecated, and I think it would be better to use DateTimeFormatter in Java 8~

String string = "January 2, 2010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02

I use it like this


2022-09-22 22:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.