天天看點

【Java學習】JDK1.8 日期類LocalDateTime(28)

擷取目前時間

Date類

SimpleDateFormat

Calendar

Date date = new Date();//擷取日期對象//Fri Jan 25 21:42:28 GMT+08:00 2019
        String string = date.toLocaleString();//擷取本地時間(過時方法)
        System.out.println(string);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd月");//定義格式化格式
        String format2 = simpleDateFormat.format(date);//格式化日期
        System.out.println(format2);
        Calendar instance = Calendar.getInstance();//擷取月曆對象
        Date time3 = instance.getTime();//得到時間對象
        System.out.println(time3);
        String format3 = simpleDateFormat.format(time3);
        System.out.println(format3);

           

JDK1.8新特性<------>(線程安全)

LocalDate 擷取年月日

LocalTime 擷取時分秒

LocalDateTime 擷取年月日時分秒

package com.westos.csdn;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Demo {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println(now);
        LocalTime now1 = LocalTime.now();
        System.out.println(now1);
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println(now2);
        LocalDate localDate = now2.toLocalDate();
        System.out.println(localDate);
        LocalTime localTime = now2.toLocalTime();
        System.out.println(localTime);
    }
}

           
【Java學習】JDK1.8 日期類LocalDateTime(28)

時間對象的方法

DayOfWeek dayOfWeek = now.getDayOfWeek();//枚舉一周七天
        System.out.println(dayOfWeek);
        Month month = now.getMonth();//枚舉十二個月份
        System.out.println(month);
           
LocalDate future = LocalDate.of(2020, 1, 1);//設定時間
        boolean after = future.isAfter(now);
        boolean before = now.isBefore(future);
        boolean leapYear = future.isLeapYear();//判斷是否閏年
           
//将字元串解析成日期對象,進而調用日期中方法
        String time = "2019-01-01";
        LocalDate parse = LocalDate.parse(time);//預設解析為2019-01-01
        System.out.println(parse);
        String time1 = "2019年01月01日";
        LocalDate parse1 = LocalDate.parse(time1,DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        System.out.println(parse1);
        String time2="2019-01-25T20:24:34.378";//這是ISO-8601預設格式,T連接配接
        LocalDateTime parse2 = LocalDateTime.parse(time2);
        DayOfWeek dayOfWeek1 = parse2.getDayOfWeek();
        System.out.println(dayOfWeek1);
           
//給指定日期加減和設定時間量
        //加plusXXX();傳回新的日期對象
        LocalDate localDate1 = now.plusDays(1);
        LocalDate localDate2 = now.plusYears(2);
        //減minusXXX();傳回新的日期對象
        LocalDate localDate3 = now.minusMonths(3);
        LocalDate localDate4 = now.minusWeeks(3);
        //指定時間系列withXXX();
        LocalDate localDate5 = now.withYear(2020);
        LocalDate localDate6 = now.withDayOfYear(20);
        LocalDate with = now.with(TemporalAdjusters.firstDayOfMonth());//傳回這個月的第一天
        LocalDate with1 = now.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));//擷取這個月第一個在周五
        System.out.println(with1);
           

擷取時間戳Instant和ZoneId時區類

Instant now3 = Instant.now();//擷取時間戳對象,是美國時間.中國時間=(美國時間+8小時)
        System.out.println(now3);//2019-01-25T12:44:57.580Z
        OffsetDateTime ChinaTime = now3.atOffset(ZoneOffset.ofHours(8));//變成中國時間
        System.out.println(ChinaTime);//2019-01-25T20:44:57.580+08:00
        //擷取本地時區
        ZoneId zoneId = ZoneId.systemDefault();//擷取本地時區
        ZonedDateTime ChinaTime1 = now3.atZone(zoneId);//将時間時區化
        System.out.println(ChinaTime1);//2019-01-25T20:44:57.580+08:00[GMT+08:00]
           
方法 不帶時區的本地時間 帶時區的本地時區
年月日 LocalDate ZoneDate
時分秒 LocalTime ZoneTime
年月日時分秒 LocalDateTime ZoneDateTime

擷取各國時區編号

ZoneId zoneId1 = ZoneId.systemDefault();//擷取本地時區編号
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();//擷取所有地方的時區編号
        for (String availableZoneId : availableZoneIds) {
            System.out.println(availableZoneId);
        }
           

案例:

計算兩個時間的間隔,以前是再代碼之前擷取本地時間,再在代碼後擷取本地時間,兩個相減,換算毫秒值.

以前

long start = System.currentTimeMillis();
        //代碼
        //代碼
        //代碼
        long end = System.currentTimeMillis();
        double second =(end - start) / 1000.0;//擷取秒值
        System.out.println(second);
           

現在用Java提供的方法

Instant start = Instant.now();
        for (int i = 0; i < 500000; i++) {
            System.out.println(i);
        }
        Instant end = Instant.now();
        Duration between = Duration.between(start, end);
        System.out.println(between.getSeconds());//擷取間隔時間的秒值
           

案例:

計算出生到現在你多少歲了

//出生到現在你多少歲了
        LocalDate birth = LocalDate.of(1996, 10, 28);
        LocalDate now4 = LocalDate.now();
        Period between1 = Period.between(birth, now4);
        System.out.println(between1.getYears());

           

時間矯正器TemporalAdjuster

//擷取下一個的周五
        TemporalAdjuster next = TemporalAdjusters.next(DayOfWeek.FRIDAY);
        LocalDate with2 = now.with(next);
        System.out.println(with2);
           

案例:

去有的機關辦事,機關人說,你下個工作日再來,請問下個工作日是什麼時候

LocalDate now5 = LocalDate.now();
        LocalDate with3 = now.with(new TemporalAdjuster() {
            @Override
            public Temporal adjustInto(Temporal temporal) {
                LocalDate date = (LocalDate) temporal;
                DayOfWeek day = date.getDayOfWeek();
                //System.out.println(day);
                if (day.equals(DayOfWeek.FRIDAY)) {
                    return now5.plusDays(3);
                } else if (day.equals(DayOfWeek.SATURDAY)) {
                    return now5.plusDays(2);
                } else {return now5.plusDays(1);}
            }
        });
        System.out.println(with3);
           
LocalDate now = LocalDate.now();
        LocalDate nextworkdate = now.with(new TemporalAdjuster() {
            @Override
            public Temporal adjustInto(Temporal temporal) {
                LocalDate localDate1 = (LocalDate) temporal;
                int day = 0;
                while (true) {
                    LocalDate localDate = localDate1.plusDays(day);
                    DayOfWeek dayOfWeek = localDate.getDayOfWeek();
                    if (dayOfWeek.equals(DayOfWeek.FRIDAY) || dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                        day += 1;
                        continue;
                    } else {
                        day += 1;
                        break;
                    }
                }
                return localDate1.plusDays(day);
            }
        });
        System.out.println(nextworkdate);
           

格式化日期類

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd月");
        LocalDate now6 = LocalDate.now();
        String format = now6.format(formatter);//日期類的format方法,傳入格式化對象
        System.out.println(format);
        String format1 = formatter.format(now6);//格式化類的format方法,傳入日期
        System.out.println(format1);
           

謝謝!

繼續閱讀