【java基础】07-常用API介绍02

1.时间日期类-概述

时间日期类小结
要点:

毫秒值:比秒还小的时间单位;1= 1000 毫秒; 
    	扩展:     1毫秒 = 1000 微秒; 
				1微秒 = 1000 纳秒;
毫秒值也是一个瞬时的时间值,这个值是距离一个时间原点的值;
    
时间原点:在计算机中,时间原点是1970-1-1 00:00:00,我们中国在东八区,所以,我们的时间原点是 1970-1-1 08:00:00 (了解) 

毫秒值的作用: 可以对日期和时间进行计算..

对于我们来说, 时间原点: 1970-01-01  08:00:00

2.时间日期类-Date构造方法

Date构造方法

3.时间日期类-Date成员方法

Date成员方法
时间成员方法Demo

4.时间日期类-SimpleDateFormat

SDF1
SDF2
SDF3
SDF4
要点:

DateFormat:抽象类
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String format(Date d):格式化
Date parse(String str):解析

//多态写法
new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

String format(Date);

Date parse(String);

5.时间日期类-练习

SDF练习1
SDF练习2
SDF练习3


6.Date小结

构造方法
    new Date()
    new Date(long 毫秒值)
成员方法
    setTime(毫秒值)
    long getTime()

7.SimpleDateFormat小结

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//格式化
String format = df.format(new Date());

String s = "2020-11-11 15:55:54";
//解析
Date parse = df.parse(s);

8.时间日期类-JDK8的体验

SDF-JDK8-1
新方式:
SDF-JDK8-2

9.JDK8时间类-获取时间对象

JDK8-获取时间对象-1
LocalDateTime创建方法
LDT-Demo1
LDT-Demo2

public class JDK8DateDemo2 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
        System.out.println(localDateTime);
    }
}

注意:
LocalTime,LocalDate,LocalDateTime记录了一个日期,可以计算,可以获取日期中的年,月,日,星期等,没有时区信息等附加信息, 所以不能表示时间线上的瞬时.
注意范围: 月,日,时,分,秒 范围(1-12,0-23,0-59)

10.JDK8时间类-获取时间中的每个值

LDT获取时间中的每个值

public class JDK8DateDemo3 {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 20);
        //public int getYear()           获取年
        int year = localDateTime.getYear();
        System.out.println("年为" +year);
        //public int getMonthValue()     获取月份(1-12)
        int month = localDateTime.getMonthValue();
        System.out.println("月份为" + month);

        Month month1 = localDateTime.getMonth();
//        System.out.println(month1);

        //public int getDayOfMonth()     获取月份中的第几天(1-31)
        int day = localDateTime.getDayOfMonth();
        System.out.println("日期为" + day);

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

11.JDK8时间类-转换方法

LDT转换方法

public class JDK8DateDemo4 {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.of(2020, 12, 12, 8, 10, 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);
    }
}

12.JDK8时间类-格式化和解析

LocalDateTime类

方法名说明
public String format (指定格式)把一个LocalDateTime格式化成为一个字符串
public static LocalDateTime parse (准备解析的字符串, 解析格式)把一个日期字符串解析成为一个LocalDateTime对象
public static DateTimeFormatter ofPattern(String pattern)使用指定的日期模板获取一个日期格式化器DateTimeFormatter对象
public class JDK8DateDemo5 {
    public static void main(String[] args) {
        //method1();
        //method2();
    }

    private static void method2() {
        //public static LocalDateTime parse (准备解析的字符串, 解析格式) 把一个日期字符串解析成为一个LocalDateTime对象
        String s = "2020年11月12日 13:14:15";
        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, 11, 12, 13, 14, 15);
        System.out.println(localDateTime);
        //public String format (指定格式)   把一个LocalDateTime格式化成为一个字符串
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String s = localDateTime.format(pattern);
        System.out.println(s);
    }
}

13.JDK8时间类-plus系列的方法

LDT增加或减少时间的方法

/**
 * JDK8 时间类添加或者减去时间的方法
 */
public class JDK8DateDemo6 {
    public static void main(String[] args) {
        //public LocalDateTime plusYears (long years)   添加或者减去年

        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
        //LocalDateTime newLocalDateTime = localDateTime.plusYears(1);
        //System.out.println(newLocalDateTime);

        LocalDateTime newLocalDateTime = localDateTime.plusYears(-1);
        System.out.println(newLocalDateTime);
    }
}

要点:

方便,可以直接对年,,,,,,星期,进行 +-    (正数是加, 负数是减 )

​		返回新的对象,原来的LocalDateTime对象变吗?
    	  会变		
​        如果要添加年,,3??
				链式编程
				localDateTime.plusMonths(1).plusYears(1).plusDays(1);

14. JDK8时间类-minus系列的方法

LDT减少或者增加时间的方法

要点:
minus是减,plus是加


/**
 * JDK8 时间类减少或者添加时间的方法
 */
public class JDK8DateDemo7 {
    public static void main(String[] args) {
        //public LocalDateTime minusYears (long years)  减去或者添加年
        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
        //LocalDateTime newLocalDateTime = localDateTime.minusYears(1);
        //System.out.println(newLocalDateTime);

        LocalDateTime newLocalDateTime = localDateTime.minusYears(-1);
        System.out.println(newLocalDateTime);

    }
}

15.JDK8时间类-with系列的方法

LDT修改方法

/**
 * JDK8 时间类修改时间
 */
public class JDK8DateDemo8 {
    public static void main(String[] args) {
        //public LocalDateTime withYear(int year)   修改年
        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 13, 14, 15);
       // LocalDateTime newLocalDateTime = localDateTime.withYear(2048);
       // System.out.println(newLocalDateTime);

        LocalDateTime newLocalDateTime = localDateTime.withMonth(20);
        System.out.println(newLocalDateTime);

    }
}

要点
withXXX(),修改, 返回新对象,原始对象本身不变!

16.JDK8时间类-时间间隔对象

Period

/**
 *  计算两个时间的间隔
 */
public class JDK8DateDemo9 {
    public static void main(String[] args) {
        //public static Period between(开始日期,结束日期)  计算两个"日期"的间隔

        LocalDate localDate1 = LocalDate.of(2020, 1, 1);
        LocalDate localDate2 = LocalDate.of(2048, 12, 12);
        Period period = Period.between(localDate1, localDate2);
        System.out.println(period);//P28Y11M11D

        //public int getYears()         获得这段时间的年数
        System.out.println(period.getYears());//28
        //public int getMonths()        获得此期间的月数
        System.out.println(period.getMonths());//11
        //public int getDays()          获得此期间的天数
        System.out.println(period.getDays());//11

        //public long toTotalMonths()   获取此期间的总月数
        System.out.println(period.toTotalMonths());//347

    }
}

Duration

/**
 *  计算两个时间的间隔
 */
public class JDK8DateDemo10 {
    public static void main(String[] args) {
        //public static Duration between(开始时间,结束时间)  计算两个“时间"的间隔

        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 1, 1, 13, 14, 15);
        LocalDateTime localDateTime2 = LocalDateTime.of(2020, 1, 2, 11, 12, 13);
        Duration duration = Duration.between(localDateTime1, localDateTime2);
        System.out.println(duration);//PT21H57M58S
        //public long toSeconds()	       获得此时间间隔的秒
        System.out.println(duration.toSeconds());//79078
        //public int toMillis()	           获得此时间间隔的毫秒
        System.out.println(duration.toMillis());//79078000
        //public int toNanos()             获得此时间间隔的纳秒
        System.out.println(duration.toNanos());//79078000000000
    }
}

要点

Period:周期,大间隔 以年,月和日为单位

LocalDate localDate1 = LocalDate.of(2020, 1, 1);
        LocalDate localDate2 = LocalDate.of(2048, 12, 12);
        Period period = Period.between(localDate1, localDate2);
        System.out.println(period);//P28Y11M11D

Duration:小间隔 以秒和纳秒为单位

LocalDateTime localDateTime1 = LocalDateTime.of(2020, 1, 1, 13, 14, 15);
        LocalDateTime localDateTime2 = LocalDateTime.of(2020, 1, 1, 14, 15, 16);
        Duration duration = Duration.between(localDateTime1, localDateTime2);
        System.out.println("间隔小时" + duration.toHours());//间隔小时1
        System.out.println("间隔分钟" + duration.toMinutes());//间隔分钟61
        System.out.println("间隔秒" + duration.toSeconds());//间隔秒3661

17.JDK8时间类-小结

时间类小结1
时间类小结2
要点:

Date相关方法
    Date()
    Date(long)
    setTime(long)
    getTime()
    
SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    格式化:Date对象--->字符串
        	String format(Date d)
	解析:字符串----->Date对象
        	Date parse(String s)

1.为什么使用LocalDateTime?
    	简洁,优雅,健壮,不可变,线程安全
2.如何得到 LocalDateTime 对象?
    	LocalDateTime.now()
    	LocalDateTime.of(2021,1,2,12,13,15);
3.获取年,,,,,,星期?
4.计算
    	LocalDateTime plusYears(2/-2);
5.修改月,,,,秒
    	LocalDateTime withYear()
6.格式化,解析
    	格式化:LocalDateTime--->字符串
         	String format(DateTimeFormatter);
		解析:字符串----->LocalDateTime 对象
            static LocalDateTime parse(String,DateTimeFormatter)
 
 Period: 大间隔
 Duration:小间隔
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值