Java Data Type How to - Parse date string in form of 'Apr 18 2014'








Question

We would like to know how to parse date string in form of 'Apr 18 2014'.

Answer

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
  public static void main(String[] argv) {
    String goodFriday = "Apr 18 2014";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy");
    LocalDate holiday = LocalDate.parse(goodFriday, formatter);
    System.out.printf("Successfully parsed String %s, date is %s%n", goodFriday, holiday);
  }/*  ww w.  ja v a  2 s  .  c o m*/
}

The code above generates the following result.