Java|Java 8 要点
Java 8 要点
- new Date/Time API
- Stream
- Lambda Expressions
- Method references
- Optional
- Default Method
- 老时间库的问题 - why jsr310
- java.util.Date和SimpleDateFormat 线程不安全
- 月份从1开始,日期从0开始
- 两个相同的java.sql.Time实例不equals
- java.util.Date vs. java.sql.Timestamp/Date/Time
- 新的时间库的设计思想
- Immutable
- LocalDate, LocalTime, LocalDateTime
- 如果需要时区信息,换另外一套
- 新老时间类型互转
- java.sql.Timestamp#toLocalDateTime
- java.sql.Timestamp#from
- format
@Test
public void format() throws Exception {
DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
Assert.assertEquals("2011-11-11", dateFormatter.format(LocalDate.of(2011, 11, 11)));
}
- parse
@Test
public void parse() throws Exception {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate day = LocalDate.parse("1900-01-01", dateFormatter);
Assert.assertEquals(LocalDate.of(1900, 1, 1), day);
}
- JDBC
JDBC4.2(mysql java 驱动6.0)开始才支持 Java 8的新的时间库,在使用JDBC4.2之前,使用时需要将新的类型转换成老的类型 - Spring MVC
@SpringBootApplication
public class WebMain extends WebMvcConfigurerAdapter {@Override
public void addFormatters(FormatterRegistry registry) {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateFormatter(DateTimeFormatter.ISO_DATE);
registrar.setTimeFormatter(DateTimeFormatter.ISO_TIME);
registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
registrar.registerFormatters(registry);
}}
- JPA
@Bean
public LocalContainerEntityManagerFactoryBean catalogEntityManagerFactory(
@Qualifier(CatalogRepositoryConfig.UNIT_NAME) DataSource catalogDataSource,
EntityManagerFactoryBuilder builder, JpaProperties jpaProperties) {
return builder.dataSource(catalogDataSource)
.packages(Server.class, Jsr310JpaConverters.class)
.persistenceUnit(UNIT_NAME)
.properties(jpaProperties.getHibernateProperties(catalogDataSource))
.build();
}
- Jackson
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
com.fasterxml.jackson.datatype
jackson-datatype-jsr310
@Bean
public SimpleModule jsr310JacksonModule() {
SimpleModule jsr310Module = new SimpleModule();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
jsr310Module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
jsr310Module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
return jsr310Module;
}
- 时间计算
@Test
public void plusOrMinus() throws Exception {
LocalDateTime payTime = LocalDateTime.of(2015, 10, 10, 19, 0);
Assert.assertEquals(LocalDateTime.of(2015, 10, 30, 19, 0), payTime.plusDays(20));
Assert.assertEquals(LocalDateTime.of(2015, 10, 11, 5, 0), payTime.plusHours(10));
Assert.assertEquals(LocalDateTime.of(2015, 9, 30, 19, 0), payTime.minusDays(10));
}
stream consumer
使用场景
- groupBy
- partition
- sum
- map
- filter
- stream只能consume一次
- Iterable没有stream
- stream代表的是一些function的整合,并不是一个数据结构
- parallel stream 慎用
- stream的每个function最好是没有副作用的
- Optional
- OptionalInt, OptionalLong, OptionalDouble
- https://leanpub.com/whatsnewinjava8/read
- https://github.com/adamd/hellojava8
- https://github.com/katoquro/jdk8-lambda-samples
- https://octoperf.com/blog/2015/08/24/one-year-with-java-8-in-production/
- http://zeroturnaround.com/rebellabs/java-parallel-streams-are-bad-for-your-health/
- http://zeroturnaround.com/rebellabs/how-your-addiction-to-java-8-default-methods-may-make-pandas-sad-and-your-teammates-angry/
- http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/
- http://blog.jooq.org/2014/06/13/java-8-friday-10-subtle-mistakes-when-using-the-streams-api/
- http://blog.jooq.org/2014/04/04/java-8-friday-the-dark-side-of-java-8/
- http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/
- http://blog.takipi.com/new-parallelism-apis-in-java-8-behind-the-glitz-and-glamour/
- http://blog.takipi.com/the-dark-side-of-lambda-expressions-in-java-8/
- https://robinst.github.io/guava-java8-presentation
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用
- Java基础-高级特性-枚举实现状态机