1.Date类
java
//创建对象表示当前时间
Date d1 = new Date();
System.out.println(d1);
//创建对象表示一个指定的时间
Date d2 = new Date(0L);
System.out.println(d2);//Thu Jan 01 08:00:00 CST 1970
//settime设置时间
//1000毫秒 = 1秒
d1.setTime(1000l);//Thu Jan 01 08:00:01 CST 1970
System.out.println(d1);
- 打印时间原点一年以后的时间
Java
private static void extracted() {
Date d1 = new Date(0);
System.out.println(d1);//Thu Jan 01 08:00:00 CST 1970
long time = d1.getTime();
time = time + 1000l*60*60*24*365;
d1.setTime(time);
System.out.println(d1);//Fri Jan 01 08:00:00 CST 1971
}
- 定义任意两个date对象,比较哪个时间在前,哪个时间在后
Java
private static void max() {
Random r = new Random();
Date date01 = new Date(Math.abs(r.nextInt()));
Date date02 = new Date(Math.abs(r.nextInt()));
System.out.println(date01);
System.out.println(date02);
long time01 = date01.getTime();
long time02 = date02.getTime();
if (time01 > time02) {
System.out.println("time01 > time02");
}else {
System.out.println("time01 < time02");
}
}
1.1 ZoodID
java
//1.获取Java中支持的所有时区
Set<String> ZoneIds = ZoneId.getAvailableZoneIds();
System.out.println(ZoneIds);
//2.获取系统默认时区
ZoneId zoneId1 = ZoneId.systemDefault();
System.out.println(zoneId1);
//3.获取一个指定时区
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
System.out.println(zoneId);
1.2 Istant
java
//1.获取当前的时间Instant对象
Instant instant = Instant.now();
System.out.println(instant);
//2.根据(秒/毫秒/纳秒)获取Instant对象
Instant instant1 = Instant.ofEpochMilli(1000l);//根据毫秒获取对象
System.out.println(instant1);
Instant instant2 = Instant.ofEpochSecond(1l);//根据秒获取对象
System.out.println(instant2);//根据秒和纳秒获取对象
Instant instant3 = Instant.ofEpochSecond(2l, 1000000000l);
System.out.println(instant3);
//3.获取指定时区的时间
ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(zonedDateTime);
//isBefore 判断是否在前面
Instant instant4 = Instant.ofEpochMilli(100l);
Instant instant5 = Instant.ofEpochSecond(1l);
boolean result = instant4.isBefore(instant5);
System.out.println(result);
//isAfter 判断是否在后面
boolean result1 = instant4.isAfter(instant5);
System.out.println(result);
//4.minusXxx 减少时间系列的方法
Instant instant7 = Instant.ofEpochSecond(3l);//1970-01-01T00:00:03Z
Instant instant8 = instant7.minusSeconds(1);
System.out.println(instant8);//1970-01-01T00:00:02Z
//5.plusXxx 增加时间系列的方法
Instant instant9 = instant8.plusSeconds(1);
System.out.println(instant9);
}
1.3 ZoneDatetime
java
public static void main(String[] args) {
//1.获得当前时间的ZoneDateTime对象(带时区)
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);//2026-01-23T14:36:48.289692100+08:00[Asia/Shanghai]
//2.获得指定时间的ZoneDatetime对象(带时区)
//按年月日时分秒纳秒时区指定
ZonedDateTime.of(2026,6,16,23,23,
22,23, ZoneId.of("Asia/Shanghai"));
//3.通过Instant+时区的方法获取指定事件对象
Instant instant = Instant.ofEpochMilli(0l);
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
System.out.println(ZonedDateTime.ofInstant(instant, zoneId));//1970-01-01T08:00+08:00[Asia/Shanghai]
//4.withXxx 修改时间系列的方法
ZonedDateTime zonedDateTime = now.withYear(2000);
System.out.println(zonedDateTime);//2000-01-23T14:48:58.668346200+08:00[Asia/Shanghai]
//减少时间系列的方法
ZonedDateTime zonedDateTime1 = zonedDateTime.minusDays(1);
System.out.println(zonedDateTime1);//2000-01-22T14:54:56.174043700+08:00[Asia/Shanghai]
//增加时间系列的方法
ZonedDateTime zonedDateTime2 = zonedDateTime.plusDays(1);
System.out.println(zonedDateTime2);//2000-01-24T14:54:56.174043700+08:00[Asia/Shanghai]
}
/*jdk8出现的时间对象都是不可改变的
如果我们修改 增加或者减少
调用者是不会发生改变的 只会产生新的时间对象
*/
2.Calender(日历类)
java
public static void main(String[] args) {
/*1.获取日历对象
细节1.
Calendar是一个抽象类 不能直接new出来 需要用静态方法获取到子类对象
底层原理:会根据系统的不同时区获取不同的日历对象,默认为当前时间
会把时间中的纪元,年,月,日,时,分,秒都放进一个数组中
细节2.
月份:范围0~11 如果获取的是0实际上得到的是一月
星期:在老外眼里星期日就是一周中的第一天 1(星期天) 2(星期一)
*/
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
// Date d = new Date(0l);
// c.setTime(d);
long timeInMillis = c.getTimeInMillis();//拿到时间毫秒值
c.setTimeInMillis(0l);//给日历对象设置时间毫秒值
System.out.println(c.getTime());//Fri Jan 23 14:01:44 CST 2026
c.set(Calendar.HOUR_OF_DAY,0);//修理日历中某的字段的信息
c.add(Calendar.DAY_OF_MONTH,1);//为某个字段的信息增加或减少指定的值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
System.out.println(year+" "+month+" "+day+" "+getweek(week));//2026 1 24 星期六
getweek(week);
}
private static String getweek(int week) {
String arr[] = {"","星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
return arr[week];
}
}
2.1LocalDate (年月日)
Java
public static void main(String[] args) {
//当前时间的日历对象 包括年月日
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
//获得指定时间的日历对象
LocalDate localDate1 = LocalDate.of(2024,4,2);
System.out.println(localDate1);
System.out.println("==========================");
//get方法获取日历中每个属性的值
//年
int year = localDate.getYear();
System.out.println(year);//2026
//月: 第一种形式
int month = localDate.getMonthValue();
System.out.println(month);//1
// 第二种形式
Month month1 = localDate.getMonth();
System.out.println(month1.getValue());//1
//日
int day = localDate.getDayOfMonth();
System.out.println(day);//23
//周
DayOfWeek dayOfWeek = localDate1.getDayOfWeek();
System.out.println(dayOfWeek); //TUESDAY
System.out.println(dayOfWeek.getValue());//2
System.out.println("==========================");
//is开头的方法表示判断
boolean after = localDate.isAfter(localDate);
System.out.println(after);//false
boolean after1 = localDate1.isAfter(localDate);
System.out.println(after1);//false
System.out.println("==========================");
//with开头的表示修改方法 只能修改年月日
LocalDate localDate2 = localDate1.withYear(2000);
System.out.println(localDate2);//2000-04-02
System.out.println("==========================");
//minus开头的方法表示减少,只能减少年月日
LocalDate localDate3 = localDate.minusYears(1);
System.out.println(localDate3);//2025-01-23
//plus开头的方法表示增加,只能增加年月日
LocalDate localDate4 = localDate.plusYears(1);
System.out.println(localDate4);//2027-01-23
System.out.println("==========================");
//判断今天是不是你生日
LocalDate bir = LocalDate.of(2026, 1, 12);
Month today = Month.from(LocalDate.now());
System.out.println("今天是你的生日吗"+today.equals(bir));
}
2.2LocalTime(时分秒)
Java
public static void main(String[] args) {
// 获取当前日历对象 包括时分秒
LocalTime now = LocalTime.now();
System.out.println("今天的时间"+now);
int hour = now.getHour();//时
System.out.println(hour);
int minute = now.getMinute();//分
System.out.println(minute);
int second = now.getSecond();//秒
System.out.println(second);
int nano = now.getNano();//纳秒
System.out.println(nano);
System.out.println("==========================");
System.out.println(LocalTime.of(11, 12));
System.out.println(LocalTime.of(11, 12, 30));
System.out.println(LocalTime.of(11, 12, 30,2000));
LocalTime localTime = LocalTime.of(11, 12, 30,2000);
System.out.println("==========================");
//is系列的方法
System.out.println(localTime.isBefore(now));//true
System.out.println(localTime.isAfter(now));//false
//with系列的方法 只能修改时分秒
System.out.println( now.withHour(2));//02:31:08.892886700
//minus系列的方法 只能减少时分秒
System.out.println(now.minusHours(2));//15:31:08.892886700
//plus系列的方法,只能增加时分秒
System.out.println(now.plusHours(2));//19:31:08.892886700
2.3 localDateTime(年月日时分秒)
Java
public static void main(String[] args) {
//当前时间的日历对象 包括年月日时分秒
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("今天是"+localDateTime);//今天是2026-01-23T16:17:35.229178800
System.out.println(localDateTime.getYear());//年 2026
System.out.println(localDateTime.getMonthValue());//月 1
System.out.println(localDateTime.getDayOfMonth());//日23
System.out.println(localDateTime.getHour());//时 16
System.out.println("==========================");
//日:当年的第几天
System.out.println(localDateTime.getDayOfYear());
//月:
Month month = localDateTime.getMonth();
System.out.println(month.getValue());//1
//星期:
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
System.out.println(dayOfWeek.getValue());//5
System.out.println("==========================");
//转换为LocalData对象
LocalDate ld = localDateTime.toLocalDate();
System.out.println(ld);//2026-01-23
//转换为LocalTime对象
LocalTime lt = localDateTime.toLocalTime();
System.out.println(lt);//16:36:03.871771300
System.out.println(lt.getHour());//16
System.out.println(lt.getMinute());//36
System.out.println(lt.getSecond()); //3
3.日期格式化类
3.1 SimpleDateFormat
将日期对象解析为字符串
java
private static void method() {
//使用空参构造创建对象,默认格式
SimpleDateFormat sdf1 = new SimpleDateFormat();
Date d1 = new Date(0l);
System.out.println(sdf1.format(d1));//1970/1/1 08:00
//使用带参构造创建对象
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d2 = new Date(0l);
System.out.println(sdf2.format(d2));//1970年01月01日 08:00:00
}
将字符串解析为日期对象
java
//1.定义一个字符串来表示时间
String sj = "2025-11-11 11:11:11";
//2.利用构造方法创建对象
// 细节:创建对象的格式要跟字符串的格式完全一致
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse(sj);
//3.打印结果
System.out.println(d1.getTime());
练习
Java
public static void main(String[] args) throws ParseException {
//肯德基举办活动 从10点到10点10 小明下单时间为10点10点01 小明是否参与活动
String starttime = "10:00:00";
String endtime = "10:10:00";
String ordertime = "10:01:00";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date start = sdf.parse(starttime);
Date end = sdf.parse(endtime);
Date order = sdf.parse(ordertime);
if (order.getTime() <= end.getTime()&&order.getTime() >= start.getTime()) {
System.out.println("参与了活动" );
}else {
System.out.println("没有参加活动");
}
}
4.工具类
4.1Period(年月日)
java
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now);//2026-01-24
LocalDate birthday = LocalDate.of(2001,1,1);
System.out.println(birthday);//2001-01-01
Period period = Period.between(birthday,now);
System.out.println("相差的时间间隔Period is " + period);//相差的时间间隔Period is P25Y23D
System.out.println(period.getYears());//25
System.out.println(period.getMonths());//0
System.out.println(period.getDays());//23
System.out.println(period.toTotalMonths());//300
4.2Duration(时分秒)
java
//用于计算两个时间间隔(秒,纳秒)
LocalTime time = LocalTime.now();
System.out.println(time);
LocalTime time1 = LocalTime.of(1,1,1);
System.out.println(time1);
Duration duration = Duration.between(time,time1);
System.out.println(duration);
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
4.3 ChronoUnit(年月日时分秒)
Java
LocalDateTime time = LocalDateTime.now();
System.out.println(time);
LocalDateTime bir = LocalDateTime.of(2001, 6, 17, 6, 20, 20);
System.out.println(bir);
System.out.println("相差的年数为"+ChronoUnit.YEARS.between(bir,time));
System.out.println("相差的月数为"+ChronoUnit.MONTHS.between(bir,time));
System.out.println("相差的天数为"+ChronoUnit.DAYS.between(bir,time));
System.out.println("相差的小时数为"+ChronoUnit.HOURS.between(bir,time));
System.out.println("相差的分钟数为"+ChronoUnit.MINUTES.between(bir,time));
System.out.println("相差的秒数为"+ChronoUnit.SECONDS.between(bir,time));
System.out.println("相差的毫秒数为"+ChronoUnit.MILLIS.between(bir,time));
System.out.println("相差的纳秒数为"+ChronoUnit.NANOS.between(bir,time));
System.out.println("相差的半天数为"+ChronoUnit.HALF_DAYS.between(bir,time));
System.out.println("相差的十年数为"+ChronoUnit.DECADES.between(bir,time));
System.out.println("相差的周数为"+ChronoUnit.WEEKS.between(bir,time));
System.out.println("相差的世纪(百年)数为"+ChronoUnit.CENTURIES.between(bir,time));
System.out.println("相差的千年数为"+ChronoUnit.MILLENNIA.between(bir,time));
System.out.println("相差的纪元数为"+ChronoUnit.ERAS.between(bir,time));