第二十天:Calendar类
吾日三省吾身:早上吃什么,中午吃什么,晚上吃什么
狂神未更新,转千锋教育 (bilibili.com)
学习内容
Calendar
获取时间
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
| package com.joker_yue.javalearn.CalendarLearn;
import java.util.Calendar;
public class Demo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); System.out.println(calendar.getTimeInMillis()); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); 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); System.out.println(year+"年"+ month+1+"月"+day+"日"+hour+":"+minute+":"+second); } }
|
最后的输出结果为
1 2 3
| Wed Sep 14 22:16:41 CST 2022 1663165001103 2022年81月14日22:16:41
|
修改时间
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
| package com.joker_yue.javalearn.CalendarLearn;
import java.util.Calendar;
public class Demo { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); System.out.println(calendar.getTimeInMillis()); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); 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); System.out.println(year+"年"+ (month+1)+"月"+day+"日"+hour+":"+minute+":"+second);
Calendar cal = calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH,6); System.out.println(cal.getTime() );
cal.add(Calendar.HOUR,1); System.out.println(cal.getTime());
int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.println(max); int min = cal.getActualMinimum(Calendar.DAY_OF_MONTH); System.out.println(min); } }
|
最后的输出结果为
1 2 3 4 5 6 7
| Wed Sep 14 22:35:54 CST 2022 1663166154685 2022年9月14日22:35:54 Tue Sep 06 22:35:54 CST 2022 Tue Sep 06 23:35:54 CST 2022 30 1
|