首页  

Java8日期api使用     所属分类 java 浏览量 1471
java.util.Date 可变 
SimpleDateFormat 非线程安全 

java8 新的 日期 api  , java.time 包  不可变且线程安全


Instant 时间戳
Duration 持续时间,时间差
LocalDate 只包含日期,比如:2018-02-05
LocalTime 只包含时间,比如:23:12:10
LocalDateTime 包含日期和时间,比如:2018-02-05 23:14:21
Period 时间段
ZoneOffset 时区偏移量,比如:+8:00
ZonedDateTime 带时区的时间
Clock 时钟,比如获取目前美国纽约的时间
java.time.format.DateTimeFormatter 时间格式化


import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateApiTest {

	public static void main(String[] args) throws Exception {
		System.out.println("java8 DateApiTest start " + LocalDateTime.now());

		// 当前 日期 时间 日期和时间
		System.out.println(LocalDate.now());
		System.out.println(LocalTime.now());
		System.out.println(LocalDateTime.now());

		// 获取年月日
		LocalDate today = LocalDate.now();
        int year = today.getYear();
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();
        
        System.out.println(year+"-"+month+"-"+day);
        
        LocalDate myBirthDay = LocalDate.of(1982,10,13);
        System.out.println("myBirthDay="+myBirthDay);
        
        System.out.println(today.equals(myBirthDay));
        
        // 时间加减
        LocalTime nowtime = LocalTime.now();
        System.out.println("nowtime="+nowtime);
        System.out.println("nowtime.plusHours(3)="+nowtime.plusHours(3));    
        System.out.println("nowtime.plusHours(20)="+nowtime.plusHours(20));
        System.out.println("nowtime.plusHours(30)="+nowtime.plusHours(30));

        LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
        System.out.println("today="+today);
        System.out.println("一周后的日期="+nextWeek);
        
        // 判断时间先后
        System.out.println(nextWeek.isAfter(today));

        LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
        System.out.println("一年前的日期=" + previousYear);
        
        System.out.println(previousYear.isAfter(today));


        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        System.out.println("一年后的日期="+nextYear);

        // 时钟  
        Clock clock = Clock.systemUTC();
        System.out.println("Clock.systemUTC : " + clock +","+clock.millis());

        Clock defaultClock = Clock.systemDefaultZone();
        System.out.println("defaultClock : " + defaultClock+","+defaultClock.millis());


        // 时区 
        ZoneId america = ZoneId.of("America/New_York");
        LocalDateTime localtDateAndTime = LocalDateTime.now();
        ZonedDateTime dateAndTimeInNewYork  = ZonedDateTime.of(localtDateAndTime, america );
        System.out.println("Current date and time in a particular timezone : " + dateAndTimeInNewYork);

        // 各时区的当前时间
        System.out.println(localtDateAndTime);
        System.out.println(LocalDateTime.now(america));
        System.out.println(LocalDateTime.now(Clock.systemUTC()));    
        System.out.println(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        System.out.println(LocalDateTime.now(ZoneId.of("Europe/Rome")));

        // 
        YearMonth currentYearMonth = YearMonth.now();
        System.out.println("currentYearMonth="+currentYearMonth);

        System.out.println("This year is Leap year:"+today.isLeapYear());
        
        // 
        Period p1 = Period.between(today, myBirthDay);       
        System.out.println(p1+","+p1.toTotalMonths());
        
        p1 = Period.between(myBirthDay,today);
        System.out.println(p1+","+p1.toTotalMonths());
        
        //  Instant 和 Date  转换
        Instant timestamp = Instant.now();
        System.out.println("What is value of this instant " + timestamp.toEpochMilli());
        System.out.println("timestamp="+timestamp);

        
        Date nowdate = Date.from(timestamp) ;
        System.out.println("nowdate="+nowdate);
        
        Instant nowInstant = nowdate.toInstant();
        System.out.println("nowInstant="+nowInstant);
        
        String myBirthDayStr = "19821013";
        LocalDate myBirthDay2 = LocalDate.parse(myBirthDayStr,DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("myBirthDay2="+myBirthDay2);
        
        // 时间格式化
        LocalDateTime nowdatetime = LocalDateTime.now();      
        System.out.println("nowdatetime="+nowdatetime);

      
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowdatetimestr = nowdatetime.format(format);
        
        System.out.println("nowdatetimestr="+nowdatetimestr);

        nowdatetime = LocalDateTime.parse(nowdatetimestr, format);
        System.out.println("nowdatetime="+nowdatetime);
           
	}
}

上一篇     下一篇
java debug 原理简介

javaagent机制

javaagent两种加载方式及实例

jconsole连接jmx失败处理

大数据风控概述

金融科技关键技术及应用