Last active
August 29, 2015 14:15
-
-
Save eagafonov/e96c674d0b07c6e44c7d to your computer and use it in GitHub Desktop.
Parse date per format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 date | |
System.out.println("Parsed date: " + date); | |
// print date in same format | |
System.out.println("Formatted date: " + df.format(date)); | |
// Parse invalid date | |
date = df.parse("2015-02.11", new ParsePosition(0)); | |
// date is null since string does not match the format string | |
System.out.println("Parsed invalid date (must be null): " + date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment