Java string to date or date to string
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 ad year, MM is the month, DD is the date, as for HH:mm:ss do not 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 divided; HH is a 24-hour system, and HH is 12-hour system 1. String to date: 2002-10-8 15:30:22 to turn it into a date, you can use
Date date = Sdf.parse ("2002-10-8 15:30:22");
2. Date-to-string if you turn today's date into a string available
String datestr = Sdf.format (New Date ());
The contents of this string will resemble 2002-10-08 14:55:38
With this API, we can turn the date into the string format we want, for example, if we want to export the date to October 08, 2002,
We can write this:
SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day"); String datestr = Sdf.format (New Date ());
DATESTR will output according to the format we set.
Attached SimpleDateFormat parser = new SimpleDateFormat ("Eeee, MMMM dd, yyyy"); SimpleDateFormat formatter = new SimpleDateFormat ("EEE. Mm/dd ");
Java string to date or date to string