SimpleDateFormat is a subclass of DateFormat, it gives me some ways to complete the conversion of strings and time objects!
DateFormat is an abstract class and cannot create its own instance. It has only one subclass SimpleDateFormat
DateFormat belongs to Java.text class, should add import Java.text.DateFormat before use;
SimpleDateFormat Similarly, should add import Java.text.SimpleDateFormat before use;
Format
Package mydate;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Mycode02 {
public static void Main (string[] args) {
DateFormat df = new SimpleDateFormat ("yyyy mm month DD Day Hh:mm:ss");
Date A = new date ();
String str = Df.format (a); Converts a time object into a string according to a formatted string
System.out.println (str);
}
}
Output Result:
November 13, 2016 02:45:06
Parse (parse)
Package mydate;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Mycode02 {
public static void Main (string[] args) {
String str = "1977-7-7"; Green part must format consistent, otherwise error!
DateFormat df = new SimpleDateFormat ("YYYY-MM-DD"); Create a formatter
Date A;
try {
A = Df.parse (str);
System.out.println (a);
} catch (ParseException e) {
E.printstacktrace ();
}
}
}
Java Learning notes-dateformat and SimpleDateFormat