文章目录
JDK8新特性笔记(一):Default,base64,LocalDate
1.Default
jdk1.8以前接口里只能写抽象方法;1.8以后引入了default关键字,使用default修饰方法就可以在接口里写实现方法。
//接口
public interface Animal {
void run();
void eat();
default void breath(){
System.out.println("呼吸氧气");
}
}
//实现类
public class Dog implements Animal {
@Override
public void run() {
System.out.println("小狗在跑");
}
@Override
public void eat() {
System.out.println("小狗在啃骨头");
}
}
//主函数
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.run();
dog.breath();
}
}
运行结果:
接口里写静态方法:
//接口
public interface Animal {
void run();
void eat();
default void breath(){
System.out.println("呼吸氧气");
}
static void test(){
System.out.println("接口中写静态方法");
System.out.println("小狗静静站着");
}
}
//主函数
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.run();
dog.breath();
Animal.test();
}
运行结果:
2.新增base64 api
jdk8的java.util包中新增了Base64的类,不需要引包,编解码效率大于sun.misc和ApacheCommonsCodec
public static void main(String[] args) throws UnsupportedEncodingException {
Base64.Encoder encoder = Base64.getEncoder();
Base64.Decoder decoder = Base64.getDecoder();
String text = "Base64 测试";
//转成字节数组
byte[] textBytes = text.getBytes("UTF-8");
//编码
String encodeString = encoder.encodeToString(textBytes);
System.out.println("编码后的结果:"+encodeString);
//解码
System.out.println("解码后的结果:"+new String(decoder.decode(encodeString),"UTF-8"));
}
3.jdk8的处理时间api
旧:以前版本java.util.Date是非线程安全的 API设计比较差,日期/时间对象比较,加减麻烦
新:使日期的比较格式化都变得简洁了
LocalDate:不包含具体时间的日期。
LocalTime:不包含日期的时间。
LocalDateTime:包含了日期及时间。
部分api例子:
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println("今天日期:"+localDate);
// 获取年月日星期等
System.out.println("现在是哪年:"+localDate.getYear());
System.out.println("现在是哪月:"+localDate.getMonth());
System.out.println("现在是哪月(数字):"+localDate.getMonthValue());
System.out.println("现在是几号:"+localDate.getDayOfMonth());
System.out.println("现在是周几:"+localDate.getDayOfWeek());
// 日期计算
LocalDate plusYears = localDate.plusYears(2);
System.out.println("加2后年份:"+plusYears.getYear());
System.out.println("isAfter:"+plusYears.isAfter(localDate));
}
运行结果:
localDate常用API:
方法 | 返回值类型 | 说明 |
---|---|---|
getYear() | int | 获取当前日期的年份 |
getMonth() | Month | 获取当前日期的月份对象 |
getMonthValue() | int | 获取当前日期是第几月 |
getDayOfWeek() | DayOfWeek | 表示该对象表示的日期是星期几 |
getDayOfMonth() | int | 表示该对象表示的日期是这个月第几天 |
getDayOfYear() | int | 表示该对象表示的日期是今年第几天 |
withYear(int year) | LocalDate | 修改当前对象的年份 |
withMonth(int month) | LocalDate | 修改当前对象的月份 |
withDayOfMonth(int dayOfMonth) | LocalDate | 修改当前对象在当月的日期 |
plusYears(long yearsToAdd) | LocalDate | 当前对象增加指定的年份数 |
plusMonths(long monthsToAdd) | LocalDate | 当前对象增加指定的月份数 |
plusWeeks(long weeksToAdd) | LocalDate | 当前对象增加指定的周数 |
plusDays(long daysToAdd) | LocalDate | 当前对象增加指定的天数 |
minusYears(long yearsToSubtract) | LocalDate | 当前对象减去指定的年数 |
minusMonths(long monthsToSubtract) | LocalDate | 当前对象减去注定的月数 |
minusWeeks(long weeksToSubtract) | LocalDate | 当前对象减去指定的周数 |
minusDays(long daysToSubtract) | LocalDate | 当前对象减去指定的天数 |
compareTo(ChronoLocalDate other) | int | 比较当前对象和other对象在时间上的大小,返回值如果为正,则当前对象时间较晚 |
isBefore(ChronoLocalDate other) | boolean | 比较当前对象日期是否在other对象日期之前 |
isAfter(ChronoLocalDate other) | boolean | 比较当前对象日期是否在other对象日期之后 |
isEqual(ChronoLocalDate other) | boolean | 比较两个日期对象是否相等 |
日期时间格式化(DateTimeFormat):
1.日期时间格式化
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("处理前:"+now);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = now.format(dateTimeFormatter);
System.out.println("处理后:"+format);
}
结果:
处理前:2020-09-18T10:13:39.838
处理后:2020-09-18 10:13:39
2.设置时间
使用LocalDateTime.of()实现
public static void main(String[] args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime setTime = LocalDateTime.of(2020, 4, 1, 8, 42, 21);
System.out.println("设置的时间:"+setTime);
String format1 = setTime.format(dateTimeFormatter);
System.out.println("设置的时间处理后:"+format1);
}
运行结果:
设置的时间:2020-04-01T08:42:21
设置的时间处理后:2020-04-01 08:42:21
3.时间计算
使用Duration.between(time1,time2)实现 这个的结果是Duration类型的time2-time1的值
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("处理前:"+now);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = now.format(dateTimeFormatter);
System.out.println("处理后:"+format);
LocalDateTime setTime = LocalDateTime.of(2020, 4, 1, 8, 42, 21);
System.out.println("设置的时间:"+setTime);
String format1 = setTime.format(dateTimeFormatter);
System.out.println("设置的时间处理后:"+format1);
Duration duration = Duration.between( setTime,now);//第二个参数减第一个参数
System.out.println(duration.toDays());//两个时间差的天数
System.out.println(duration.toHours());//两个时间差的⼩小时数
System.out.println(duration.toMinutes());//两个时间差的分钟数
System.out.println(duration.toMillis());//两个时间差的毫秒数
System.out.println(duration.toNanos());//两个时间差的纳秒数
}
运行结果
处理前:2020-09-18T11:12:51.108
处理后:2020-09-18 11:12:51
设置的时间:2020-04-01T08:42:21
设置的时间处理后:2020-04-01 08:42:21
170
4082
244950
14697030108
14697030108000000
这里可以看出获取纳秒时间差其实精度并没有到纳秒,只是毫秒级,这里应该只是省去了我们自行把毫秒转换成纳秒的步骤吧。