当前位置:网站首页>【LocalDate LocalTime LocalDateTime】1. Using immutability to achieve thread safety 2 Current date, current time, current date time 3 Since the time zone is not considered, you need to add 8 hours to th

【LocalDate LocalTime LocalDateTime】1. Using immutability to achieve thread safety 2 Current date, current time, current date time 3 Since the time zone is not considered, you need to add 8 hours to th

2022-06-12 14:36:00 Understand the principle + good code skills

 1) test 1

package org.example.testTime;

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

public class TestLocalDateTime {
    public static void main(String[] args) {
        // step1. date 
        LocalDate now = LocalDate.now();
        System.out.println("now=" + now); // now=2022-02-18

        // step2. Time 
        LocalTime now1 = LocalTime.now();
        System.out.println("now1=" + now1.withNano(0)); // now1=00:45:19

        // step3. date + Time 
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println("now2=" + now2.withNano(0)); // now2=2022-02-18T00:46:04

        // step4. Specify month, day, hour, minute and second 
        LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8, 0);
        System.out.println("of=" + of); // of=2008-08-08T20:08:08

        // step5. Designated year 
        LocalDateTime of2 = of.withYear(2022);
        System.out.println("of2=" + of2); // of2=2022-08-08T20:08:08

        // step6. Add a day 
        LocalDateTime of3 = of2.plusDays(2);
        System.out.println("of3=" + of3); // of3=2022-08-10T20:08:08

        // step7. Specify the timestamp of the day of the week 
        int dayOfWeek = 7;
        LocalDateTime ldt = LocalDateTime.now();
        LocalDateTime newTime = ldt.minusDays(ldt.getDayOfWeek().getValue()) //  Let's put the day of the week into 0
                .plusDays(dayOfWeek) //  Plus the number of days elapsed 
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
        System.out.println("newTime=" + newTime); // newTime=2022-02-20T00:00

        // step8. Get the current timestamp ( millisecond )
        LocalDateTime now3 = LocalDateTime.now();
        long l = now3.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println("l=" + l); // l=1645116890691
    }
}




2) test 2

TimeService.java

package org.example.testTime.service;

import org.example.testTime.logic.time.TimeManager;

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

public class TimeService {

    /**
     *  Zhou {} {}:{}:{} The timestamp 
     *
     * @param str   Such as : "05:00:00"
     * @param loop  Such as : 7
     * @return
     */
    public static long getSpecificDayTime(String str, int loop) {
        String[] split = str.split(":");

        int h = Integer.parseInt(split[0]);
        int min = Integer.parseInt(split[1]);
        int sec = Integer.parseInt(split[2]);

        LocalDateTime weekDay = TimeManager.getSpecialWeekDayAtStartOfDayTime(loop);
        LocalDateTime time = weekDay
                .withHour(h)
                .withMinute(min)
                .withSecond(sec)
                .withNano(0);
        return TimeManager.localDateTimeToTimeMilli(time);
    }

    /**
     * 2 Whether the timestamps are on the same day 
     *
     * @param timestamp1
     * @param timestamp2
     * @return
     */
    public static boolean isSameDay(long timestamp1, long timestamp2) {
        LocalDate localDate1 = TimeManager.getLocalDateByTimestamp(timestamp1);
        LocalDate localDate2 = TimeManager.getLocalDateByTimestamp(timestamp2);
        return localDate1.equals(localDate2);
    }

    /**
     *  Current timestamp 
     *
     * @return
     */
    public static long getCurrentTimeMillis() {
        return TimeManager.localDateTimeToTimeMilli(LocalDateTime.now());
    }
}

TimeManager.java

package org.example.testTime.logic.time;

import java.time.*;

public class TimeManager {
    private static final ZoneId ZONE_ID = ZoneId.systemDefault();

    /**
     *  Timestamp of current time 
     *
     * @param time
     * @return
     */
    public static long localDateTimeToTimeMilli(LocalDateTime time) {
        return time.atZone(ZONE_ID).toInstant().toEpochMilli();
    }

    /**
     *  Zhou {} 0 Local time of point 
     *
     * @param loop
     * @return
     */
    public static LocalDateTime getSpecialWeekDayAtStartOfDayTime(int loop) {
        DayOfWeek dayOfWeek = DayOfWeek.of(loop);
        //  Get today's zero 
        LocalDateTime now = LocalDate.now().atStartOfDay();
        //  According to the week shift, we get 
        LocalDateTime weekDay = now.minusDays(now.getDayOfWeek().getValue() - dayOfWeek.getValue());
        return weekDay;
    }

    /**
     *  Time stamp to date time 
     *
     * @param timestamp
     * @return
     */
    public static LocalDateTime getLocalDateTimeByTimestamp(long timestamp) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZONE_ID);
    }

    /**
     *  Time stamp date 
     *
     * @param timestamp
     * @return
     */
    public static LocalDate getLocalDateByTimestamp(long timestamp) {
        return getLocalDateTimeByTimestamp(timestamp).toLocalDate();
    }


}

Main.java

package org.example.testTime;

import org.example.testTime.service.TimeService;

public class Main {
    public static void main(String[] args) {
        // step1: Test specified week {} {}:{}:{} The timestamp 
        System.out.println(TimeService.getSpecificDayTime("00:00:00", 7)); // 1645286400000 ==>2022-02-20 00:00:00
        System.out.println(TimeService.getSpecificDayTime("05:00:00", 7)); // 1645304400000 ==>2022-02-20 05:00:00

        // step2: Judge 2 Whether the timestamps are the same day 
        System.out.println(TimeService.isSameDay(1645286400000L, 1645304400000L));

        // step3. Current timestamp 
        System.out.println(System.currentTimeMillis()); // 1645168940559
        System.out.println(TimeService.getCurrentTimeMillis()); // 1645168940559
    }
}

原网站

版权声明
本文为[Understand the principle + good code skills]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010504207499.html