Recently, when developing JSON data deserialization to Java objects, Spring MVC and Jackson were found to be not particularly well supported by the date type, although it provided convenient annotations in the process of serializing Java objects to JSON data, but deserialization did not This leads to the following question, I need to manually convert the string to date type data, Java.text.SimpleDateFormat provides us with the infrastructure for this transformation. A simple definition of the format pattern listed below, if ' M ' is greater than or equal to 3 bits, the month is displayed as a letter, otherwise it is a number. For more detailed definitions, refer to Java.text.SimpleDateFormat JavaDoc.
Letter Description Examples
Y year 2015
M Month in the year Nov, 11, 7
D Day in month 1-31
E Day name in week Friday, Sunday
A am/pm marker Am, PM
H Hour in Day 0-23
H Hour in am/pm 1-12
M Minute in hour 0-60
S Second in minute 0-60
1. Date = 7-nov-2015
SimpleDateFormat formatter = new SimpleDateFormat ("dd-mmm-yyyy");
String dateinstring = "7-nov-2015";
try {
Date date = Formatter.parse (dateinstring);
SYSTEM.OUT.PRINTLN (date);
System.out.println (Formatter.format (date));
catch (ParseException e) {
E.printstacktrace ();
}
Output
Mon Nov Modified 00:00:00 MYT 2015
07-nov-2015
2. Date = 07/06/2015
SimpleDateFormat formatter = new SimpleDateFormat ("dd/mm/yyyy");
String dateinstring = "07/06/2015";
try {
Date date = Formatter.parse (dateinstring);
SYSTEM.OUT.PRINTLN (date);
System.out.println (Formatter.format (date));
catch (ParseException e) {
E.printstacktrace ();
}
Output
Mon Nov Modified 00:00:00 MYT 2015
07/06/2015
3. Date = Nov 7, 2015
SimpleDateFormat formatter = new SimpleDateFormat ("MMM dd, yyyy");
String dateinstring = "Nov 7, 2015";
try {
Date date = Formatter.parse (dateinstring);
SYSTEM.OUT.PRINTLN (date);
System.out.println (Formatter.format (date));
catch (ParseException e) {
E.printstacktrace ();
}
Output
Mon Nov Modified 00:00:00 MYT 2015
Nov 07, 2015
4. Date = Fri, Nov 7 2015
SimpleDateFormat formatter = new SimpleDateFormat ("E, MMM dd yyyy");
String dateinstring = "Fri, Nov 7 2015";
try {
Date date = Formatter.parse (dateinstring);
SYSTEM.OUT.PRINTLN (date);
System.out.println (Formatter.format (date));
catch (ParseException e) {
E.printstacktrace ();
}
Output
Mon Nov Modified 00:00:00 MYT 2015
Mon, Nov 07 2015
2. Date and Time Example
1. Date and time = Friday, Nov 7, 2015 12:10:56 PM
SimpleDateFormat formatter = new SimpleDateFormat ("Eeee, MMM dd, yyyy HH:mm:ss a");
String dateinstring = "Friday, Nov 7, 2015 12:10:56 PM";
try {
Date date = Formatter.parse (dateinstring);
SYSTEM.OUT.PRINTLN (date);
System.out.println (Formatter.format (date));
catch (ParseException e) {
E.printstacktrace ();
}
Output
Mon Nov Modified 12:10:56 MYT 2015
Monday, Nov, 2015 12:10:56 PM
Transformation of date and string in Java
1: General ideas
This conversion will use the Java.text.SimpleDateFormat class
String converted to date type:
Method 1:
Also the simplest method date Date=new date ("2008-04-14");
Method 2:
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");/lowercase MM indicates minutes
String dstr= "2008-4-24";
Java.util.Date Date=sdf.parse (DSTR);
Date converted to string:
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date date=new java.util.Date ();
String Str=sdf.format (date);
2: program Example
Package test;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import java.text.ParseException;
Import Java.util.Date;
public class Stringordate {
public static string datetostring (date date, String type) {
String str = NULL;
DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
if (Type.equals ("short")) {
07-1-18
Format = dateformat.getdateinstance (Dateformat.short);
str = Format.format (date);
else if (type.equals ("MEDIUM")) {
2007-1-18
Format = dateformat.getdateinstance (Dateformat.medium);
str = Format.format (date);
else if (type.equals ("full")) {
Thursday, January 18, 2007
Format = dateformat.getdateinstance (dateformat.full);
str = Format.format (date);
}
return str;
}
public static Date stringtodate (String str) {
DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = null;
try {
Fri Feb 00:00:00 CST 2012
Date = Format.parse (str);
catch (ParseException e) {
E.printstacktrace ();
}
2012-02-24
Date = Java.sql.Date.valueOf (str);
return date;
}
public static void Main (string[] args) {
Date date = new Date ();
System.out.println (stringordate.datetostring (date, "MEDIUM"));
String str = "2012-2-24";
SYSTEM.OUT.PRINTLN (Stringordate.stringtodate (str));
}