【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);
    }
}

在这里插入图片描述


时间对象的方法

		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]
方法不带时区的本地时间带时区的本地时区
年月日LocalDateZoneDate
时分秒LocalTimeZoneTime
年月日时分秒LocalDateTimeZoneDateTime

获取各国时区编号

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);

谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值