First, Java.util.Calendar Introduction
The Calendar class is an abstract class that provides methods for a specific moment to transform between a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and for manipulating calendar fields, such as getting the date of next week. Instantly available in milliseconds, it is an offset from the 00:00:00.000 (i.e. Greenwich Mean time, January 1, 1970, Gregorian calendar).
Second, simple example
java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd") by formatting the output date;
Calendar cal = Calendar.getinstance ();//Take the current date.
System.out.println ("Today is:" + Format.format (Cal.gettime ()));
Cal = Calendar.getinstance ();
Cal.add (Calendar.day_of_month,-1);//Take the day before the current date.
System.out.println ("Yesterday is:" + Format.format (Cal.gettime ()));
Cal = Calendar.getinstance ();
Cal.add (Calendar.day_of_month, + 1);//Take the day after the current date.
System.out.println ("NextDay is:" + Format.format (Cal.gettime ()));
Or
Java.util.Date today=new java.util.Date ();
Java.text.SimpleDateFormat DateFormat = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.text.SimpleDateFormat DateTimeFormat = new Java.text.SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
System.out.println ("Today is" +dateformat.format);
System.out.println (today) +datetimeformat.format;
Ii. Construction of specific time
Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Calendar calendar = new GregorianCalendar (2007, 25,0,0,0);
Date date = Calendar.gettime ();
System.out.println ("2007 Christmas is:" +format.format (date));
GregorianCalendar Construction method parameters are: year, month-1, day, hour, minute, second.
Or
Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date date= format.parse ("2007-12-25");
System.out.println ("2007 Christmas is:" +format.format (date));
Three, take the date of each part
int year =calendar.get (calendar.year);
int Month=calendar.get (calendar.month) +1;
int day =calendar.get (calendar.day_of_month);
int hour =calendar.get (calendar.hour_of_day);
int minute =calendar.get (calendar.minute);
Take the month to add 1
Iv. get the maximum number of days for the current month
Calendar cal = Calendar.getinstance ();
int Day=cal.getactualmaximum (calendar.day_of_month);
V. Take the last day of the month
Calendar cal = Calendar.getinstance ();
int Maxday=cals.getactualmaximum (calendar.day_of_month);
Java.text.Format formatter3=new Java.text.SimpleDateFormat ("yyyy-mm-" +maxday);
System.out.println (Formatter3.format (Cal.gettime ()));
Six, take the first day of the month
Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("yyyy-mm-01");
Java.util.Date firstday=new java.util.Date ();
Vii. find the number of days between two dates
Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date begindate= format.parse ("2007-12-24");
Java.util.Date enddate= format.parse ("2007-12-25");
Long day= (Date.gettime ()-mydate.gettime ())/(24*60*60*1000);
Dated eight or one years ago.
Java.text.Format formatter=new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date todaydate=new java.util.Date ();
Long Beforetime= (Todaydate.gettime ()/1000) -60*60*24*365;
Todaydate.settime (beforetime*1000);
String Beforedate=formatter.format (todaydate);
Nine or one years after the date
Java.text.Format formatter=new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date todaydate=new java.util.Date ();
Long Aftertime= (Todaydate.gettime ()/1000) +60*60*24*365;
Todaydate.settime (aftertime*1000);
String Afterdate=formatter.format (todaydate);
Ten, 10 hours after the time
Java.util.Calendar cal=java.util.calendar.getinstance ();
Cal.settime (dateoper);
Cal.add (java.util.calendar.hour_of_day,10);
Xi. 10 hours ago
Java.util.Calendar cal=java.util.calendar.getinstance ();
Cal.settime (dateoper);
Cal.add (JAVA.UTIL.CALENDAR.HOUR_OF_DAY,-10);
12, the current date of Monday and Sunday
SimpleDateFormat DateFormat = new SimpleDateFormat ("YyyyMMdd");
GregorianCalendar cal = new GregorianCalendar ();
int dayinweek = Cal.get (Calendar.day_of_week);
int offset = 0;
if (Dayinweek = = 1) {
//Sunday
offset = 6;
} else {
//Monday to Saturday
offset = dayInWeek-2;
}
Cal.add (Gregoriancalendar.day_of_month,-offset);
String sday = Dateformat.format (Cal.gettime ());
Cal.add (Gregoriancalendar.day_of_month, 6);
String Eday = Dateformat.format (Cal.gettime ());
System.out.println ("Monday of this week:" + sday);
System.out.println ("Sunday of this week:" + eday);
12, get the current date of the week belongs to the first few weeks of this year
GregorianCalendar cal = new GregorianCalendar ();
int weekofyear = Cal.get (calendar.week_of_year);
SYSTEM.OUT.PRINTLN ("This week belongs to the first few weeks:" + weekofyear);
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.