Calendar 类
参考文档:https://blog.csdn.net/weixin_44259720/article/details/86669177
概述
1 2
| public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar>{}
|
该类由 abstract 修饰,是一个抽象类,因此无法通过 new 来实现。它提供了 getInstance()方法来获取由当前日期及时间初始化的子类。
常用方法
获取时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import java.text.SimpleDateFormat; import java.util.Calendar; public class CalenderTest { @Test public void testGetCalender() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = cal.getTime(); String nowTime = sdf.format(cal.getTime());
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day_of_month = cal.get(Calendar.DAY_OF_MONTH); int day = cal.get(Calendar.DATE);
int max_day_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int hour24 = cal.get(Calendar.HOUR_OF_DAY);
int hour12 = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1;
int amOrPm = cal.get(Calendar.AM_PM);
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
int week_of_month = cal.get(Calendar.WEEK_OF_MONTH);
int day_of_week_in_month = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
int day_of_year = cal.get(Calendar.DAY_OF_YEAR); } }
|
设置时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import java.text.SimpleDateFormat; import java.util.Calendar; public class CalenderTest { @Test public void testSetCalender() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = cal.getTime(); String nowTime = sdf.format(cal.getTime());
cal.set(Calendar.HOUR_OF_DAY, 11); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 30); String date1 = sdf.format(cal.getTime());
cal.set(Calendar.YEAR, 2018); cal.set(Calendar.MARCH, 7); cal.set(Calendar.DATE, 21); String date2 = sdf.format(cal.getTime());
String newDate1 = sdf.format(cal.getTime()); String newDate2 = sdf.format(new Date()); } }
|
时间运算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import java.text.SimpleDateFormat; import java.util.Calendar; public class CalenderTest { @Test public void testAddCalender() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowTime = sdf.format(cal.getTime());
cal.add(Calendar.YEAR, 1); String time1 = sdf.format(cal.getTime());
cal.add(Calendar.MONTH, 2); String time2 = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 5); String time3 = sdf.format(cal.getTime());
cal.add(Calendar.HOUR_OF_DAY, -1); String time4 = sdf.format(cal.getTime());
cal.add(Calendar.MINUTE, 1); String time5 = sdf.format(cal.getTime());
cal.add(Calendar.SECOND, 1); String time6 = sdf.format(cal.getTime()); } }
|