Skip to content

Instantly share code, notes, and snippets.

@eagafonov
Created February 11, 2015 10:52
Show Gist options
  • Save eagafonov/eb9b5c77800911b1c229 to your computer and use it in GitHub Desktop.
Save eagafonov/eb9b5c77800911b1c229 to your computer and use it in GitHub Desktop.
Parse date per format
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParsePosition;
public class DateTimeTest {
public static void main(String[] args) {
// Create parser
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
// Parse correct data
Date date = df.parse("2015-02-11", new ParsePosition(0));
// put
System.out.println("Parsed date: " + date.toString());
date = df.parse("2015-02.11", new ParsePosition(0));
// date is null since string does not match the format string
System.out.println("Parsed date: " + date);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment