One: Get the current time, an hour ago, an hour later, January ago, the time after January
GregorianCalendar a = new GregorianCalendar ();
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
System.out.println ("Current Time:" +format.format (A.gettime ()));
A.add (Calendar.hour_of_day,-1);
System.out.println ("An hour ago:" +format.format (A.gettime ()));
A = new GregorianCalendar ();
A.add (Calendar.day_of_month,-1);
System.out.println ("One day ago:" +format.format (A.gettime ()));
A = new GregorianCalendar ();
A.add (Calendar.month,-1);
System.out.println ("Previous month:" +format.format (A.gettime ()));
A = new GregorianCalendar ();
A.add (Calendar.month, 1);
System.out.println ("After January:" +format.format (A.gettime ()));
A = new GregorianCalendar ();
A.add (calendar.hour_of_day,5);
Two: Get the current month maximum days
Calendar cal = Calendar.getinstance ();
int Day=cal.getactualmaximum (calendar.day_of_month);
SYSTEM.OUT.PRINTLN ("Maximum days of the current month:" +day);
Three: Format date public
static Date parsedate (string datestr, string format) {
date date = null;
try {
SimpleDateFormat format2 = new SimpleDateFormat (format);
Date = Format2.parse (DATESTR);
} catch (Exception e) {
} return
date;
}
Four: Date turns string public
static string format (date date, String format) {
string result = "";
try {
if (date!= null) {
SimpleDateFormat DateFormat = new SimpleDateFormat (format);
result = Dateformat.format (date);
}
catch (Exception e) {
} return result
;
}
Five: Return this time is the number of weeks of this year public
static int Getyearofweek (date date) {
Calendar calendar = calendar.getinstance ();
Calendar.settime (date);
Return Calendar.get (calendar.week_of_year);
}
VI: Judge whether two dates are not one week public
static Boolean weekofyeak (date date1, date date2) {
String dates1 = Getmondayofthisweek ( DATE1);
String dates2 = Getmondayofthisweek (date2);
if (Dates1.equals (Dates2)) {return
true;
}
return false;
}
Vii. return this time is the number of days of the year public
static int getyearofday (date date) {
Calendar calendar = calendar.getinstance ();
Calendar.settime (date);
Return Calendar.get (calendar.day_of_year);
}