In the article, the API is SimpleDateFormat, it belongs to Java.text.SimpleDateFormat, so please remember import come in!
Usage: SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
This line is the most important, it established the format of the conversion, YYYY is the full year, MM is the month, DD is the date, as for HH:mm:ss don't need me to explain it!
PS: Why some format uppercase, some format lowercase, it is afraid to avoid confusion, such as MM is the month, MM is the minute; HH is a 24-hour system, while HH is a 12-hour system.
1. String Turn date
2008-07-10 19:20:00 to convert it to a date, you can use date date = Sdf.parse ("2008-07-10 19:20:00");
2. Date-turn string
If you turn today's date into a string, you can use string str = Sdf.format (new date ());
The format of this string content resembles the 2008-07-10 19:20:00.
Through this API, we can turn the date into the string format we want, such as to export the date as July 10, 2008, so we can write:
SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day");
String str = sdf.format (new Date ());
STR will be exported in accordance with the format we have set.
A simple example with a good writing:
Import java.util.Date;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
public class Convertdemo {
/**
* Date converted to string
* @param date
* @return str/public
static String Dat ETOSTR (date date) {
simpledateformat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String str = format.format (date);
return str;
}
/**
* String converted to date
* @param str
* @return Date
/public static date Strtodate (String str) {
c22/> simpledateformat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Date date = null;
try {
date = Format.parse (str);
catch (ParseException e) {
e.printstacktrace ();
} return
date;
}
public static void Main (string[] args) {
Date date = new Date ();
SYSTEM.OUT.PRINTLN ("date-turn string:" + convertdemo.datetostr (date));
System.out.println ("String Turn Date:" + convertdemo.strtodate (convertdemo.datetostr
(date));
}