Java_JDK8(時間與日期操作)

Java_JDK8(時間與日期操作)

package com.lius.joda;

import java.sql.SQLOutput;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Set;
import java.util.TreeSet;

/**
 * <p>Java8 時間處理函數</p>
 */
public class jodeTimeTest3 {
    public static void main(String[] args) {
        //LocaDate
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        System.out.println(localDate.getYear()+","+localDate.getMonthValue()+","+localDate.getDayOfMonth());
        System.out.println("**********************");
        LocalDate lDate = LocalDate.of(2020, 03, 04);
        System.out.println(lDate);
        System.out.println("**********************");
        //MonthDay
        LocalDate tDate = LocalDate.of(2020, 06, 10);
        MonthDay md = MonthDay.of(lDate.getMonthValue(), lDate.getDayOfMonth());
        MonthDay fd = MonthDay.from(LocalDate.of(2019, 01, 16));
        if(md.equals(fd)){
            System.out.println("equals");
        }else System.out.println("not equals");
        System.out.println("****************************");
        //LocalTime
        LocalTime time = LocalTime.now();
        LocalTime time2 = time.plusHours(3).plusMinutes(20);
        System.out.println(time2);
        System.out.println("*****************************");


        LocalDate now = LocalDate.now();
        LocalDate addTWeeks = now.plus(2, ChronoUnit.WEEKS);
        System.out.println(addTWeeks);
        LocalDate minustMonth = now.minus(2, ChronoUnit.MONTHS);
        System.out.println(minustMonth);
        System.out.println("****************");

        Set<String> re = ZoneId.getAvailableZoneIds();
        Set<String> tRe = new TreeSet<String>(){
            {
                addAll(re);
            }
        };
        tRe.stream().forEach(System.out::println);
        System.out.println("*************************");

        LocalDateTime nowTime = LocalDateTime.now();
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime zTime = ZonedDateTime.of(nowTime, zoneId);
        System.out.println(nowTime);
        System.out.println(zTime);

        System.out.println("*************************");


        YearMonth ym = YearMonth.now();
        System.out.println(ym);
        System.out.println(ym.lengthOfMonth());
        System.out.println(ym.isLeapYear());

        System.out.println("************************");

        YearMonth ym1 = YearMonth.of(2020, 06);
        System.out.println(ym1);
        System.out.println(ym1.lengthOfMonth());
        System.out.println(ym1.lengthOfYear());
        System.out.println(ym1.isLeapYear());


        System.out.println("****************************");

        //period
        now = LocalDate.now();
        LocalDate testDate = LocalDate.of(2020, 06, 11);
        Period period = Period.between(testDate,now);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

        System.out.println("***********************");


        System.out.println(Instant.now());  //獲取當前utc時間


        System.out.println("*******************");

        Clock zone = Clock.systemDefaultZone();
        System.out.println(zone.millis());   //獲取當前系統時區的毫秒數





    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章