Java 8 要点
- new Date/Time API
- Stream
- Lambda Expressions
- Method references
- Optional
- Default Method
new Date/Time API - java.time
- 老时间库的问题 - 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
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
@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最好是没有副作用的
Lambda Expressions
Method References
Optional
- 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
网友评论