相关文章:
- 《常用类 (一) ----- Arrays数组工具类》
- 《常用类 (二) ----- Math类》
- 《常用类 (三) ----- BigDecimal和BigInteger大数类》
- 《常用类 (四) ----- Random随机数类》
- 《常用类 (五) ----- Date日期类和Calender日历类》
- 《常用类 (六) ----- String类与字符串》
- 《常用类 (七) ----- 包装类》
时间类Date
一、将Date类以自定义格式打印出来
使用到的类:Date
和SimpleDateFormat
使用到的方法:SimpleDateFormat的String format(Date date)
方法
// 获取系统当前时间
Date nowDate = new Date();
System.out.println(nowDate); // 以默认格式输出:Fri Jul 10 18:01:05 CST 2020
// 自定义格式(字母的个数代表数字位数):
/*
* yyyy 年
* MM 月
* dd 日
* HH 时
* mm 分
* ss 秒
* SSS 毫秒
* 注意:除了上面的字母不能随意写之外,剩下的符号格式、字母顺序可以随意组织
* */
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
// 将Date对象转为字符串打印出来,以自定义格式输出:2020-07-10 18:10:37 757
System.out.println(sdf.format(nowDate));
二、将字符串转为Date对象
使用到的类:String
、Date
和SimpleDateFormat
使用到的方法:SimpleDateFormat的Date parse(String str)
方法
String s = "2020-12-14 11:30";
// 必须对应字符串的年月日时分
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = sdf.parse(s);
System.out.println(date);// 打印 Mon Dec 14 11:30:00 CST 2020
三、练习
-
从1970年1月1日到2020年7月1日每个月的1号为星期一的天数是多少?
Date date = null; int count = 0; // 遍历从1970-01-01、1997-02-01...一直到2020-07-10 // 每次遍历把Date转为字符串,按空格拆分字符串到String数组中 // 因为Date的默认打印格式形如:Fri Jul 10 18:01:05 CST 2020 // 所以判断String数组第一个元素为"Mon"、第三个元素为"01",则计数 for (int year = 70; year <= 120; year++) { for (int month = 1; month <= 12; month++) { // 注意:Date的构造方法中,year的设置为1900的偏移量 date = new Date(year, month, 1); String[] s = date.toString().split(" "); if (s[0].equals("Mon") && s[2].equals("01")) count++; } } System.out.println(count);
-
获取昨天的当前时刻
// 该构造方法传入的是从1970-01-01 00:00:00 000 开始的毫秒偏移量 // System.currentTimeMillis()——返回从1970-01-01 00:00:00 000 开始 到当前时刻的毫秒数 Date yesterday = new Date(System.currentTimeMillis() - 24 * 3600 * 1000); System.out.println(yesterday);// Fri Jul 10 11:00:29 CST 2020
日历类Calender
一、Calender类的实例化
Calender calender = Calender.getInstance();
二、Calender类常用的成员变量(日历字段)
字段 | 表示意义 |
---|---|
YEAR | 年 |
MONTH | 月 |
DAY_OF_MONTH | 日 |
DAY_OF_WEEK | 星期 |
DAY_OF_YEAR | 当前年中的第几天 |
DAY_OF_WEEK_IN_MONTH | 当前月中的第几周 |
HOUR | 时 |
MINUTE | 分 |
SECOND | 秒 |
注意:
以上成员变量均可通过Calender对象调用set、get方法来修改和获取
与我们正常思维不同的两个属性:MONTH和DAY_OF_WEEK
(1) MONTH
取值范围:0~11,(显示月份/输入月份)+1 = 实际月份,如:month = 0,2,8 表示:一月、三月、九月
(2) DAY_OF_WEEK
取值范围:1~7,(显示星期/输入星期) -1 = 实际星期,如:week = 1,4,7 表示:星期天、星期三、星期六
三、Calender的set方法
-
将给定字段修改为给定值——
void set(int field, int value)
Calendar calendar = Calendar.getInstance(); int year = 2000; int month = 11;// 实际上表示12月 int day = 14; calendar.set(Calendar.YEAR, year);// 2000年 calendar.set(Calendar.MONTH, month);// 12月 calendar.set(Calendar.DAY_OF_MONTH, day);// 14日
-
直接对Calender设置给定的年月日——
void set(int year, int month, int date)
Calendar calendar = Calendar.getInstance(); // 2019年6月7日 calendar.set(2019, 5, 7); // 2019年6月7日 9:30 calendar.set(2019, 5, 7, 9, 30); // 2019年6月7日 09:30:45 calendar.set(2019, 5, 7, 9, 30, 45);
四、Calender的get方法
获取Calender对象给定字段的值——int get(int field)
Calendar calendar = Calendar.getInstance();
// 2019年6月7日 09:30:45
calendar.set(2019, Calendar.JUNE, 7);
int year = calendar.get(Calendar.YEAR);// 2019年
int month = calendar.get(Calendar.MONTH);// 6月
int day = calendar.get(Calendar.DAY_OF_MONTH);// 7日
int week = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(week); // 去年高考时间是星期五
五、练习
-
从1970年1月1日到2300年12月1日每个月的1号为星期天的天数为多少?
Calendar calendar = Calendar.getInstance(); int count = 0; for(int year = 1970; year <= 2300; year++) { for(int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) { calendar.set(year, month, 1); if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { System.out.println(year + "-" + month + "-" + 1); count++; } } } System.out.println(count);
-
从1970年1月1日到2300年12月1日每年父亲节为6月20日的年数为多少?(父亲节为每年6月第三个星期天)
Calendar calendar = Calendar.getInstance(); int count = 0; for(int year = 1970; year <= 2300; year++) { calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, Calendar.JUNE);// 6月 calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);// 第三个星期 calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);// 星期天 if(calendar.get(Calendar.DAY_OF_MONTH) == 20) { System.out.println(year); count++; } } System.out.println(count);