日期时间
package com.itheima.demo;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Test {
public static void main(String[] args) {
// Date
//SimpleDateFormat
// Calendar
//JDK1.8 提供的时间日期API
//LocalDate 年月日
// LocalTime、 时分秒
// LocalDateTime 年月日时分秒
//获取当前时间的三种方式
LocalDate now = LocalDate.now();
System.out.println(now);//2020-09-03
LocalTime now1 = LocalTime.now();
System.out.println(now1);//16:24:51.912
LocalDateTime now2 = LocalDateTime.now();
System.out.println(now2);//2020-09-03T16:24:51.912
}
}
package com.itheima.demo;
import java.time.LocalDateTime;
import java.time.Month;
public class Test1 {
public static void main(String[] args) {
//指定 日期 of() 指定日期
LocalDateTime of = LocalDateTime.of(2019, 8, 6, 14, 20, 20);
System.out.println(of);//2019-08-06T14:20:20
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
System.out.println(year);//2020
int dayOfYear = now.getDayOfYear();
System.out.println(dayOfYear);//247
int dayOfMonth = now.getDayOfMonth();
System.out.println(dayOfMonth);//3
Month month = now.getMonth();
System.out.println(month);//SEPTEMBER 输出季节的英文大写
int nano = now.getNano();
System.out.println(nano);//946000000
int second = now.getSecond();
System.out.println(second);//37
}
}
package com.itheima.demo;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test3 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
//格式化日期
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
String format = now.format(f);
System.out.println(format);//2020年09月03日 17时19分30秒
//把日期字符串,按照指定的格式,解析成日期对象
String str = "2020年08月29日 14时50分02秒";
LocalDateTime p = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒"));
System.out.println(p);//2020-08-29T14:50:02
}
}
package com.itheima.demo;
import java.time.LocalDate;
public class Test4 {
public static void main(String[] args) {
LocalDate of = LocalDate.of(2022, 2, 3);
LocalDate now = LocalDate.now();
boolean after = of.isAfter(now);
System.out.println(after);//true
System.out.println(of.isBefore(now));//false
System.out.println(of.isEqual(now));//false
System.out.println(of.isLeapYear());//false
}
}
package com.itheima.demo;
import java.time.LocalDateTime;
public class Test5 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
//plusXXX 给日期增加时间量系列的方法,注意返回的 的是一个新的日期对象
LocalDateTime localDateTime = now.plusYears(2);
System.out.println(localDateTime);//2022-09-03T17:27:01.068
//minusXXX 给日期减去相应的时间量
LocalDateTime localDateTime1 = now.minusDays(9);
System.out.println(localDateTime1);//2020-08-25T17:27:01.068
}
}
给定指定的日期对象
LocalDate now = LocalDate.now();
//withXXX指定日期的方法,返回的是一个新的日期对象
LocalDate localDate = now.withYear(2055);
System.out.println(localDate);
System.out.println("================================");
//TemporalAdjuster temporalAdjuster = TemporalAdjusters.firstDayOfMonth();
// TemporalAdjuster next = TemporalAdjusters.next(DayOfWeek.FRIDAY);
// TemporalAdjuster temporalAdjuster = TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY);
// TemporalAdjuster previous = TemporalAdjusters.previous(DayOfWeek.FRIDAY);
TemporalAdjuster temporalAdjuster = TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY);
LocalDate with = now.with(temporalAdjuster);
System.out.println(with);
Instant 时间戳类从1970-01-01 00:00:00 截止到当前时间的毫秒值
-
获取对象的方法 now() 注意默认获取出来的是当前的美国时间和我们相差八个小时 Instant ins = Instant.now(); System.out.println(ins); 我们在东八区 所以可以加8个小时 就是我们的北京时间
-
2. Instant中设置偏移量的方法:atOffset() 设置偏移量 OffsetDateTime time = ins.atOffset(ZoneOffset.ofHours(8)); System.out.println(time); 3.获取系统默认时区时间的方法atZone() 方法的参数是要一个时区的编号可以通过时区编号类获取出来 ZoneId.systemDefault()获取本地的默认时区ID ZonedDateTime zonedDateTime = ins.atZone(ZoneId.systemDefault()); System.out.println(zonedDateTime); 4.get系列的方法 getEpochSecond() 获取从1970-01-01 00:00:00到当前时间的秒值 toEpochMilli();获取从1970-01-01 00:00:00到当前时间的毫秒值 getNano()方法是把获取到的当前时间的秒数 换算成纳秒 long epochSecond = ins.getEpochSecond();//获取从1970-01-01 00:00:00到当前时间的秒值 getNano()方法是把获取到的当前时间的豪秒数 换算成纳秒 比如当前时间是2018-01-01 14:00:20:30 那就把30豪秒换算成纳秒 int nano = ins.getNano(); 3. ofEpochSecond()方法 给计算机元年增加秒数 ofEpochMilli() 给计算机元年增加毫秒数 例如 Instant instant = Instant.ofEpochSecond(5); System.out.println(instant); 单位换算 0.1 毫秒 = 10 的5次方纳秒 = 100000 纳秒 1 毫秒 = 1000 微妙 = 1000000 纳秒
Instant now = Instant.now();
System.out.println(now);
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
System.out.println("===========================");
//ZoneID 时区类
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
for (String availableZoneId : availableZoneIds) {
System.out.println(availableZoneId);
/* if (availableZoneId.contains("Shanghai")) {
System.out.println(availableZoneId);
}*/
}
//获取从1970--到现在所间隔的毫秒值
// long l = System.currentTimeMillis();
long time = new Date().getTime();
System.out.println(time);
Instant now = Instant.now();
//获取从1970--到现在所间隔的秒值
long epochSecond = now.getEpochSecond();
System.out.println(epochSecond);
获取从1970--到现在所间隔的毫秒值
long l = now.toEpochMilli();
System.out.println(l);
System.out.println("=========================");
Instant instant = Instant.ofEpochSecond(60 * 60 * 24);
System.out.println(instant);
Instant now1 = Instant.now();
int nano = now1.getNano();
System.out.println(nano);
LocalDateTime now = LocalDateTime.now();
String format = now.format(DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss SSSS”));
System.out.println(format);
int nano = now.getNano();//把30豪秒换算成纳秒
System.out.println(nano);
Duration : 用于计算两个“时间”间隔的类
Period : 用于计算两个“日期”间隔的类
Duration类中静态方法between()
Instant start = Instant.now();
for(int i=0;i<1000L;i++){
System.out.println("循环内容");
}
Instant end = Instant.now();
静态方法:between() 计算两个时间的间隔,默认是秒
Duration between = Durati’on.between(start, end);
Duration中的toMillis()方法:将秒转成毫秒
System.out.println(between.toMillis());
Period类 中的静态方法between()
计算两个日期之间的间隔
LocalDate s = LocalDate.of(1985, 03, 05);
LocalDate now = LocalDate.now();
Period be = Period.between(s, now);
System.out.println(be.getYears());间隔了多少年
System.out.println(be.getMonths());间隔了多少月
System.out.println(be.getDays());间隔多少天
Instant now = Instant.now();
for (int i = 0; i < 50000; i++) {
System.out.println(i);
}
Instant end = Instant.now();
Duration between = Duration.between(now, end);
long l = between.toMillis();//将秒转成毫秒
System.out.println(l);
System.out.println("======================================");
LocalDate birthday = LocalDate.of(1998, 5, 28);
LocalDate now1 = LocalDate.now();
Period between1 = Period.between(birthday, now1);
int years = between1.getYears();
int months = between1.getMonths();
int days = between1.getDays();
System.out.println(years);
System.out.println(months);
System.out.println(days);
时间矫正器
一般我们用该接口的一个对应的工具类 TemporalAdjusters中的一些常量,来指定日期
LocalDate now = LocalDate.now();
System.out.println(now);
1 使用TemporalAdjusters自带的常量来设置日期
LocalDate with = now.with(TemporalAdjusters.lastDayOfYear());
System.out.println(with);
2 采用TemporalAdjusters中的next方法来指定日期
LocalDate date = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(date);
例如:TemporalAdjusters.next(DayOfWeek.SUNDAY) 本周的星期天
例如:TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY) 下一周的星期一
3 采用自定义的方式来指定日期 比如指定下个工作日
LocalDateTime ldt = LocalDateTime.now();
LocalDateTime workDay = ldt.with(new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
//向下转型
LocalDateTime ld = (LocalDateTime) temporal;
//获取这周的星期几
DayOfWeek dayOfWeek = ld.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return ld.plusDays(3);//如果这天是星期五,那下个工做日就加3天
} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
return ld.plusDays(2);//如果这天是星期六,那下个工做日就加2天
} else {
//其他就加一天
return ld.plusDays(1);
}
}
});
System.out.println(workDay);
LocalDate now = LocalDate.now();
TemporalAdjuster temporalAdjuster = TemporalAdjusters.lastDayOfMonth();
//获取下一个工作日是几号
LocalDate with = now.with(new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
//下个工作日怎么算?
//如果是星期五:下个工作日当前日期+3天
//如果是星期六:下个工作日当前日期+2天
//如果是星期天:下个工作日当前日期+1天
LocalDate now = (LocalDate) temporal;
DayOfWeek dayOfWeek = now.getDayOfWeek();
if (DayOfWeek.FRIDAY.equals(dayOfWeek)) {
LocalDate localDate = now.plusDays(3);
return localDate;
} else if (DayOfWeek.SATURDAY.equals(dayOfWeek)) {
LocalDate localDate = now.plusDays(2);
return localDate;
} else {
LocalDate localDate = now.plusDays(1);
return localDate;
}
}
});
System.out.println(with);
}
DateTimeFormatter :解析和格式化日期或时间的类
1.获取对象的方式,通过静态方法ofPattern("yyyy-MM-dd");
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.now();
2.format()方法把一个日期对象的默认格式 格式化成指定的格式
String format1 = dateFormat.format(now);
System.out.println(format1);
3.格式化日期 方式2使用日期类中的format方法 传入一个日期格式化类对象
LocalDateTime now1 = LocalDateTime.now();
使用日期类中的format方法 传入一个日期格式化类对象
使用DateTimeFormatter中提供好的日期格式常量
now1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
4.使用自定义的日期格式格式化字符串
DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//自定义一个日期格式
String time = now1.format(timeFormat);
System.out.println(time);
5. 把一个日期字符串转成日期对象
使用日期类中的parse方法传入一个日期字符串,传入对应的日期格式化类
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime parse = LocalDateTime.parse(time, timeFormat);
System.out.println(parse);
ZoneID世界时区类
- 获取世界各个地方时区的集合的方法getAvailableZoneIds() ;使用ZoneID中的静态方法getAvailableZoneIds();来获取例如:SetavailableZoneIds = ZoneId.getAvailableZoneIds();
- 获取系统默认时区的ID ZoneId zoneId = ZoneId.systemDefault(); //Asia/Shanghai
- 获取带有时区的日期时间对象
//创建日期对象
LocalDateTime now = LocalDateTime.now();
//获取不同国家的日期时间根据各个地区的时区ID名创建对象
ZoneId timeID = ZoneId.of(“Asia/Shanghai”);
//根据时区ID获取带有时区的日期时间对象
ZonedDateTime time = now.atZone(timeID);
System.out.println(time);
//方式2 通过时区ID 获取日期对象
LocalDateTime now2 = LocalDateTime.now(ZoneId.of(“Asia/Shanghai”));
System.out.println(now2);