LocalDateTime学习

package com.shengda.Demo12DateJDK8;

import java.time.LocalDateTime;

public class JDK8DateDemo1 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();  // 获取当前系统时间
        System.out.println(now);

        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11,11);  // 使用指定年月日和时分秒初始化一个LocalDateTime对象
        System.out.println(localDateTime);
    }
}

LocalDateTime获取方法

// public int getYear()  获取年
// public int getMonthValue()  获取月份(1-12)
// public int getDayOfMonth()  获取月份中的第几天(1-31)
// public int getDayOfYear()  获取一年中的第几天(1-366)
// public DayOfWeek getDayOfWeek()  获取星期
// public int getMinute()  获取分钟
// public int getHour()  获取小时
package com.shengda.Demo12DateJDK8;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;

public class JDK8DateDemo2 {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
        // public int getYear()  获取年
        int year = localDateTime.getYear();
        System.out.println(year);
        // public int getMonthValue()  获取月份(1-12)
        int monthValue = localDateTime.getMonthValue();
        System.out.println(monthValue);
        // public int getDayOfMonth()  获取月份中的第几天(1-31)
        int dayOfMonth = localDateTime.getDayOfMonth();
        System.out.println(dayOfMonth);
        // public int getDayOfYear()  获取一年中的第几天(1-366)
        int dayOfYear = localDateTime.getDayOfYear();
        System.out.println("这时一年的第几天"+dayOfYear);
        // public DayOfWeek getDayOfWeek()  获取星期
        DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
        System.out.println(dayOfWeek);
        // public int getMinute()  获取分钟
        int minute = localDateTime.getMinute();
        System.out.println(minute);
        // public int getHour()  获取小时
        int hour = localDateTime.getHour();
        System.out.println(hour);
    }
}

LocalDateTime转换方法

// public LocalDate toLocalDate () 转换成一个LocalDate对象
// public LocalTime toLocalTime()  转换成一个LocalTime对象
package com.shengda.Demo12DateJDK8;

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

public class JDK8DateDemo3 {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.of(2020, 12, 12, 12, 12, 12);
        // public LocalDate toLocalDate () 转换成一个LocalDate对象
        LocalDate localDate = localDateTime.toLocalDate();
        System.out.println(localDate);
        // public LocalTime toLocalTime()  转换成一个LocalTime对象
        LocalTime localTime = localDateTime.toLocalTime();
        System.out.println(localTime);

        /*2020-12-12
        12:12:12*/

    }
}

LocalDateTime格式化和解析

public static LocalDateTime parse (准备解析的字符串,解析格式) 把一个日期字符串解析成指定格式
public String format (指定格式)   把一个LocalDateTime格式化成为一个字符串
package com.shengda.Demo12DateJDK8;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class JDK8DateDemo4 {
    public static void main(String[] args) {
        method1();  // 格式化
        // 解析
        // public static LocalDateTime parse (准备解析的字符串,解析格式) 把一个日期字符串解析成一个字符串
        String s = "2020年12月12日 12:13:14";
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(s, pattern);
        System.out.println(parse);
    }

    private static void method1() {
        LocalDateTime localDateTime = LocalDateTime.of(2020, 12, 12, 12, 12, 12);
        System.out.println(localDateTime);
        // public String format (指定格式)   把一个LocalDateTime格式化成为一个字符串
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = localDateTime.format(pattern);
        System.out.println(format);
    }
}

计算两个时间间隔的方法

计算间隔的年、月、日

package com.shengda.Demo12DateJDK8;

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

/*
计算两个时间的间隔
* */
public class JDK8DateDemo7 {
    public static void main(String[] args) {
        // public static Period between(开始时间,结束时间)  计算两个时间的间隔
        LocalDate localDate1 = LocalDate.of(2020, 1, 1);
        LocalDate localDate2 = LocalDate.now();
        Period period = Period.between(localDate1, localDate2);
        System.out.println(period);  // P2Y17D 其中的P表示Period对象 2Y表示2年 17D表示17天

        // public int getYears()  获得这段时间的年数
        System.out.println(period.getYears()); // 2
        // public int getMonths()  获得此期间月数
        System.out.println(period.getMonths()); // 0
        // public int getDays()  获得此期间的天数
        System.out.println(period.getDays());  // 17
        // public long toTotalMonths()  获得此期间的总月份
        System.out.println(period.toTotalMonths());  // 24

    }
}

计算两端时间间隔的秒、毫秒、纳秒

package com.shengda.Demo12DateJDK8;

import java.time.Duration;
import java.time.LocalDateTime;

public class JDK8DateDemo8 {
    public static void main(String[] args) {
        // public static Duration between(开始时间,结束时间)  // 计算两个时间的间隔
        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
        LocalDateTime localDateTime1 = LocalDateTime.now();
        Duration between = Duration.between(localDateTime, localDateTime1);
        System.out.println(between);  // PT10395H37M33.329607S  表示间隔了1039小时 37分钟 33.329607秒
        // public long toSeconds()  获得此时间间隔的秒
        System.out.println(between.toSeconds());  // 37424388
        // public int toMillis()  获得此时间间隔的毫秒
        System.out.println(between.toMillis()); // 37424388423
        // public int toNanos()  获得此时间间隔的纳秒
        System.out.println(between.toNanos());  // 37424388423323900

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值