您的位置:首页 > 编程语言 > Java开发

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 Date
相关文章推荐