1. Introduction to java.util.Calendar
The Calendar class is an abstract class that provides some methods for converting a specific moment to a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc., and provides some methods for manipulating calendar fields such as getting the date of the next week. The instant can be expressed as a millisecond value, which is the offset from the epoch (i.e., 00:00:00.000, GMT, January 1, 1970, GMT).
2. Simple examples
// Format the output date java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");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(today));System.out.println("Now is "+dateTimeFormat.format(today));2. Construct a specific time
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = new GregorianCalendar(2007, 11, 25,0,0,0); Date date = calendar.getTime(); System.out.println("2007 Christmas is:"+format.format(date));The parameters of the GregorianCalendar constructor are: year, month-1, day, hour, minute, and 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));3. Take each part of the date
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); int second =calendar.get(Calendar.SECOND);
Add 1 to the month
4. Get the maximum number of days in the current month
Calendar cal = Calendar.getInstance(); int day=cal.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.println(day);
5. 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()));6. 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(); System.out.println("the month first day is "+formats.format(firstDay));7. 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); System.out.println("Number of days separated="+day);8. Date one year 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); System.out.println(beforeDate);9. Date after one year
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); System.out.println(afterDate);Ten or ten hours later
java.util.Calendar Cal=java.util.Calendar.getInstance(); Cal.setTime(dateOper); Cal.add(java.util.Calendar.HOUR_OF_DAY,10); System.out.println("date:"+forma.format(Cal.getTime()));11. Time 10 hours ago
java.util.Calendar Cal=java.util.Calendar.getInstance(); Cal.setTime(dateOper); Cal.add(java.util.Calendar.HOUR_OF_DAY,-10); System.out.println("date:"+forma.format(Cal.getTime()));12. Monday and Sunday of the current date
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 week of the current date that belongs to this year
GregorianCalendar cal = new GregorianCalendar();int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);System.out.println("What week does this week belong to:" + weekOfYear);Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.