最后
每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。
如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份限量版的Java零基础宝典能够对你有所帮助。
Instant now = Instant.now();
System.out.println(“now:”+now);
System.out.println(now.getEpochSecond()); // 秒
System.out.println(now.toEpochMilli()); // 毫秒
Instant是没有时区的,但是Instant加上时区后,可以转化为ZonedDateTime
Instant ins = Instant.now();
ZonedDateTime zdt = ins.atZone(ZoneId.systemDefault());
System.out.println(zdt);
long型时间戳转Instant
要注意long型时间戳的时间单位选择Instant对应的方法转化
//1626796436 为秒级时间戳
Instant ins = Instant.ofEpochSecond(1626796436);
ZonedDateTime zdt = ins.atZone(ZoneId.systemDefault());
System.out.println(“秒级时间戳转化:”+zdt);
//1626796436111l 为秒级时间戳
Instant ins1 = Instant.ofEpochMilli(1626796436111l);
ZonedDateTime zdt1 = ins1.atZone(ZoneId.systemDefault());
System.out.println(“毫秒级时间戳转化:”+zdt1);
Instant.now()获取的时间与北京时间相差8个时区,这是一个细节,要避坑。
看源码,用的是UTC时间。
public static Instant now() {
return Clock.systemUTC().instant();
}
解决方案:
Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(“now:”+now);
=============================================================================
LocalDate表示本地日期。只有表示年月日。相当于:yyyy-MM-dd。
获取当前日期
LocalDate localDate1 = LocalDate.now();
LocalDate localDate2 = LocalDate.now(ZoneId.of(“Asia/Shanghai”));
LocalDate localDate3 = LocalDate.now(Clock.systemUTC());
System.out.println(“now :”+localDate1);
System.out.println(“now by zone :”+localDate2);
System.out.println(“now by Clock:”+localDate3);
获取localDate对象
LocalDate localDate1 = LocalDate.of(2021, 8, 14);
LocalDate localDate2 = LocalDate.parse(“2021-08-14”);
System.out.println(localDate1);
System.out.println(localDate2);
获取指定日期的年月日
LocalDate localDate1 = LocalDate.of(2021, 8, 14);
// 当前日期年份:2021
System.out.println(localDate1.getYear());
// 当前日期月份对象:AUGUST
System.out.println(localDate1.getMonth());
// 当前日期月份:8
System.out.println(localDate1.getMonthValue());
// 该日期是当前周的第几天:6
System.out.println(localDate1.getDayOfWeek().getValue());
// 该日期是当前月的第几天:14
System.out.println(localDate1.getDayOfMonth());
// 该日期是当前年的第几天:226
System.out.println(localDate1.getDayOfYear());
修改年月日
LocalDate localDate1 = LocalDate.of(2021, 8, 14);
// 修改该日期的年份:2022-08-14
System.out.println(localDate1.withYear(2022));
// 修改该日期的月份:2021-12-14
System.out.println(localDate1.withMonth(12));
// 修改该日期在当月的天数:2021-08-01
System.out.println(localDate1.withDayOfMonth(1));
比较日期
LocalDate localDate1 = LocalDate.of(2021, 8, 14);
// 比较指定日期和参数日期,返回正数,那么指定日期时间较晚(数字较大):13
int i = localDate1.compareTo(LocalDate.of(2021, 8, 1));
System.out.println(i);
// 比较指定日期是否比参数日期早(true为早):true
System.out.println(localDate1.isBefore(LocalDate.of(2021,8,31)));
// 比较指定日期是否比参数日期晚(true为晚):false
System.out.println(localDate1.isAfter(LocalDate.of(2021,8,31)));
// 比较两个日期是否相等:true
System.out.println(localDate1.isEqual(LocalDate.of(2021, 8, 14)));
LocalDate 和String相互转化、Date和LocalDate相互转化
LocalDate 和String相互转化
LocalDate localDate1 = LocalDate.of(2021, 8, 14);
// LocalDate 转 String
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd”);
String dateString = localDate1.format(dateTimeFormatter);
System.out.println(“LocalDate 转 String:”+dateString);
// String 转 LocalDate
String str = “2021-08-14”;
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(“yyyy-MM-dd”);
LocalDate date = LocalDate.parse(str, fmt);
System.out.println(“String 转 LocalDate:”+date);
Date和LocalDate相互转化
// Date 转 LocalDate
Date now = new Date();
// 先将Date转换为ZonedDateTime
Instant instant = now.toInstant();
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of(“Asia/Shanghai”));
LocalDate localDate = zonedDateTime.toLocalDate();
// Sat Aug 14 23:16:28 CST 2021
System.out.println(now);
// 2021-08-14
System.out.println(localDate);
// LocalDate 转 Date
LocalDate now1 = LocalDate.now();
ZonedDateTime dateTime = now1.atStartOfDay(ZoneId.of(“Asia/Shanghai”));
Date date1 = Date.from(dateTime.toInstant());
System.out.println(date1);
=================================================================================
表示当前日期时间,相当于:yyyy-MM-ddTHH:mm:ss
获取当前日期和时间
LocalDate d = LocalDate.now(); // 当前日期
LocalTime t = LocalTime.now(); // 当前时间
LocalDateTime dt = LocalDateTime.now(); // 当前日期和时间
System.out.println(d); // 严格按照ISO 8601格式打印
System.out.println(t); // 严格按照ISO 8601格式打印
System.out.println(dt); // 严格按照ISO 8601格式打印
由运行结果可行,本地日期时间通过now()获取到的总是以当前默认时区返回的
获取指定日期和时间
LocalDate d2 = LocalDate.of(2021, 07, 14); // 2021-07-14, 注意07=07月
LocalTime t2 = LocalTime.of(13, 14, 20); // 13:14:20
LocalDateTime dt2 = LocalDateTime.of(2021, 07, 14, 13, 14, 20);
LocalDateTime dt3 = LocalDateTime.of(d2, t2);
System.out.println(“指定日期时间:”+dt2);
System.out.println(“指定日期时间:”+dt3);
日期时间的加减法及修改
LocalDateTime currentTime = LocalDateTime.now(); // 当前日期和时间
System.out.println(“------------------时间的加减法及修改-----------------------”);
//3.LocalDateTime的加减法包含了LocalDate和LocalTime的所有加减,上面说过,这里就只做简单介绍
System.out.println(“3.当前时间:” + currentTime);
System.out.println(“3.当前时间加5年:” + currentTime.plusYears(5));
System.out.println(“3.当前时间加2个月:” + currentTime.plusMonths(2));
System.out.println(“3.当前时间减2天:” + currentTime.minusDays(2));
System.out.println(“3.当前时间减5个小时:” + currentTime.minusHours(5));
System.out.println(“3.当前时间加5分钟:” + currentTime.plusMinutes(5));
System.out.println(“3.当前时间加20秒:” + currentTime.plusSeconds(20));
//还可以灵活运用比如:向后加一年,向前减一天,向后加2个小时,向前减5分钟,可以进行连写
System.out.println(“3.同时修改(向后加一年,向前减一天,向后加2个小时,向前减5分钟):” + currentTime.plusYears(1).minusDays(1).plusHours(2).minusMinutes(5));
System.out.println(“3.修改年为2025年:” + currentTime.withYear(2025));
System.out.println(“3.修改月为12月:” + currentTime.withMonth(12));
System.out.println(“3.修改日为27日:” + currentTime.withDayOfMonth(27));
System.out.println(“3.修改小时为12:” + currentTime.withHour(12));
System.out.println(“3.修改分钟为12:” + currentTime.withMinute(12));
System.out.println(“3.修改秒为12:” + currentTime.withSecond(12));
LocalDateTime和Date相互转化
Date转LocalDateTime
System.out.println(“------------------方法一:分步写-----------------------”);
//实例化一个时间对象
Date date = new Date();
//返回表示时间轴上同一点的瞬间作为日期对象
Instant instant = date.toInstant();
//获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
//根据时区获取带时区的日期和时间
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
//转化为LocalDateTime
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.println("方法一:原Date = " + date);
System.out.println("方法一:转化后的LocalDateTime = " + localDateTime);
System.out.println(“------------------方法二:一步到位(推荐使用)-----------------------”);
//实例化一个时间对象
Date todayDate = new Date();
//Instant.ofEpochMilli(long l)使用1970-01-01T00:00:00Z的纪元中的毫秒来获取Instant的实例
LocalDateTime ldt = Instant.ofEpochMilli(todayDate.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("方法二:原Date = " + todayDate);
System.out.println("方法二:转化后的LocalDateTime = " + ldt);
LocalDateTime转Date
System.out.println(“------------------方法一:分步写-----------------------”);
//获取LocalDateTime对象,当前时间
LocalDateTime localDateTime = LocalDateTime.now();
//获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
//根据时区获取带时区的日期和时间
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
//返回表示时间轴上同一点的瞬间作为日期对象
Instant instant = zonedDateTime.toInstant();
//转化为Date
Date date = Date.from(instant);
System.out.println("方法一:原LocalDateTime = " + localDateTime);
System.out.println("方法一:转化后的Date = " + date);
System.out.println(“------------------方法二:一步到位(推荐使用)-----------------------”);
//实例化一个LocalDateTime对象
LocalDateTime now = LocalDateTime.now();
//转化为date
Date dateResult = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("方法二:原LocalDateTime = " + now);
System.out.println("方法二:转化后的Date = " + dateResult);
=============================================================================
LocalTime:本地时间,只有表示时分秒
获取当前时间
LocalTime localTime1 = LocalTime.now();
LocalTime localTime2 = LocalTime.now(ZoneId.of(“Asia/Shanghai”));
LocalTime localTime3 = LocalTime.now(Clock.systemDefaultZone());
System.out.println(“now :”+localTime1);
System.out.println(“now by zone :”+localTime2);
System.out.println(“now by Clock:”+localTime3);
获取LocalTime对象
LocalTime localTime1 = LocalTime.of(23, 26, 30);
LocalTime localTime2 = LocalTime.of(23, 26);
System.out.println(localTime1);
System.out.println(localTime2);
获取指定日期的时分秒
LocalTime localTime1 = LocalTime.of(23, 26, 30);
//当前时间的时:23
System.out.println(localTime1.getHour());
//当前时间的分:26
System.out.println(localTime1.getMinute());
//当前时间的秒:30
System.out.println(localTime1.getSecond());
修改时分秒
LocalTime localTime1 = LocalTime.of(23, 26, 30);
//修改时间的时:00:26:30
System.out.println(localTime1.withHour(0));
//修改时间的分:23:30:30
System.out.println(localTime1.withMinute(30));
//修改时间的秒:23:26:59
System.out.println(localTime1.withSecond(59));
比较时间
LocalTime localTime1 = LocalTime.of(23, 26, 30);
LocalTime localTime2 = LocalTime.of(23, 26, 32);
// 两个时间进行比较 大返回1,小就返回-1,一样就返回0:-1
System.out.println(localTime1.compareTo(localTime2));
// 比较指定时间是否比参数时间早(true为早):true
System.out.println(localTime1.isBefore(localTime2));
// 比较指定时间是否比参数时间晚(true为晚):false
System.out.println(localTime1.isAfter(localTime2));
// 比较两个时间是否相等:true
System.out.println(localTime1.equals(LocalTime.of(23, 26, 30)));
==================================================================================
OffsetDateTime:有时间偏移量的日期时间(不包含基于ZoneRegion的时间偏移量)
public final class OffsetDateTime
implements Temporal, TemporalAdjuster, Comparable, Serializable {
//The minimum supported {@code OffsetDateTime}, ‘-999999999-01-01T00:00:00+18:00’
public static final OffsetDateTime MIN = LocalDateTime.MIN.atOffset(ZoneOffset.MAX);
// The maximum supported {@code OffsetDateTime}, ‘+999999999-12-31T23:59:59.999999999-18:00’.
public static final OffsetDateTime MAX = LocalDateTime.MAX.atOffset(ZoneOffset.MIN);
…
}
上面的MIN 和MAX 是公有静态变量。
获取当前日期时间
OffsetDateTime offsetDateTime1 = OffsetDateTime.now();
OffsetDateTime offsetDateTime2 = OffsetDateTime.now(ZoneId.of(“Asia/Shanghai”));
OffsetDateTime offsetDateTime3 = OffsetDateTime.now(Clock.systemUTC());
System.out.println(“now :”+offsetDateTime1);
System.out.println(“now by zone :”+offsetDateTime2);
System.out.println(“now by Clock:”+offsetDateTime3);
获取OffsetDateTime对象
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 8, 15, 13, 14, 20);
OffsetDateTime offsetDateTime1 = OffsetDateTime.of(localDateTime1, ZoneOffset.ofHours(8));
OffsetDateTime offsetDateTime2 = OffsetDateTime. of(2021, 8, 15, 13, 14, 20,0, ZoneOffset.ofHours(8));
Instant now = Instant.now();
OffsetDateTime offsetDateTime3 = OffsetDateTime.ofInstant(now, ZoneId.of(“Asia/Shanghai”));
System.out.println(offsetDateTime1);
System.out.println(offsetDateTime2);
System.out.println(offsetDateTime3);
获取指定日期的年月日时分秒
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 8, 15, 13, 14, 20);
OffsetDateTime offsetDateTime1 = OffsetDateTime.of(localDateTime1, ZoneOffset.ofHours(8));
//当前时间的年:2021
System.out.println(offsetDateTime1.getYear());
//当前时间的月:8
System.out.println(offsetDateTime1.getMonthValue());
//当前时间的日:15
System.out.println(offsetDateTime1.getDayOfMonth());
//当前时间的时:13
System.out.println(offsetDateTime1.getHour());
//当前时间的分:14
System.out.println(offsetDateTime1.getMinute());
//当前时间的秒:20
System.out.println(offsetDateTime1.getSecond());
修改年月日时分秒
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 8, 15, 13, 14, 20);
OffsetDateTime offsetDateTime1 = OffsetDateTime.of(localDateTime1, ZoneOffset.ofHours(8));
//修改时间的年:2022-08-15T13:14:20+08:00
System.out.println(offsetDateTime1.withYear(2022));
//修改时间的月:2021-09-15T13:14:20+08:00
System.out.println(offsetDateTime1.withMonth(9));
//修改时间的日:2021-08-30T13:14:20+08:00
System.out.println(offsetDateTime1.withDayOfMonth(30));
//修改时间的时:2021-08-15T00:14:20+08:00
System.out.println(offsetDateTime1.withHour(0));
//修改时间的分:2021-08-15T13:30:20+08:00
System.out.println(offsetDateTime1.withMinute(30));
//修改时间的秒:2021-08-15T13:14:59+08:00
System.out.println(offsetDateTime1.withSecond(59));
比较日期时间
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 8, 15, 13, 14, 20);
OffsetDateTime offsetDateTime1 = OffsetDateTime.of(localDateTime1, ZoneOffset.ofHours(8));
OffsetDateTime offsetDateTime3 = OffsetDateTime.of(localDateTime1, ZoneOffset.ofHours(8));
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 8, 15, 13, 14, 30);
OffsetDateTime offsetDateTime2 = OffsetDateTime.of(localDateTime2, ZoneOffset.ofHours(8));
// 两个时间进行比较 大返回1,小就返回-1,一样就返回0:-1
System.out.println(offsetDateTime1.compareTo(offsetDateTime2));
// 比较指定时间是否比参数时间早(true为早):true
System.out.println(offsetDateTime1.isBefore(offsetDateTime2));
// 比较指定时间是否比参数时间晚(true为晚):false
System.out.println(offsetDateTime1.isAfter(offsetDateTime2));
// 比较两个时间是否相等:true
System.out.println(offsetDateTime1.equals(offsetDateTime3));
字符串转化为OffsetDateTime对象
String str = “2021-08-15T10:15:30+08:00”;
OffsetDateTime offsetDateTime1 = OffsetDateTime.parse(str);
OffsetDateTime offsetDateTime2 = OffsetDateTime.parse(str,DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(offsetDateTime1);
System.out.println(offsetDateTime2);
==============================================================================
OffsetTime:有时间偏移量的时间
public final class OffsetTime
implements Temporal, TemporalAdjuster, Comparable, Serializable {
//The minimum supported {@code OffsetTime}, ‘00:00:00+18:00’.
public static final OffsetTime MIN = LocalTime.MIN.atOffset(ZoneOffset.MAX);
//The maximum supported {@code OffsetTime}, ‘23:59:59.999999999-18:00’.
public static final OffsetTime MAX = LocalTime.MAX.atOffset(ZoneOffset.MIN);
…
}
上面的MIN 和MAX 是公有静态变量。
获取当前时间
OffsetTime offsetTime1 = OffsetTime.now();
OffsetTime offsetTime2 = OffsetTime.now(ZoneId.of(“Asia/Shanghai”));
OffsetTime offsetTime3 = OffsetTime.now(Clock.systemUTC());
System.out.println(“now :”+offsetTime1);
System.out.println(“now by zone :”+offsetTime2);
System.out.println(“now by Clock:”+offsetTime3);
获取OffsetTime对象
LocalTime localTime1 = LocalTime.of(13, 14, 20);
OffsetTime offsetTime1 = OffsetTime.of(localTime1, ZoneOffset.ofHours(8));
OffsetTime offsetTime2 = OffsetTime. of(13, 14, 20,0, ZoneOffset.ofHours(8));
Instant now = Instant.now();
OffsetTime offsetTime3 = OffsetTime.ofInstant(now, ZoneId.of(“Asia/Shanghai”));
System.out.println(offsetTime1);
System.out.println(offsetTime2);
System.out.println(offsetTime3);
获取指定时间的时分秒
LocalTime localTime1 = LocalTime.of( 13, 14, 20);
OffsetTime offsetTime1 = OffsetTime.of(localTime1, ZoneOffset.ofHours(8));
//当前时间的时:13
System.out.println(offsetTime1.getHour());
//当前时间的分:14
System.out.println(offsetTime1.getMinute());
//当前时间的秒:20
System.out.println(offsetTime1.getSecond());
修改时分秒
LocalTime localTime1 = LocalTime.of( 13, 14, 20);
OffsetTime offsetTime1 = OffsetTime.of(localTime1, ZoneOffset.ofHours(8));
//修改时间的时:00:14:20+08:00
System.out.println(offsetTime1.withHour(0));
//修改时间的分:13:30:20+08:00
System.out.println(offsetTime1.withMinute(30));
//修改时间的秒:13:14:59+08:00
System.out.println(offsetTime1.withSecond(59));
比较时间
LocalTime localTime1 = LocalTime.of( 13, 14, 20);
OffsetTime offsetTime1 = OffsetTime.of(localTime1, ZoneOffset.ofHours(8));
OffsetTime offsetTime3 = OffsetTime.of(localTime1, ZoneOffset.ofHours(8));
LocalTime localTime2 = LocalTime.of(13, 14, 30);
OffsetTime offsetTime2 = OffsetTime.of(localTime2, ZoneOffset.ofHours(8));
// 两个时间进行比较 大返回1,小就返回-1,一样就返回0:-1
System.out.println(offsetTime1.compareTo(offsetTime2));
// 比较指定时间是否比参数时间早(true为早):true
System.out.println(offsetTime1.isBefore(offsetTime2));
// 比较指定时间是否比参数时间晚(true为晚):false
System.out.println(offsetTime1.isAfter(offsetTime2));
// 比较两个时间是否相等:true
System.out.println(offsetTime1.equals(offsetTime3));
=================================================================================
表示一个带时区的日期和时间,ZonedDateTime可以理解为LocalDateTime+ZoneId
从源码可以看出来,ZonedDateTime类中定义了LocalDateTime和ZoneId两个变量。
且ZonedDateTime类也是不可变类且是线程安全的。
public final class ZonedDateTime
implements Temporal, ChronoZonedDateTime, Serializable {
/**
- Serialization version.
*/
private static final long serialVersionUID = -6260982410461394882L;
/**
- The local date-time.
*/
private final LocalDateTime dateTime;
/**
- The time-zone.
*/
private final ZoneId zone;
…
}
获取当前日期时间
// 默认时区获取当前时间
ZonedDateTime zonedDateTime = ZonedDateTime.now();
// 用指定时区获取当前时间,Asia/Shanghai为上海时区
ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of(“Asia/Shanghai”));
//withZoneSameInstant为转换时区,参数为ZoneId
ZonedDateTime zonedDateTime2 = zonedDateTime.withZoneSameInstant(ZoneId.of(“America/New_York”));
System.out.println(zonedDateTime);
System.out.println(zonedDateTime1);
System.out.println(zonedDateTime2);
ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
ZonedDateTime zonedDateTime2 = ZonedDateTime.now(ZoneId.of(“Asia/Shanghai”));
ZonedDateTime zonedDateTime3 = ZonedDateTime.now(Clock.systemUTC());
System.out.println(“now :”+zonedDateTime1);
System.out.println(“now by zone :”+zonedDateTime2);
System.out.println(“now by Clock:”+zonedDateTime3);
获取ZonedDateTime对象
最后
我还通过一些渠道整理了一些大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。
还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。
ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of(“Asia/Shanghai”));
//withZoneSameInstant为转换时区,参数为ZoneId
ZonedDateTime zonedDateTime2 = zonedDateTime.withZoneSameInstant(ZoneId.of(“America/New_York”));
System.out.println(zonedDateTime);
System.out.println(zonedDateTime1);
System.out.println(zonedDateTime2);
ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
ZonedDateTime zonedDateTime2 = ZonedDateTime.now(ZoneId.of(“Asia/Shanghai”));
ZonedDateTime zonedDateTime3 = ZonedDateTime.now(Clock.systemUTC());
System.out.println(“now :”+zonedDateTime1);
System.out.println(“now by zone :”+zonedDateTime2);
System.out.println(“now by Clock:”+zonedDateTime3);
获取ZonedDateTime对象
最后
我还通过一些渠道整理了一些大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。
[外链图片转存中…(img-XtSLPnHZ-1715721259161)]
还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。
[外链图片转存中…(img-zijSDQpZ-1715721259162)]