时间日期类
日期类
1.规定
- 计算机中的起始时间: 1970年1月1日 00:00:00(c语言的诞生时间)
long time = System.currentTimeMillis()
->时间原点到当前时间过去的毫秒数- 北京时间需要在世界标准时间上加8小时。
2.Date类概述和构造方法和成员方法
Date 代表了一个特定的时间,精确到毫秒
构造方法:
//创建一个Date对象,表示默认时间
Date date1 = new Date();//表示电脑中当前时间
//创建要给对象,表示默认时间
Date date2 = new Date(0L);//->1970-1-1-8:00:00东八区+8小时。 从计算机原点开始,过了指定毫秒(参数)的那个时间
成员方法
public long getTime();
获取时间对象的毫秒值
Date date1 = new Date();
long time = date1.getTime();
sout(time);
setTime(Long time);
设置时间
Date date2 = new Date();
date2.setTime(0L);
sout(date2);
3.SimpleDateFormate日期格式
概述
y | 年 | M | 月 | d | 日 |
---|---|---|---|---|---|
H | 时 | m | 分 | s | 秒 |
2022-04-11 13:27:06
->yyyy-MM-dd HH:mm:ss
2022年04月11日 13:27:06
->yyyy年MM月dd日 HH:mm:ss
构造及方法
public SimpleDateFormate();
public simpleDateFormate(String pattern);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String result = sdf.format(date);
sout(result);//格式化之后的格式
解析
String s = "2022-04-10"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//参数与待解析对象要一直,否则会抛ParseException
Date date = sdf.parse(s);
sout(date);//格式化之后的格式
JDK1.8新增的特性
解决对时间对象直接进行加减计算存在的问题
传统做法,繁杂。
String s = "2022-04-11 13:27:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = sdf.parse(s);
long time = date.getTime();
time = time +(1000*60*60*24);
Date new Date = new Date(time);
String result = sdf.fromat(newDate);
sout(result);
JDK8提供的操作方法
String s = "2022-04-11 13:27:06";
DateTimeFormatter pattern = new DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(s.pttren);
LocalDateTime newLocalDateTime = localDateTime.plusDay(1);
String result = newLocalDateTIme.format(pattern);
sout(result);
1.获取时间对象
LocalDate
:表示日期LocalTime
:表示时间LocalDateTime
:表示日期+时间
LocalDateTime对应原来的Date类;
LocalDateTime方法名 | Date方法名 | 说明 |
---|---|---|
public static LocalDateTIme now(); | Date(); | 系统当前时间 |
public static LocalDateTIme of(int year,int month,int day,int hour,int minute,int second); | date(long time); | 指定时间,参数范围不对,会抛异常 |
2.获取时间中的每个值
方法名 | 说明 |
---|---|
public int getYear(); | 获取年 |
public int getMonthValue(); | 获取月份(1-12) |
public int getDayOfMonth(); | 获取月份中的第几天(1-31) |
public int getDayOfYear(); | 获取一年中的第几天(1-365) |
public DayOfWeek getDayOfWeek(); | 获取星期,DayOfWeek是枚举类 |
public int getMinute(); | 获取分钟 |
public int getHour(); | 获取小时 |
3.LocalDateTime
转换方法
LocalDate toLocalDate()
:LocalDateTime
对象转为LocalDateTime
对象LocalTime toLocalTime()
:LocalDateTime
对象转为LocalDateTime
对象
4.LocalDateTime
的格式化与解析
String format(DateTimeFormatter pttern)
:可以利用DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss")
;可以生成pattern
LocalDatTime parse(CharQuern ch,DateTimeFormatter pttern)
LocalDatTim.parse(s,pattern)
5.增加时间和减少时间的方法
增加方法:底层会创建新的LocalDateTime
对象
pulic LocalDateTime plusYears(int year)
:year 可以接收整数或者负数。
pulic LocalDateTime plusMonths(int month)
:
pulic LocalDateTime plusDays(int day)
:
pulic LocalDateTime plusHours(int hour)
:
pulic LocalDateTime plusSeconds(int second)
:
pulic LocalDateTime plusWeeks(int week)
:
pulic LocalDateTime plusMinutes(int minute)
:
减少方法:底层会创建新的LocalDateTim
对象
pulic LocalDateTime minusYears(int year)
:plusYears(int year)
相反
修改方法:底层会创建新的LocalDateTime
对象
pulic LocalDateTime withYear(int year)
:
pulic LocalDateTime withMonth(int month)
:
pulic LocalDateTime withDay(int day)
:
pulic LocalDateTime withHour(int hour)
:
pulic LocalDateTime withSecond(int second)
:
pulic LocalDateTime withWeek(int week)
:
pulic LocalDateTime withMinute(int minute)
:
修改时,传递进去的参数超出有效范围,会抛出异常
6.时间间隔Period类
public static Period between(begintime,endtime)
:计算两个时间的间隔
period方法:
public int getYears()
:获取这段时间的年数
public int getMonths()
: 获取这段时间之间的月数
public int getDays()
: 获取这段时间之间的天数
public long toTotalMonths()
: 获取这段时间之间的总月数
LocalDate localDate1 = LocalDate.of(2022,1,11);
LocalDate localDate2 = LocalDate.of(2022,3,11);
Period period = Period.between(localDate1,localDate2);
7.Duration
public static Duration between(LocalDateTime localDatetime,LocalDateTime localDatetime)
:返回时间间隔对象
LocalDateTime localDateTime1 = LocalDate.of(2022,1,11);
LocalDateTime localDateTime2 = LocalDate.of(2022,3,11);
Duration duration = Duration.between(localDateTime1,localDateTime2);
Long l1 = duration.toSeconds();//相隔秒数
Long l2 = duration.toMillis();//相隔毫秒数
Long l3 = duration.toNanos();//相隔纳秒数