Java string to date or date to string & Time increase Day calendar usagestring to date or date to string
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, I do not need to explain it again!
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 a 12-hour system.
1. String Turn date
2014-07-10 19:20:00 to turn it into a date, you can use date date = Sdf.parse ("2014-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 is similar to the 2014-07-10 19:20:00.
With this API we can turn the date into the string format we want, for example, if we want to export the date as July 10, 2014, we can write:
SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day");
String str = sdf.format (new Date ());
STR will output in accordance with the format we have set.
A simple example:
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 datetostr (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) {
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 to String:" + convertdemo.datetostr (date));
System.out.println ("String to date:" + convertdemo.strtodate (convertdemo.datetostr (date));
}
}
Time increases the use of the calendar day
Package com.hnwt.test;
import java.text.ParsePosition;
import Java.text.SimpleDateFormat;
import Java.util.Calendar;
import java.util.Date;
import Java.util.GregorianCalendar;
Public class Mynew {
Public Static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("date Added day =" + Addday ("2014-10-10", 1));
}
Public Static String Addday (string s, int N) {
Try {
SimpleDateFormat SDF = new simpledateformat ("Yyyy-mm-dd");
Calendar cd = Calendar.getinstance ();
Cd.settime (Sdf.parse (s));
Cd.add (calendar.date, n);//Add one day
Cd.add (Calendar.month, n);//Add one months
return Sdf.format (Cd.gettime ());
} catch (Exception e) {
return null;
}
}
}