Java 8 – How to parse date with "dd MMM" (02 Jan), without year?

This example shows you how to parse a date (02 Jan) without a year specified. JavaDateExample.java package com.mkyong.time; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class JavaDateExample { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM", Locale.US); String date = "02 Jan"; LocalDate localDate = LocalDate.parse(date, formatter); System.out.println(localDate); System.out.println(formatter.format(localDate)); } } Output …

Read more

Java 8 – How to parse date with LocalDateTime

Here are a few Java 8 examples to parse date with LocalDateTime. First, find the DateTimeFormatter pattern that matches the date format, for example: String str = "2020-01-30 12:30:41"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); second, parse it with LocalDateTime.parse DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String str = "2020-01-30 12:30:41"; LocalDateTime localDateTime = LocalDateTime.parse(str, dtf); 1. …

Read more