天天看点

java8新特性 -------------时间日期

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * create gl 
 **/
public class DateFormatTheradLocal {

    private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>(){
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    public static Date convert(String str) throws ParseException {
        return df.get().parse(str);
    }

}      
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * create gl
 *  传统解决时间方式
 **/
public class TestTime01 {


    // 存在线程安全问题
    public static void test1() throws ExecutionException, InterruptedException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddd");
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Callable<Date> task = new Callable() {
            @Override
            public Object call() throws Exception {
                return simpleDateFormat.parse("2018-06-28");
            }
        };
        List<Future<Date>> futureList = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
             futureList.add(executorService.submit(task));
        }
        for (Future<Date> future : futureList) {
            System.out.println(future.get());
        }
    }

    public static void test2() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Callable<Date> task = new Callable() {
            @Override
            public Object call() throws Exception {
                return DateFormatTheradLocal.convert("2018-06-28");
            }
        };
        List<Future<Date>> futureList = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            futureList.add(executorService.submit(task));
        }
        for (Future<Date> future : futureList) {
            System.out.println(future.get());
        }
        executorService.shutdown();
    }

    // java8 新的api 线程安全的
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Callable<LocalDate> task = new Callable() {
            @Override
            public Object call() throws Exception {
                return LocalDate.parse("2018-06-28",dateTimeFormatter);
            }
        };
        List<Future<LocalDate>> futureList = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            futureList.add(executorService.submit(task));
        }
        for (Future<LocalDate> future : futureList) {
            System.out.println(future.get());
        }
        executorService.shutdown();
    }


}      
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/**
 * create gl
 **/
public class TestLocalDateTime {


    // LocalDate  LocalTime LocalDateTime

    public static void test(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        System.out.println(localDateTime.getYear()); // 年
        System.out.println(localDateTime.getSecond());  // 秒
        System.out.println(localDateTime.getMonthValue()); //月 中文
        System.out.println(localDateTime.getMonth());  // 月  英文
        System.out.println(localDateTime.getMinute());  // 分
        System.out.println(localDateTime.getHour());  // 小时
        System.out.println(localDateTime.getNano());  // 纳秒
        System.out.println(localDateTime.getDayOfYear());
        System.out.println(localDateTime.getDayOfWeek());  // 星期
        System.out.println(localDateTime.getDayOfMonth()); // 日


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

        LocalDateTime localDateTime1 = LocalDateTime.of(2900,10,20,22,12,30);

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

        // 对时间做一些操作

        // plus...  加  ; minus... 减
        LocalDateTime localDateTime2 = localDateTime.plusYears(2);
        System.out.println(localDateTime2);

    }


    //  Instant 时间戳  以Unix元年 1970年1月日 00:00:00 之间的毫秒值
    public static void test1(){
        Instant instant = Instant.now();  // 默认获取的UTC时区
        System.out.println(instant);
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));  // 偏移量
        System.out.println(offsetDateTime);  // 转为对应的毫秒值
        System.out.println(instant.toEpochMilli());  // 转为对应的毫秒值
        Instant ofEpochSecond = Instant.ofEpochSecond(1000);  // 1970年1月日 00:00:00  加上这个秒数
        System.out.println(ofEpochSecond);
    }

    /**
     *  Duration  计算两个时间之间的间隔
     *  Period     计算两个日期之间的间隔
     *
     */

    public static void test2() throws InterruptedException {

        Instant instant = Instant.now();
        Thread.sleep(1000);
        Instant instant1 = Instant.now();

        Duration between = Duration.between(instant, instant1);

        System.out.println(between.getSeconds());
        System.out.println(between.toMillis());   // to... 方法

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

        LocalTime cl = LocalTime.now();

        Thread.sleep(1000);

        LocalTime cl2 = LocalTime.now();

        Duration between1 = Duration.between(cl, cl2);

        System.out.println(between1.toNanos());

    }

    public static void test3(){

        LocalDate localDate = LocalDate.of(2016,10,10);
        LocalDate localDate1 = LocalDate.now();
        Period between = Period.between(localDate, localDate1);
        System.out.println(between);
        System.out.println(between.getYears());
        System.out.println(between.getMonths());
        System.out.println(between.getDays());
    }


    /**
     *  TemporalAdjuster   时间矫正器
     *
     */
    public static void test4(){

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        LocalDateTime withDayOfMonth = localDateTime.withDayOfMonth(10);// with... 系列方法
        System.out.println(withDayOfMonth);

        LocalDateTime with = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));  // 下个周日是什么时候
        System.out.println(with);


        // 自定义, 下一个工作日
        LocalDateTime localDateTime1 = localDateTime.with(x -> {
            LocalDateTime ld4 = (LocalDateTime) x;   // x 参数是 Temporal
            DayOfWeek dayOfWeek = ld4.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {  // 如果是周五
                return ld4.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {  // 如果是周六
                return ld4.plusDays(2);
            } else {
                return ld4.plusDays(1);
            }
        });

        System.out.println(localDateTime1);

    }


    /**
     * 时间格式化
     */
    public static void test5(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE;
        LocalDateTime ldt = LocalDateTime.now();
        String date = ldt.format(dateTimeFormatter);
        System.out.println(date);

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

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

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

        LocalDateTime parse = LocalDateTime.parse(format,timeFormatter);
        System.out.println(parse);
    }

    /**
     * 带时区的 时间  ZonedDate,ZonedTime,ZonedDateTime
     */
    public static void test6(){
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();   // 支持的地区
//        zoneIds.forEach(System.out::println);

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

        LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Europe/Belgrade"));
        System.out.println(localDateTime);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Europe/Belgrade"));
        System.out.println(zonedDateTime);  // + ...  与 UTC相比 差多少时差


    }


    public static void main(String[] args) throws InterruptedException {
//         test();
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
        test6();
    }


}