Java中常用的日期、时间相关类
DateTimeFormatter | 用于时间的格式化和解析 |
---|---|
Date | 日期类 |
SimpleDateFormat | 简单日期格式化 |
Calendar | 日历类 |
LocalDate | 年、月、日 |
LocalTime | 时、分、秒 |
LocalDateTime | 年、月、日、时、分、秒 |
Date类
Date对象记录的时间是用毫秒值来表示的。Java语言规定,1970年1月1日0时0分0秒认为是时间的起点,此时记作0,那么1000(1秒=1000毫秒)就表示1970年1月1日0时0分1秒,依次内推。
构造器和常见方法
SimpleDateFormat类
- Date对象转换为指定格式的日期字符串这个操作,叫做日期格式化,
- 反过来把指定格式的日期符串转换为Date对象的操作,叫做日期解析。
构造器和常见方法
Calendar类
alendar类表示日历,它提供了一些比Date类更好用的方法。
注意calendar是可变对象,一旦做出修改会使自身时间发生变化。
public class Test4Calendar {
public static void main(String[] args) {
//1:获取当前系统的日历对象
Calendar now = Calendar.getInstance();
System.out.println(now);
//2:获取 日历中的 年
int year = now.get(Calendar.YEAR);
// int year = now.get(1);
System.out.println(year);
//获取 月中日
int day1 = now.get(Calendar.DAY_OF_YEAR);//年中日
int day2 = now.get(Calendar.DAY_OF_MONTH);//月中日
int day3 = now.get(Calendar.DATE);// 日--月中日
System.out.println(day1);//214
System.out.println(day2);//2
System.out.println(day3);//2
System.out.println(now.get(Calendar.DAY_OF_WEEK));//周中日
//3:将 日历对象转换日期对象
Date date = now.getTime();
System.out.println(date);
//4:获取日历对象的毫秒值
long time = now.getTimeInMillis();
System.out.println(time);
// 5:可以修改 指定日历信息
now.set(Calendar.YEAR,2028);
//月份改查10月
now.set(Calendar.MONTH,9);//9是10 11是12 0 是 1
System.out.println(now);
// 2028年 10月
//偏移
now.add(Calendar.YEAR,-2000);// 对指定的日历信息 进行偏移 负数 左偏移 正数 右偏移
System.out.println(now);
now.set(2011,10,11);//设置指定的日历信息
System.out.println(now);
}
}
LocalDate、LocalTime、LocalDateTime(三兄弟)
- LocalDate:代表本地日期(年、月、日、星期)
- LocalTime:代表本地时间(时、分、秒、纳秒)
- LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)
- 转换相关的API
LocalDate ld = ldt.toLocalDate();
LocalTime lt = ldt.toLocalTime();
LocalDateTime ldt10 = LocalDateTime.of(ld, lt);
LocalDate (年月日)
package com.itheima.new_three_bro;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
public class LocalDateDemo {
public static void main(String[] args) {
//1.获取当前时间的日历对象(包含 年月日)
LocalDate nowDate = LocalDate.now();
System.out.println("今天的日期:" + nowDate);
//2.获取指定的时间的日历对象
LocalDate ldDate = LocalDate.of(2004, 2, 20);
System.out.println("指定日期:" + ldDate);
System.out.println("=============================");
//3.get系列方法获取日历中的每一个属性值//获取年
int year = ldDate.getYear();
System.out.println("year: " + year);
//获取月//方式一:
Month m = ldDate.getMonth();
System.out.println(m);
System.out.println(m.getValue());
//方式二:
int month = ldDate.getMonthValue();
System.out.println("month: " + month);
//获取日
int day = ldDate.getDayOfMonth();
System.out.println("day:" + day);
//获取一年的第几天
int dayofYear = ldDate.getDayOfYear();
System.out.println("dayOfYear:" + dayofYear);
//获取星期
DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getValue());
//is开头的方法表示判断
System.out.println("是不是在当前时间之前:"+ldDate.isBefore(nowDate));
System.out.println("是不是在当前时间之后:"+ldDate.isAfter(nowDate));
//with开头的方法表示修改,只能修改年月日
LocalDate withLocalDate = ldDate.withYear(2000);//修改得到是新的对象 原对象不变
System.out.println("修改年之后新的时间:"+withLocalDate);
//minus开头的方法表示减少,只能减少年月日
LocalDate minusLocalDate = ldDate.minusYears(1);
System.out.println("减少1年之后"+minusLocalDate);
//plus开头的方法表示增加,只能增加年月日
LocalDate plusLocalDate = ldDate.plusDays(1);
System.out.println("增加一天之后:"+plusLocalDate);
//-------------
// 判断今天是否是你的生日
LocalDate birDate = LocalDate.of(2000, 2, 3);
LocalDate nowDate1 = LocalDate.now();
// 扩展 年月日对象 月中的天对象 9月28日
MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());
MonthDay nowMd = MonthDay.from(nowDate1);// 2 3
System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));//今天是你的生日吗?
}
}
LocalTime (时分秒)
package com.itheima.new_three_bro;
import java.time.LocalTime;
public class LocalTimeDemo {
public static void main(String[] args) {
// 获取本地时间的日历对象。(包含 时分秒) 精确到纳秒
LocalTime nowTime = LocalTime.now();
System.out.println("今天的时间:" + nowTime);
int hour = nowTime.getHour();//时
System.out.println("hour: " + hour);
int minute = nowTime.getMinute();//分
System.out.println("minute: " + minute);
int second = nowTime.getSecond();//秒
System.out.println("second:" + second);
int nano = nowTime.getNano();//纳秒
System.out.println("nano:" + nano);
System.out.println("------------------------------------");
System.out.println(LocalTime.of(8, 20));//时分
System.out.println(LocalTime.of(8, 20, 30));//时分秒
System.out.println(LocalTime.of(8, 20, 30, 150));//时分秒纳秒
LocalTime mTime = LocalTime.of(8, 20, 30, 150);
//is系列的方法
System.out.println("当前时分秒 是不是 在 mTime 之前:"+nowTime.isBefore(mTime));
System.out.println("当前时分秒 是不是 在 mTime 之后:"+nowTime.isAfter(mTime));
//with系列的方法,只能修改时、分、秒
System.out.println(nowTime.withHour(10));
//plus系列的方法,只能修改时、分、秒
System.out.println(nowTime.plusHours(10));
}
}
LocalDateTime (年月日时分秒)
package com.itheima.new_three_bro;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class LocalDateTimeDemo {
public static void main(String[] args) {
// 当前时间的的日历对象(包含年月日时分秒)
LocalDateTime nowDateTime = LocalDateTime.now();//获取 年月日时分秒 精确到纳秒
System.out.println("今天是:" + nowDateTime);//今天是:今天是:2024-02-03T11:32:25.514610600
System.out.println(nowDateTime.getYear());//年 2024
System.out.println(nowDateTime.getMonthValue());//月
System.out.println(nowDateTime.getDayOfMonth());//日
System.out.println(nowDateTime.getHour());//时
System.out.println(nowDateTime.getMinute());//分
System.out.println(nowDateTime.getSecond());//秒
System.out.println(nowDateTime.getNano());//纳秒
// 日:当年的第几天
System.out.println("dayofYear:" + nowDateTime.getDayOfYear());
//星期
System.out.println(nowDateTime.getDayOfWeek());
System.out.println(nowDateTime.getDayOfWeek().getValue());
//月份
System.out.println(nowDateTime.getMonth());
System.out.println(nowDateTime.getMonth().getValue());
// 把 年月日 时分秒 可以把它 拆成 LocalDate LocalTime
LocalDate ld = nowDateTime.toLocalDate();
System.out.println(ld);
LocalTime lt = nowDateTime.toLocalTime();
System.out.println(lt.getHour());
System.out.println(lt.getMinute());
System.out.println(lt.getSecond());
}
}
线程安全问题*
主要是传统的时期处理类Date、Calendar不是多线程安全的,
而LocalDate 线程安全的,所以不用担心并发问题。