I. Get the current system time and date and format the output:
Import Java.util.Date;
Import Java.text.SimpleDateFormat;
public class Nowstring {
public static void Main (string[] args) {
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format
System.out.println (Df.format (new date));//new Date () to get current system time
}
}
Ii. acquisition of yesterday's time
Take yesterday's date, I would like to cut out the date of a good. Another thought wrong, if today is a number how to do.
Two options available
1:date as = new Date (new Date (). GetTime () -24*60*60*1000);
SimpleDateFormat matter1 = new SimpleDateFormat ("Yyyy-mm-dd");
String time = Matter1.format (AS);
System.out.println (time);
Take out the digital time and subtract 24*60*60*1000, you get yesterday's time;
This is a little out of date.
2:calendar cal = Calendar.getinstance ();
Cal.add (Calendar.date,-1);
String yesterday = new SimpleDateFormat ("Yyyy-mm-dd"). Format (Cal.gettime ());
System.out.println (yesterday);
This method is very convenient, the month and the day can change as one pleases. Look at the program below.
Package Com.yiibai;
Import Java.util.Calendar;
public class Calendardemo {public
static void Main (string[] args) {
//Create a calendar
Calendar cal = Calen Dar.getinstance ()
//print current date
System.out.println ("The current date is:" + cal.gettime ());
Add
to the Calendar cal.add (calendar.date);
System.out.println ("Days later:" + cal.gettime ());
Subtract 2 months
from the calendar cal.add (Calendar.month,-2);
System.out.println ("2 months ago:" + cal.gettime ());
Subtract 5 year
from the calendar cal.add (Calendar.year,-5);
System.out.println ("5 years ago:" + cal.gettime ());
}
Let's compile and run the above program, which will produce the following results:
The current date Is:thu Apr 02:13:28 eest later:wed may
02:13:28 eest
2 months Ago:fri Mar 02:13:28 EET
5 years ago:fri Mar 02:13:28 EET 2007