当前位置: 首页 > news >正文

安徽搜索引擎优化网站seo好学吗

安徽搜索引擎优化,网站seo好学吗,你懂的我的意思正能量句子,阳西网络问政平台官网JDK1.8 新特性 本文主要介绍了JDK1.8版本中的一些新特性#xff0c;乃作者视频观后笔记#xff0c;仅供参考。 jdk1.8新特性知识点#xff1a; Lambda表达式函数式接口方法引用和构造器调用Stream API接口中的默认方法和静态方法新时间日期API 在jdk1.8中对hashMap等map集…JDK1.8 新特性 本文主要介绍了JDK1.8版本中的一些新特性乃作者视频观后笔记仅供参考。 jdk1.8新特性知识点 Lambda表达式函数式接口方法引用和构造器调用Stream API接口中的默认方法和静态方法新时间日期API 在jdk1.8中对hashMap等map集合的数据结构优化。hashMap数据结构的优化 原来的hashMap采用的数据结构是哈希表数组链表hashMap默认大小是16一个0-15索引的数组如何往里面存储元素首先调用元素的hashcode 方法计算出哈希码值经过哈希算法算成数组的索引值如果对应的索引处没有元素直接存放如果有对象在那么比较它们的equals方法比较内容 如果内容一样后一个value会将前一个value的值覆盖如果不一样在1.7的时候后加的放在前面形成一个链表形成了碰撞在某些情况下如果链表 无限下去那么效率极低碰撞是避免不了的 加载因子0.75数组扩容达到总容量的75%就进行扩容但是无法避免碰撞的情况发生 在1.8之后在数组链表红黑树来实现hashmap当碰撞的元素个数大于8时 总容量大于64会有红黑树的引入 除了添加之后效率都比链表高1.8之后链表新进元素加到末尾 ConcurrentHashMap (锁分段机制)concurrentLevel,jdk1.8采用CAS算法(无锁算法不再使用锁分段)数组链表中也引入了红黑树的使用 Lambda表达式 lambda表达式本质上是一段匿名内部类也可以是一段可以传递的代码 先来体验一下lambda最直观的优点简洁代码 //匿名内部类ComparatorInteger cpt new ComparatorInteger() {Overridepublic int compare(Integer o1, Integer o2) {return Integer.compare(o1,o2);}};TreeSetInteger set new TreeSet(cpt);System.out.println();//使用lambda表达式ComparatorInteger cpt2 (x,y) - Integer.compare(x,y);TreeSetInteger set2 new TreeSet(cpt2);123456789101112131415只需要一行代码极大减少代码量 这样一个场景在商城浏览商品信息时经常会有条件的进行筛选浏览例如要选颜色为红色的、价格小于8000千的…. // 筛选颜色为红色 public ListProduct filterProductByColor(ListProduct list){ListProduct prods new ArrayList();for (Product product : list){if (红色.equals(product.getColor())){prods.add(product);}}return prods;}// 筛选价格小于8千的 public ListProduct filterProductByPrice(ListProduct list){ListProduct prods new ArrayList();for (Product product : list){if (product.getPrice() 8000){prods.add(product);}}return prods;}123456789101112131415161718192021我们发现实际上这些过滤方法的核心就只有if语句中的条件判断其他均为模版代码每次变更一下需求都需要新增一个方法然后复制黏贴假设这个过滤方法有几百行那么这样的做法难免笨拙了一点。如何进行优化呢 优化一使用设计模式 定义一个MyPredicate接口 public interface MyPredicate T {boolean test(T t); }123如果想要筛选颜色为红色的商品定义一个颜色过滤类 public class ColorPredicate implements MyPredicate Product {private static final String RED 红色;Overridepublic boolean test(Product product) {return RED.equals(product.getColor());}12345678定义过滤方法将过滤接口当做参数传入这样这个过滤方法就不用修改在实际调用的时候将具体的实现类传入即可。 public ListProduct filterProductByPredicate(ListProduct list,MyPredicateProduct mp){ListProduct prods new ArrayList();for (Product prod : list){if (mp.test(prod)){prods.add(prod);}}return prods;}123456789例如如果想要筛选价格小于8000的商品那么新建一个价格过滤类既可 public class PricePredicate implements MyPredicateProduct {Overridepublic boolean test(Product product) {return product.getPrice() 8000;} }123456这样实现的话可能有人会说每次变更需求都需要新建一个实现类感觉还是有点繁琐呀那么再来优化一下 优化二使用匿名内部类 定义过滤方法 public ListProduct filterProductByPredicate(ListProduct list,MyPredicateProduct mp){ListProduct prods new ArrayList();for (Product prod : list){if (mp.test(prod)){prods.add(prod);}}return prods;}123456789调用过滤方法的时候 // 按价格过滤 public void test2(){filterProductByPredicate(proList, new MyPredicateProduct() {Overridepublic boolean test(Product product) {return product.getPrice() 8000;}}); }// 按颜色过滤public void test3(){filterProductByPredicate(proList, new MyPredicateProduct() {Overridepublic boolean test(Product product) {return 红色.equals(product.getColor());}});}12345678910111213141516171819使用匿名内部类就不需要每次都新建一个实现类直接在方法内部实现。看到匿名内部类不禁想起了Lambda表达式。 优化三使用lambda表达式 定义过滤方法 public ListProduct filterProductByPredicate(ListProduct list,MyPredicateProduct mp){ListProduct prods new ArrayList();for (Product prod : list){if (mp.test(prod)){prods.add(prod);}}return prods;}123456789使用lambda表达式进行过滤 Test public void test4(){ListProduct products filterProductByPredicate(proList, (p) - p.getPrice() 8000);for (Product pro : products){System.out.println(pro);}}1234567在jdk1.8中还有更加简便的操作 Stream API 优化四使用Stream API 甚至不用定义过滤方法直接在集合上进行操作 // 使用jdk1.8中的Stream API进行集合的操作 Test public void test(){// 根据价格过滤proList.stream().fliter((p) - p.getPrice() 8000).limit(2).forEach(System.out::println);// 根据颜色过滤proList.stream().fliter((p) - 红色.equals(p.getColor())).forEach(System.out::println);// 遍历输出商品名称proList.stream().map(Product::getName).forEach(System.out::println); }12345678910111213141516171819Lmabda表达式的语法总结 () - (); 前置语法无参数无返回值() - System.out.println(“Hello WOrld”)有一个参数无返回值(x) - System.out.println(x)有且只有一个参数无返回值x - System.out.println(x)有多个参数有返回值有多条lambda体语句(xy) - {System.out.println(“xxx”);return xxxx;}有多个参数有返回值只有一条lambda体语句(xy) - xxxx 口诀左右遇一省括号左侧推断类型省 注当一个接口中存在多个抽象方法时如果使用lambda表达式并不能智能匹配对应的抽象方法因此引入了函数式接口的概念 函数式接口 函数式接口的提出是为了给Lambda表达式的使用提供更好的支持。 什么是函数式接口 简单来说就是只定义了一个抽象方法的接口Object类的public方法除外就是函数式接口并且还提供了注解FunctionalInterface 常见的四大函数式接口 Consumer 《T》消费型接口有参无返回值 Testpublic void test(){changeStr(hello,(str) - System.out.println(str));}/*** ConsumerT 消费型接口* param str* param con*/public void changeStr(String str, ConsumerString con){con.accept(str);}12345678910111213Supplier 《T》供给型接口无参有返回值 Testpublic void test2(){String value getValue(() - hello);System.out.println(value);}/*** SupplierT 供给型接口* param sup* return*/public String getValue(SupplierString sup){return sup.get();}1234567891011121314Function 《T,R》:函数式接口有参有返回值 Testpublic void test3(){Long result changeNum(100L, (x) - x 200L);System.out.println(result);}/*** FunctionT,R 函数式接口* param num* param fun* return*/public Long changeNum(Long num, FunctionLong, Long fun){return fun.apply(num);}123456789101112131415Predicate《T》 断言型接口有参有返回值返回值是boolean类型 public void test4(){boolean result changeBoolean(hello, (str) - str.length() 5);System.out.println(result);}/*** PredicateT 断言型接口* param str* param pre* return*/public boolean changeBoolean(String str, PredicateString pre){return pre.test(str);}1234567891011121314 在四大核心函数式接口基础上还提供了诸如BiFunction、BinaryOperation、toIntFunction等扩展的函数式接口都是在这四种函数式接口上扩展而来的不做赘述。 总结函数式接口的提出是为了让我们更加方便的使用lambda表达式不需要自己再手动创建一个函数式接口直接拿来用就好了贴 方法引用 若lambda体中的内容有方法已经实现了那么可以使用“方法引用” 也可以理解为方法引用是lambda表达式的另外一种表现形式并且其语法比lambda表达式更加简单 (a) 方法引用 三种表现形式 \1. 对象实例方法名 \2. 类静态方法名 \3. 类实例方法名 lambda参数列表中第一个参数是实例方法的调用 者第二个参数是实例方法的参数时可用 public void test() {/***注意* 1.lambda体中调用方法的参数列表与返回值类型要与函数式接口中抽象方法的函数列表和返回值类型保持一致* 2.若lambda参数列表中的第一个参数是实例方法的调用者而第二个参数是实例方法的参数时可以使用ClassName::method**/ConsumerInteger con (x) - System.out.println(x);con.accept(100);// 方法引用-对象::实例方法ConsumerInteger con2 System.out::println;con2.accept(200);// 方法引用-类名::静态方法名BiFunctionInteger, Integer, Integer biFun (x, y) - Integer.compare(x, y);BiFunctionInteger, Integer, Integer biFun2 Integer::compare;Integer result biFun2.apply(100, 200);// 方法引用-类名::实例方法名BiFunctionString, String, Boolean fun1 (str1, str2) - str1.equals(str2);BiFunctionString, String, Boolean fun2 String::equals;Boolean result2 fun2.apply(hello, world);System.out.println(result2);}12345678910111213141516171819202122232425(b)构造器引用 格式ClassName::new public void test2() {// 构造方法引用 类名::newSupplierEmployee sup () - new Employee();System.out.println(sup.get());SupplierEmployee sup2 Employee::new;System.out.println(sup2.get());// 构造方法引用 类名::new 带一个参数FunctionInteger, Employee fun (x) - new Employee(x);FunctionInteger, Employee fun2 Employee::new;System.out.println(fun2.apply(100));}12345678910111213©数组引用 格式Type[]::new public void test(){// 数组引用FunctionInteger, String[] fun (x) - new String[x];FunctionInteger, String[] fun2 String[]::new;String[] strArray fun2.apply(10);Arrays.stream(strArray).forEach(System.out::println); }1234567Stream API Stream操作的三个步骤 创建stream中间操作过滤、map终止操作 stream的创建 // 1校验通过Collection 系列集合提供的stream()或者paralleStream()ListString list new ArrayList();StreanString stream1 list.stream();// 2.通过Arrays的静态方法stream()获取数组流String[] str new String[10];StreamString stream2 Arrays.stream(str);// 3.通过Stream类中的静态方法ofStreamString stream3 Stream.of(aa,bb,cc);// 4.创建无限流// 迭代StreamInteger stream4 Stream.iterate(0,(x) - x2);//生成Stream.generate(() -Math.random());1234567891011121314151617Stream的中间操作: /*** 筛选 过滤 去重*/emps.stream().filter(e - e.getAge() 10).limit(4).skip(4)// 需要流中的元素重写hashCode和equals方法.distinct().forEach(System.out::println);/*** 生成新的流 通过map映射*/emps.stream().map((e) - e.getAge()).forEach(System.out::println);/*** 自然排序 定制排序*/emps.stream().sorted((e1 ,e2) - {if (e1.getAge().equals(e2.getAge())){return e1.getName().compareTo(e2.getName());} else{return e1.getAge().compareTo(e2.getAge());}}).forEach(System.out::println); 123456789101112131415161718192021222324252627282930313233Stream的终止操作 /*** 查找和匹配* allMatch-检查是否匹配所有元素* anyMatch-检查是否至少匹配一个元素* noneMatch-检查是否没有匹配所有元素* findFirst-返回第一个元素* findAny-返回当前流中的任意元素* count-返回流中元素的总个数* max-返回流中最大值* min-返回流中最小值*//*** 检查是否匹配元素*/boolean b1 emps.stream().allMatch((e) - e.getStatus().equals(Employee.Status.BUSY));System.out.println(b1);boolean b2 emps.stream().anyMatch((e) - e.getStatus().equals(Employee.Status.BUSY));System.out.println(b2);boolean b3 emps.stream().noneMatch((e) - e.getStatus().equals(Employee.Status.BUSY));System.out.println(b3);OptionalEmployee opt emps.stream().findFirst();System.out.println(opt.get());// 并行流OptionalEmployee opt2 emps.parallelStream().findAny();System.out.println(opt2.get());long count emps.stream().count();System.out.println(count);OptionalEmployee max emps.stream().max((e1, e2) - Double.compare(e1.getSalary(), e2.getSalary()));System.out.println(max.get());OptionalEmployee min emps.stream().min((e1, e2) - Double.compare(e1.getSalary(), e2.getSalary()));System.out.println(min.get());1234567891011121314151617181920212223242526272829303132333435363738394041424344454647还有功能比较强大的两个终止操作 reduce和collect reduce操作 reduce:(T identity,BinaryOperator)/reduce(BinaryOperator)-可以将流中元素反复结合起来得到一个值 /*** reduce 规约操作*/ListInteger list Arrays.asList(1,2,3,4,5,6,7,8,9,10);Integer count2 list.stream().reduce(0, (x, y) - x y);System.out.println(count2);OptionalDouble sum emps.stream().map(Employee::getSalary).reduce(Double::sum);System.out.println(sum); 12345678910111213collect操作Collect-将流转换为其他形式接收一个Collection接口的实现用于给Stream中元素做汇总的方法 /*** collect收集操作*/ListInteger ageList emps.stream().map(Employee::getAge).collect(Collectors.toList());ageList.stream().forEach(System.out::println);12345678并行流和串行流 在jdk1.8新的stream包中针对集合的操作也提供了并行操作流和串行操作流。并行流就是把内容切割成多个数据块并且使用多个线程分别处理每个数据块的内容。Stream api中声明可以通过parallel()与sequential()方法在并行流和串行流之间进行切换。 jdk1.8并行流使用的是fork/join框架进行并行操作 ForkJoin框架 Fork/Join 框架就是在必要的情况下将一个大任务进行拆分(fork)成若干个小任务拆到不可再拆时再将一个个的小任务运算的结果进行 join 汇总。 关键字递归分合、分而治之。 采用 “工作窃取”模式work-stealing 当执行新的任务时它可以将其拆分分成更小的任务执行并将小任务加到线 程队列中然后再从一个随机线程的队列中偷一个并把它放在自己的队列中 相对于一般的线程池实现,fork/join框架的优势体现在对其中包含的任务的 处理方式上.在一般的线程池中,如果一个线程正在执行的任务由于某些原因 无法继续运行,那么该线程会处于等待状态.而在fork/join框架实现中,如果 某个子问题由于等待另外一个子问题的完成而无法继续运行.那么处理该子 问题的线程会主动寻找其他尚未运行的子问题来执行.这种方式减少了线程 的等待时间,提高了性能.。 /*** 要想使用Fark—Join类必须继承* RecursiveAction无返回值* Or* RecursiveTask有返回值 * */ public class ForkJoin extends RecursiveTaskLong {/*** 要想使用Fark—Join类必须继承RecursiveAction无返回值 或者* RecursiveTask有返回值** author Wuyouxin*/private static final long serialVersionUID 23423422L;private long start;private long end;public ForkJoin() {}public ForkJoin(long start, long end) {this.start start;this.end end;}// 定义阙值private static final long THRESHOLD 10000L;Overrideprotected Long compute() {if (end - start THRESHOLD) {long sum 0;for (long i start; i end; i) {sum i;}return sum;} else {long middle (end - start) / 2;ForkJoin left new ForkJoin(start, middle);//拆分子任务压入线程队列left.fork();ForkJoin right new ForkJoin(middle 1, end);right.fork();//合并并返回return left.join() right.join();}}/*** 实现数的累加*/Testpublic void test1() {//开始时间Instant start Instant.now();//这里需要一个线程池的支持ForkJoinPool pool new ForkJoinPool();ForkJoinTaskLong task new ForkJoin(0L, 10000000000L);// 没有返回值 pool.execute();// 有返回值long sum pool.invoke(task);//结束时间Instant end Instant.now();System.out.println(Duration.between(start, end).getSeconds());}/*** java8 并行流 parallel()*/Testpublic void test2() {//开始时间Instant start Instant.now();// 并行流计算 累加求和LongStream.rangeClosed(0, 10000000000L).parallel().reduce(0, Long :: sum);//结束时间Instant end Instant.now();System.out.println(Duration.between(start, end).getSeconds());}Testpublic void test3(){ListInteger list Arrays.asList(1, 2, 3, 4, 5);list.stream().forEach(System.out::print);list.parallelStream().forEach(System.out::print);}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798展示多线程的效果 Testpublic void test(){// 并行流 多个线程执行ListInteger numbers Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);numbers.parallelStream().forEach(System.out::print);//System.out.println();numbers.stream().sequential().forEach(System.out::print);}12345678910111213Optional容器 使用Optional容器可以快速的定位NPE并且在一定程度上可以减少对参数非空检验的代码量。 1 /*** Optional.of(T t); // 创建一个Optional实例* Optional.empty(); // 创建一个空的Optional实例* Optional.ofNullable(T t); // 若T不为null创建一个Optional实例否则创建一个空实例* isPresent(); // 判断是够包含值* orElse(T t); //如果调用对象包含值返回该值否则返回T* orElseGet(Supplier s); // 如果调用对象包含值返回该值否则返回s中获取的值* map(Function f): // 如果有值对其处理并返回处理后的Optional否则返回Optional.empty();* flatMap(Function mapper);// 与map类似。返回值是Optional** 总结Optional.of(null) 会直接报NPE*/OptionalEmployee op Optional.of(new Employee(zhansan, 11, 12.32, Employee.Status.BUSY));System.out.println(op.get());// NPEOptionalEmployee op2 Optional.of(null);System.out.println(op2); Testpublic void test2(){OptionalObject op Optional.empty();System.out.println(op);// No value presentSystem.out.println(op.get());} Testpublic void test3(){OptionalEmployee op Optional.ofNullable(new Employee(lisi, 33, 131.42, Employee.Status.FREE));System.out.println(op.get());OptionalObject op2 Optional.ofNullable(null);System.out.println(op2);// System.out.println(op2.get());}Testpublic void test5(){OptionalEmployee op1 Optional.ofNullable(new Employee(张三, 11, 11.33, Employee.Status.VOCATION));System.out.println(op1.orElse(new Employee()));System.out.println(op1.orElse(null));}Testpublic void test6(){OptionalEmployee op1 Optional.of(new Employee(田七, 11, 12.31, Employee.Status.BUSY));op1 Optional.empty();Employee employee op1.orElseGet(() - new Employee());System.out.println(employee);}Testpublic void test7(){OptionalEmployee op1 Optional.of(new Employee(田七, 11, 12.31, Employee.Status.BUSY));System.out.println(op1.map( (e) - e.getSalary()).get());}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556接口中可以定义默认实现方法和静态方法 在接口中可以使用default和static关键字来修饰接口中定义的普通方法 public interface Interface {default String getName(){return zhangsan;}static String getName2(){return zhangsan;} } 12345678910在JDK1.8中很多接口会新增方法为了保证1.8向下兼容1.7版本中的接口实现类不用每个都重新实现新添加的接口方法引入了default默认实现static的用法是直接用接口名去调方法即可。当一个类继承父类又实现接口时若后两者方法名相同则优先继承父类中的同名方法即“类优先”如果实现两个同名方法的接口则要求实现类必须手动声明默认实现哪个接口中的方法。 新的日期API LocalDate | LocalTime | LocalDateTime 新的日期API都是不可变的更使用于多线程的使用环境中 Testpublic void test(){// 从默认时区的系统时钟获取当前的日期时间。不用考虑时区差LocalDateTime date LocalDateTime.now();//2018-07-15T14:22:39.759System.out.println(date);System.out.println(date.getYear());System.out.println(date.getMonthValue());System.out.println(date.getDayOfMonth());System.out.println(date.getHour());System.out.println(date.getMinute());System.out.println(date.getSecond());System.out.println(date.getNano());// 手动创建一个LocalDateTime实例LocalDateTime date2 LocalDateTime.of(2017, 12, 17, 9, 31, 31, 31);System.out.println(date2);// 进行加操作得到新的日期实例LocalDateTime date3 date2.plusDays(12);System.out.println(date3);// 进行减操作得到新的日期实例LocalDateTime date4 date3.minusYears(2);System.out.println(date4);} 1234567891011121314151617181920212223242526Testpublic void test2(){// 时间戳 1970年1月1日000000 到某一个时间点的毫秒值// 默认获取UTC时区Instant ins Instant.now();System.out.println(ins);System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of(8)).toEpochMilli());System.out.println(System.currentTimeMillis());System.out.println(Instant.now().toEpochMilli());System.out.println(Instant.now().atOffset(ZoneOffset.ofHours(8)).toInstant().toEpochMilli());}12345678910111213Testpublic void test3(){// Duration:计算两个时间之间的间隔// Period计算两个日期之间的间隔Instant ins1 Instant.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}Instant ins2 Instant.now();Duration dura Duration.between(ins1, ins2);System.out.println(dura);System.out.println(dura.toMillis());System.out.println();LocalTime localTime LocalTime.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}LocalTime localTime2 LocalTime.now();Duration du2 Duration.between(localTime, localTime2);System.out.println(du2);System.out.println(du2.toMillis());}1234567891011121314151617181920212223242526272829 Testpublic void test4(){LocalDate localDate LocalDate.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}LocalDate localDate2 LocalDate.of(2016,12,12);Period pe Period.between(localDate, localDate2);System.out.println(pe);}1234567891011121314Testpublic void test5(){// temperalAdjust 时间校验器// 例如获取下周日 下一个工作日LocalDateTime ldt1 LocalDateTime.now();System.out.println(ldt1);// 获取一年中的第一天LocalDateTime ldt2 ldt1.withDayOfYear(1);System.out.println(ldt2);// 获取一个月中的第一天LocalDateTime ldt3 ldt1.withDayOfMonth(1);System.out.println(ldt3);LocalDateTime ldt4 ldt1.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));System.out.println(ldt4);// 获取下一个工作日LocalDateTime ldt5 ldt1.with((t) - {LocalDateTime ldt6 (LocalDateTime)t;DayOfWeek dayOfWeek ldt6.getDayOfWeek();if (DayOfWeek.FRIDAY.equals(dayOfWeek)){return ldt6.plusDays(3);}else if (DayOfWeek.SATURDAY.equals(dayOfWeek)){return ldt6.plusDays(2);}else {return ldt6.plusDays(1);}});System.out.println(ldt5);}123456789101112131415161718192021222324252627282930313233Testpublic void test6(){// DateTimeFormatter: 格式化时间/日期// 自定义格式LocalDateTime ldt LocalDateTime.now();DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy年MM月dd日);String strDate1 ldt.format(formatter);String strDate formatter.format(ldt);System.out.println(strDate);System.out.println(strDate1);// 使用api提供的格式DateTimeFormatter dtf DateTimeFormatter.ISO_DATE;LocalDateTime ldt2 LocalDateTime.now();String strDate3 dtf.format(ldt2);System.out.println(strDate3);// 解析字符串to时间DateTimeFormatter df DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);LocalDateTime time LocalDateTime.now();String localTime df.format(time);LocalDateTime ldt4 LocalDateTime.parse(2017-09-28 17:07:05,df);System.out.println(LocalDateTime转成String类型的时间localTime);System.out.println(String类型的时间转成LocalDateTimeldt4);}12345678910111213141516171819202122232425// ZoneTime ZoneDate ZoneDateTimeTestpublic void test7(){LocalDateTime now LocalDateTime.now(ZoneId.of(Asia/Shanghai));System.out.println(now);LocalDateTime now2 LocalDateTime.now();ZonedDateTime zdt now2.atZone(ZoneId.of(Asia/Shanghai));System.out.println(zdt);SetString set ZoneId.getAvailableZoneIds();set.stream().forEach(System.out::println);}12345678910111213补充 表示日期的LocalDate 表示时间的LocalTime 表示日期时间的LocalDateTime 新的日期API的几个优点 * 之前使用的java.util.Date月份从0开始我们一般会1使用很不方便java.time.LocalDate月份和星期都改成了enum* java.util.Date和SimpleDateFormat都不是线程安全的而LocalDate和LocalTime和最基本的String一样是不变类型不但线程安全而且不能修改。* java.util.Date是一个“万能接口”它包含日期、时间还有毫秒数更加明确需求取舍* 新接口更好用的原因是考虑到了日期时间的操作经常发生往前推或往后推几天的情况。用java.util.Date配合Calendar要写好多代码而且一般的开发人员还不一定能写对。 1234LocalDate public static void localDateTest() {//获取当前日期,只含年月日 固定格式 yyyy-MM-dd 2018-05-04LocalDate today LocalDate.now();// 根据年月日取日期5月就是5LocalDate oldDate LocalDate.of(2018, 5, 1);// 根据字符串取默认格式yyyy-MM-dd02不能写成2LocalDate yesteday LocalDate.parse(2018-05-03);// 如果不是闰年 传入29号也会报错LocalDate.parse(2018-02-29);}1234567891011121314LocalDate常用转化 /*** 日期转换常用,第一天或者最后一天...*/public static void localDateTransferTest(){//2018-05-04LocalDate today LocalDate.now();// 取本月第1天 2018-05-01LocalDate firstDayOfThisMonth today.with(TemporalAdjusters.firstDayOfMonth());// 取本月第2天2018-05-02LocalDate secondDayOfThisMonth today.withDayOfMonth(2);// 取本月最后一天再也不用计算是282930还是31 2018-05-31LocalDate lastDayOfThisMonth today.with(TemporalAdjusters.lastDayOfMonth());// 取下一天2018-06-01LocalDate firstDayOf2015 lastDayOfThisMonth.plusDays(1);// 取2018年10月第一个周三 so easy? 2018-10-03LocalDate thirdMondayOf2018 LocalDate.parse(2018-10-01).with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY));}1234567891011121314151617LocalTime public static void localTimeTest(){//16:25:46.448(纳秒值)LocalTime todayTimeWithMillisTime LocalTime.now();//16:28:48 不带纳秒值LocalTime todayTimeWithNoMillisTime LocalTime.now().withNano(0);LocalTime time1 LocalTime.parse(23:59:59);}1234567LocalDateTime public static void localDateTimeTest(){//转化为时间戳 毫秒值long time1 LocalDateTime.now().toInstant(ZoneOffset.of(8)).toEpochMilli();long time2 System.currentTimeMillis();//时间戳转化为localdatetimeDateTimeFormatter df DateTimeFormatter.ofPattern(YYYY-MM-dd HH:mm:ss.SSS);System.out.println(df.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time1),ZoneId.of(Asia/Shanghai))));}12345678910
http://www.yutouwan.com/news/419172/

相关文章:

  • 网站版式设计wordpress特定页面设为主页
  • 望牛墩网站建设北京网站优化关键词排名
  • 网站推广员能力要求移动网上
  • 已备案网站域名做建网站的工作一年赚几百万
  • 建立英文网站wordpress精致建站
  • 先网站开发后软件开发好wordpress二次元网站
  • 学网站开发去哪学中英文微信网站建设
  • 开原网站制作公司大型回合制手游排行榜
  • 福州台江区网站建设简历模板免费下载word 文档
  • 如何制作网站图片网站规划的原则有哪些内容
  • 上海市建设监理协会网站查询招生型网站建设
  • 太阳镜商城网站建设wordpress广告模板下载
  • 电子商务网站建设作品百度统计 wordpress 插件
  • 10m光纤做网站wordpress表格
  • 家具网站开发报告网站建设氺金手指排名11
  • 等保二级网站建设方案网站功能需求列表
  • 技术支持 合肥网站建设网站建设会使用的技术
  • 地板网站源码临沂市建设局的网站
  • 上海推广网站公司可以生成静态网站源码
  • 设计专业新手网站国家职业资格证书全国联网
  • 网站开发 培训织梦网站模板响应式
  • 网站有很多304状态码代写新闻稿
  • 长春高端品牌网站建设怎么查询在建工程
  • 怎样弄一个网站什么是关键词搜索
  • 惠州做网站的公司哪家好html5怎么做简单的网站
  • 武进区住房和城乡建设局网站免费平台
  • 注册域名网站wordpress制作小工具
  • 高端网站建设必去磐石网络平面设计网站大全有哪些
  • 外贸网站建设是什么意思做有声小说网站
  • 北京网站开发哪家专业网站推广计划包含的主要内容