Skip to content

Instantly share code, notes, and snippets.

@mstrYoda
Created August 12, 2018 13:32
Show Gist options
  • Save mstrYoda/2b3c975428f488358a9099a841577647 to your computer and use it in GitHub Desktop.
Save mstrYoda/2b3c975428f488358a9099a841577647 to your computer and use it in GitHub Desktop.
Java LocalDate and Date example
public static void main(String[] args) throws IOException, ParseException {
String str = "Sun Jun 10 05:23:03 2018";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss yyyy");
System.out.println(LocalDate.parse(str, formatter));
DateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = fmt.parse(str);
System.out.println(date);
System.out.println(targetFormat.format(date));
}
/*
Outputs :
// date time formatter
2018-06-10
// simple date format
Sun Jun 10 05:23:03 EET 2018
10-06-2018
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment