Java8学习计划----NewDateAPI的使用
2018-03-09 17:10
656 查看
零零散散接近一个月的课余时间,学完Java8InAction和Guava,感触很多,收获也很大,特别开心,接下来会利用空余时间学习Spark,希望自己在技术上慢慢积累,越来越从容。
下面是关于新版的Java8的new DateAPIpackage com.company.LambdaExpressions.NewDateAPI;
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* Created by mengxiaopeng on 2018/3/9.
* com.company.LambdaExpressions.NewDateAPI
* 新版的DateAPI 将date 和 time 分开使用
* 主要相比JDK8之前的 修复了很多问题 比如线程安全 或者意图不明显等
* 新版的所有的Time包下的Class This class is immutable and thread-safe.
* Localdate LocalTime LocalDateTime
* Instant Duration Period
* localDate.formate
* LocalDate.parse
*
*/
public class MyDateAPI8 {
public void testLocalDate(){
LocalDate localDate = LocalDate.of(2016, 11, 13);
System.out.println(localDate.getYear());
System.out.println(localDate.getMonth());
System.out.println(localDate.getMonthValue());
System.out.println(localDate.getDayOfYear());
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek());
}
public void testLocalTime() {
LocalTime time = LocalTime.now();
System.out.println(time.getHour());
System.out.println(time.getMinute());
System.out.println(time.getSecond());
}
public void combineLocalDateAndTime() {
LocalDate localDate = LocalDate.now();
LocalTime time = LocalTime.now();
//####
LocalDateTime localDateTime = LocalDateTime.of(localDate, time);
System.out.println(localDateTime.toString());
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
}
//关于时间点Instant 类似于System.currentTimeMillis() 或者guava的stopwatch
public void testInstant() throws InterruptedException {
Instant start = Instant.now();
Thread.sleep(1000L);
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
System.out.println(duration.toMillis());
}
public void testDuration() {
LocalTime time = LocalTime.now();
LocalTime beforeTime = time.minusHours(1);
Duration duration = Duration.between(beforeTime,time);
System.out.println(duration.toHours());
}
//大的时间区间period
public void testPeriod() {
Period period = Period.between(LocalDate.of(2014, 1, 10), LocalDate.of(2016, 1, 10));
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.getYears());
}
//Formate针对类型 localDate等
public void testDateFormat() {
LocalDate localDate = LocalDate.now();
String format1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(format1);
//#### 自定义 时间日期格式
DateTimeFormatter mySelfFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format = localDate.format(mySelfFormatter);
System.out.println(format);
}
//针对字符串 LocalDate的静态方法
public void testDateParse() {
String date1 = "20161113";
LocalDate localDate = LocalDate.parse(date1, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(localDate);
DateTimeFormatter mySelfFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date2 = "2016-11-13";
LocalDate localDate2 = LocalDate.parse(date2, mySelfFormatter);
System.out.println(localDate2);
}
}
下面是关于新版的Java8的new DateAPIpackage com.company.LambdaExpressions.NewDateAPI;
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* Created by mengxiaopeng on 2018/3/9.
* com.company.LambdaExpressions.NewDateAPI
* 新版的DateAPI 将date 和 time 分开使用
* 主要相比JDK8之前的 修复了很多问题 比如线程安全 或者意图不明显等
* 新版的所有的Time包下的Class This class is immutable and thread-safe.
* Localdate LocalTime LocalDateTime
* Instant Duration Period
* localDate.formate
* LocalDate.parse
*
*/
public class MyDateAPI8 {
public void testLocalDate(){
LocalDate localDate = LocalDate.of(2016, 11, 13);
System.out.println(localDate.getYear());
System.out.println(localDate.getMonth());
System.out.println(localDate.getMonthValue());
System.out.println(localDate.getDayOfYear());
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek());
}
public void testLocalTime() {
LocalTime time = LocalTime.now();
System.out.println(time.getHour());
System.out.println(time.getMinute());
System.out.println(time.getSecond());
}
public void combineLocalDateAndTime() {
LocalDate localDate = LocalDate.now();
LocalTime time = LocalTime.now();
//####
LocalDateTime localDateTime = LocalDateTime.of(localDate, time);
System.out.println(localDateTime.toString());
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
}
//关于时间点Instant 类似于System.currentTimeMillis() 或者guava的stopwatch
public void testInstant() throws InterruptedException {
Instant start = Instant.now();
Thread.sleep(1000L);
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
System.out.println(duration.toMillis());
}
public void testDuration() {
LocalTime time = LocalTime.now();
LocalTime beforeTime = time.minusHours(1);
Duration duration = Duration.between(beforeTime,time);
System.out.println(duration.toHours());
}
//大的时间区间period
public void testPeriod() {
Period period = Period.between(LocalDate.of(2014, 1, 10), LocalDate.of(2016, 1, 10));
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.getYears());
}
//Formate针对类型 localDate等
public void testDateFormat() {
LocalDate localDate = LocalDate.now();
String format1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(format1);
//#### 自定义 时间日期格式
DateTimeFormatter mySelfFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format = localDate.format(mySelfFormatter);
System.out.println(format);
}
//针对字符串 LocalDate的静态方法
public void testDateParse() {
String date1 = "20161113";
LocalDate localDate = LocalDate.parse(date1, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(localDate);
DateTimeFormatter mySelfFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date2 = "2016-11-13";
LocalDate localDate2 = LocalDate.parse(date2, mySelfFormatter);
System.out.println(localDate2);
}
}
相关文章推荐
- Java使用new Date()和System.currentTimeMillis()获取当前时间戳
- Java写的手机计费系统(作为学习参考的实例:关键是Java类库里面的date和Calendar的使用和相互转换,以及一些日期的实际问题)
- JAVA学习第三十四课 (经常使用对象API)—List集合及其子类特点
- JAVA学习第三十九课(常用对象API)— Map集合及其子类对象(集合使用技巧)
- JAVA学习第三十三课(经常使用对象API) —迭代器
- java高级编程学习如何使用打印服务 API
- Java菜鸟学习笔记(3)--Java API 文档下载与文档注释的使用
- java使用new Date()和System.currentTimeMillis()获取当前时间戳
- JAVA学习第三十六课(经常使用对象API)— Set集合:HashSet集合演示
- java使用new Date()和System.currentTimeMillis()获取当前时间戳(转载)
- JAVA学习第二十九课(经常使用对象API)- String类
- java中时间格式SimpleDateFormat的使用学习
- Java进阶(十六)使用new Date()和System.currentTimeMillis()获取当前时间戳
- Java学习--(十一)包装类,Date,SimpleDateFormat,Calendar类的使用
- Java-第四章-使用switch为小明制定学习计划
- Java进阶(十六)使用new Date()和System.currentTimeMillis()获取当前时间戳
- Java菜鸟学习笔记--配置篇(三):Java API 文档下载与文档注释的使用
- Android(java)学习笔记193:利用谷歌API对数据库增删改查(推荐使用)
- java学习之Date的使用
- JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)