convert datetime to custom format in java 8 1

convert datetime to custom format in java 8

DateTimeFormatter oldPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter newPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");

LocalDateTime datetime = LocalDateTime.parse(input, oldPattern); 
String output = datetime.format(newPattern);

Here is what the above code is Doing:
1. Create a DateTimeFormatter object with the old pattern.
2. Create a DateTimeFormatter object with the new pattern.
3. Parse the input string using the old pattern.
4. Format the parsed date using the new pattern.

Similar Posts