Package Com.test.TestBoot.SingleModel;
import Java.text.SimpleDateFormat;
import java.util.Date;
Public class Test {
Public static void Main (string[] args) {
/**
* Date Turn String
*/
Date date = new Date ();
String datestr = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (date);
System.out.println (DATESTR);
/**
*string Turn Date
*/
String ss = "2016-10-24 21:59:06";
SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
try {
Date D = sdf.parse (ss);
System.out.println (d);
} catch (ParseException e) {
e.printstacktrace ();
}
/**
* First day of the month
*/
Calendar MONTHCA = Calendar.getinstance ();
Monthca.add (calendar.month, 0);
Monthca.set (calendar.day_of_month,1);
String monthfirstday = Sdf.format (Monthca.gettime ()); First day of the month
System.out.println (Monthfirstday);
/**
* Last day of the month
*/
Calendar calendar = Calendar.getinstance ();
Calendar.set (Calendar.date, Calendar.getactualmaximum (calendar.date));
String monthlastday = Sdf.format (Calendar.gettime ()); Last day of the month
System.out.println (Monthlastday);
/**
* Yesterday
*/
Calendar STARTCA = Calendar.getinstance (); Get an example of a calendar
Startca.settime (New Date ()); Set time to current time
Startca.add (Calendar.date,-1); Date minus 1
Date sdate = Startca.gettime (); The day before
SimpleDateFormat sd = new SimpleDateFormat ("Yyyy-mm-dd");
String starttime = Sd.format (sdate); The day before (yesterday)
System.out.println (starttime);
/**
* First day of last month
*/
Calendar Lastmonthfirst = Calendar.getinstance ();
Lastmonthfirst.add (Calendar.month,-1);
Lastmonthfirst.set (Calendar.day_of_month, 1);
String lastmonthfirstday = Sd.format (Lastmonthfirst.gettime ()); The first day of last month
System.out.println (Lastmonthfirstday);
/**
* The last day of last month
*/
Calendar lastmonthlast = Calendar.getinstance ();
Lastmonthlast.set (Calendar.day_of_month, 1);
Lastmonthlast.add (Calendar.date,-1);
String lastmonthlastday = Sd.format (Lastmonthlast.gettime ()); Last day of last month
System.out.println (Lastmonthlastday);
}
}
Java Common date format conversions and date acquisition