第五章.日期类

第五章.Date日期类

1.Date类的介绍

1.概述:类 Date 表示特定的瞬间,精确到毫秒

2.地理常识:

a.北京经纬度:东经116 北纬39.56

b.时区:北京在东八区 -> 一个时区差一个小时

c.分南北半球:赤道

d.0度经线:本初子午线

e.毫秒: 1000毫秒 = 1秒

f.北京温度带:

温带大陆性季风气候->四季分明

g.时间原点:1970年1月1日 0时0分0秒 -> 我国北京时间是东八区的时间,比时间原点差8个小时

2.Date类的使用

1.构造:

Date() -> 分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。

Date(long time) -> 分配 Date 对象并初始化此对象-> 从时间原点开始算的->传递毫秒值

public class Demo01Date {
public static void main(String[] args) {
//Date() -> 分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。
Date date = new Date();
System.out.println("date = " + date);
//Date(long time) -> 分配 Date 对象并初始化此对象-> 从时间原点开始算的->传递毫秒
值
Date date1 = new Date(1000L);
System.out.println("date1 = " + date1);
}
}

3.Date类的常用方法

long getTime()-> 获取当前系统时间毫秒值
void setTime(long time)->设置时间,传递毫秒值,从时间原点开始计算

public class Demo02Date {
    public static void main(String[] args) {
        Date date = new Date();
        //long getTime()-> 获取当前系统时间毫秒值
        System.out.println("date.getTime() = " + date.getTime());
        //void setTime(long time)->设置时间,传递毫秒值,从时间原点开始计算
        date.setTime(1000L);
        System.out.println("date = " + date);
    }
}

第六章.Calendar日历类

1.Calendar介绍

1.概述:日历类,是一个抽象类

2.获取:

static Calendar getInstance()

3.月份问题:

国外: 0 1 2 3 4 5 6 7 8 9 10 11

国内: 1 2 3 4 5 6 7 8 9 10 11 12

2.用方法:

int get(int field) ->返回给定日历字段的值

void set(int field, int value) :将给定的日历字段设置为指定的值

void add(int field, int amount) :根据日历的规则,为给定的日历字段添加或者减去指定的时间

Date getTime():将Calendar转成Date对象

field:代表的是日历字段-> 年 月 日 星期等,都是静态的

public class Demo01Calendar {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        //int get(int field) ->返回给定日历字段的值
        int year = calendar.get(Calendar.YEAR);
        System.out.println("year = " + year);
        int month = calendar.get(Calendar.MONTH);
        System.out.println("month = " + month);
        //void set(int field, int value) :将给定的日历字段设置为指定的值
        //calendar.set(Calendar.YEAR,2000);
        //System.out.println(calendar.get(Calendar.YEAR));
        //void add(int field, int amount) :根据日历的规则,为给定的日历字段添加或者减去
        指定的时间量
        calendar.add(Calendar.YEAR,-1);
        System.out.println(calendar.get(Calendar.YEAR));
        //Date getTime():将Calendar转成Date对象
        Date date = calendar.getTime();
        System.out.println("date = " + date);
    }
}

3.扩展方法:

void set(int year, int month, int date) -> 直接设置年月日

需求:键盘录入一个年份,判断这一年是闰年,还是平年

步骤:

1.创建Scanner对象,调用nextInt方法,录入一个年份 year

2.获取Calendar对象

3.调用set方法,设置年月日

set(year,2,1) -> 设置2月1日相当于3月1日,因为外国是从0开始

4.将1日往前-1天就变成了2月的最后一天

5.获取day的字段,比较是否为28,如果是28平年

public class Demo02Calendar {
    public static void main(String[] args) {
        //1.创建Scanner对象,调用nextInt方法,录入一个年份 year
        Scanner sc = new Scanner(System.in);
        System.out.println("请您输入一个年份:");
        int year = sc.nextInt();
        //2.获取Calendar对象
        Calendar calendar = Calendar.getInstance();
        //3.调用set方法,设置年月日
        //set(year,2,1) -> 设置2月1日相当于3月1日,因为外国是从0开始
        calendar.set(year,2,1);
        //4.将1日往前-1天就变成了2月的最后一天
        calendar.add(Calendar.DATE,-1);
        //5.获取day的字段,比较是否为28,如果是28平年
        int day = calendar.get(Calendar.DATE);
        if (day==28){
            System.out.println("今年是平年");
        }else{
            System.out.println("今年是闰年");
        }
    }
}

第七章.SimpleDateFormat日期格式化类

1.SimpleDateFormat介绍

1.概述:DateFormat是一个抽象类

使用其子类:SimpleDateFormat

2.作用:可以按照指定的格式将日期格式化

3.创建:

SimpleDateFormat(String pattern)

pattern:指定的格式

yyyy-MM-dd HH:mm:ss -> 连接符可以改变,但是字母不能变

2.SimpleDateFormat常用方法

String format(Date date) -> 将Date对象按照指定格式转成String

Date parse(String source) -> 将符合指定格式的String转成Date对象

public class Demo01DateFormat {
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String format(Date date) -> 将Date对象按照指定格式转成String
        String time = sdf.format(date);
        System.out.println("time = " + time);
        System.out.println("====================================");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeDate = "2000-10-10 10:10:10";
        //Date parse(String source) -> 将符合指定格式的String转成Date对象
        Date date1 = sdf1.parse(timeDate);
        System.out.println("date1 = " + date1);
    }
}

第八章.JDK8新日期类

1. LocalDate 本地日期

1.1.获取LocalDate对象

1.LocalDate概述:LocalDate是一个不可变的日期时间对象,表示日期,通常被视为年月日
2.获取:
a.static LocalDate now()
b.static LocalDate of(int year, int month, int dayOfMonth)

public class Demo01Date {
    public static void main(String[] args) {
        //a.static LocalDate now()
        LocalDate localDate1 = LocalDate.now();
        System.out.println("localDate1 = " + localDate1);
        //b.static LocalDate of(int year, int month, int dayOfMonth)
        LocalDate localDate2 = LocalDate.of(2000, 12, 12);
        System.out.println("localDate2 = " + localDate2);
    }
}

1.LocalDateTime概述:LocalDateTime是一个不可变的日期时间对象,代表日期时间,通常被视为

年 - 月 - 日 - 时 - 分 - 秒

2.获取:

a.static LocalDateTime now()

b.static LocalDateTime of(int year, int month, int dayOfMonth, int hour,

int minute, int second)

public class Demo02Date {
    public static void main(String[] args) {
        //a.static LocalDateTime now()
        LocalDateTime localDateTime1 = LocalDateTime.now();
        System.out.println("localDateTime1 = " + localDateTime1);
        //static LocalDateTime of(int year, int month, int dayOfMonth, int
        hour, int minute, int second)
        LocalDateTime localDateTime2 =
        LocalDateTime.of(2000, 12, 12, 12, 12, 12);
        System.out.println("localDateTime2 = " + localDateTime2);
    }
}

1.2.获取日期字段的方法 : 名字是get开头

int getYear()->获取年份

int getMonthValue()->获取月份

int getDayOfMonth()->获取月中的第几天

public class Demo03Date {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        System.out.println("localDate.getYear() = " + localDate.getYear());
        System.out.println("localDate.getMonthValue() = " +
                           localDate.getMonthValue());
        System.out.println("localDate.getDayOfMonth() = " +
                           localDate.getDayOfMonth());
    }
}

1.3.设置日期字段的方法 : 名字是with开头

LocalDate withYear(int year):设置年份

LocalDate withMonth(int month):设置月份

LocalDate withDayOfMonth(int day):设置月中的天数

public class Demo04Date {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        // System.out.println("localDate = " + localDate);
        // System.out.println("=========================");
        // LocalDate localDate1 = localDate.withYear(2000);
        // System.out.println("localDate1 = " + localDate1);
        // System.out.println("=========================");
        // LocalDate localDate2 = localDate1.withMonth(12);
        // System.out.println("localDate2 = " + localDate2);
        // System.out.println("=========================");
        // LocalDate localDate3 = localDate2.withDayOfMonth(12);
        // System.out.println("localDate3 = " + localDate3);
        System.out.println("=================================");
        LocalDate localDate1 =
        localDate.withYear(2000).withMonth(12).withDayOfMonth(12);
        System.out.println("localDate1 = " + localDate1);
    }
}

1.4.日期字段偏移

设置日期字段的偏移量,方法名plus开头,向后偏移

设置日期字段的偏移量,方法名minus开头,向前偏移

public class Demo05Date {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        //LocalDate localDate1 = localDate.plusYears(1);
        //System.out.println(localDate1.getYear());
        System.out.println("=======================");
        LocalDate localDate1 = localDate.minusYears(1);
        System.out.println(localDate1.getYear());
    }
}

2.Period和Duration类

2.1 Period 计算日期之间的偏差

方法:

static Period between(LocalDate d1,LocalDate d2):计算两个日期之间的差值

getYears()->获取相差的年

getMonths()->获取相差的月

getDays()->获取相差的天

public class Demo06Date {
    public static void main(String[] args) {
        LocalDate localDate1 = LocalDate.of(2022, 12, 12);
        LocalDate localDate2 = LocalDate.of(2021, 11, 11);
        Period period = Period.between(localDate2, localDate1);
        //getYears()->获取相差的年
        System.out.println("period.getYears() = " + period.getYears());
        //getMonths()->获取相差的月
        System.out.println("period.getMonths() = " + period.getMonths());
        //getDays()->获取相差的天
        System.out.println("period.getDays() = " + period.getDays());
    }
}

2.2 Duration计算时间之间的偏差

1.static Duration between(Temporal startInclusive, Temporal endExclusive) -> 计

算时间差

2.参数:Temporal接口

实现类有:LocalDate,LocalDateTime

3.注意:

Duration计算时间偏差,计算的是精确时间,所以当我们调用between的时候参数就不应该传递

LocalDate,应该传递能操作精确时间的LocalDateTime

4.利用Duration获取相差的时分秒 -> to开头

toDays():获取相差天数

toHours():获取相差小时

toMinutes():获取相差分钟

toMillis():获取相差秒(毫秒)

public class Demo07Date {
    public static void main(String[] args) {
        LocalDateTime localDate1 = LocalDateTime.of(2022, 12, 12,12,12,12);
        LocalDateTime localDate2 = LocalDateTime.of(2021, 11, 11,11,11,11);
        Duration duration = Duration.between(localDate2, localDate1);
        //toDays():获取相差天数
        System.out.println("duration.toDays() = " + duration.toDays());
        //toHours():获取相差小时
        System.out.println("duration.toHours() = " + duration.toHours());
        //toMinutes():获取相差分钟
        System.out.println("duration.toMinutes() = " + duration.toMinutes());
        //toMillis():获取相差秒(毫秒)
        System.out.println("duration.toMillis() = " + duration.toMillis());
    }
}

如果计算年月日用Period

如果计算精确时间(时分秒)用Duration

3.DateTimeFormatter日期格式化类

1.概述:日期格式化类

2.获取:

static DateTimeFormatter ofPattern(String pattern)

pattern:指定的格式

3.常用方法:

String format(TemporalAccessor temporal) ->将日期对象按照指定格式转成String

TemporalAccessor是一个接口,是Temporal接口的子接口实现类有:LocalDate,LocalDateTime

TemporalAccessor parse(CharSequence text) -> 将符合格式的字符串日期转成日期对象

返回值:TemporalAccessor接口,实现类有:LocalDate LocalDateTime

static LocalDateTime from(TemporalAccessor temporal) -> 将 TemporalAccessor转

成LocalDateTime

public class Demo08Date {
    public static void main(String[] args) {
        //format();
        parse();
    }
    /**
* TemporalAccessor parse(CharSequence text) -> 将符合格式的字符串日期转成日期
对象
* 返回值:TemporalAccessor接口,实现类有:LocalDate LocalDateTime
*
* static LocalDateTime from(TemporalAccessor temporal) -> 将
TemporalAccessor转成LocalDateTime
*/
    private static void parse() {
        String time = "2000-10-10 10:10:10";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd
                                                            HH:mm:ss");
        TemporalAccessor temporalAccessor = dtf.parse(time);
        System.out.println("temporalAccessor = " + temporalAccessor);
        LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
        System.out.println("localDateTime = " + localDateTime);
    }
    /**
* String format(TemporalAccessor temporal) ->将日期对象按照指定格式转成String
* TemporalAccessor是一个接口,是Temporal接口的子接口
* 实现类有:LocalDate,LocalDateTime
*/
    private static void format() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd
                                                            HH:mm:ss");
        String time = dtf.format(localDateTime);
        System.out.println("time = " + time);
    }
}

小结

以上关于时间的基本操作类和方法,后续会继续记录。

相关推荐

  1. Linux系统日志管理

    2024-06-17 07:12:04       24 阅读
  2. 实战

    2024-06-17 07:12:04       35 阅读
  3. 饥饿

    2024-06-17 07:12:04       38 阅读
  4. Collections

    2024-06-17 07:12:04       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 07:12:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 07:12:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 07:12:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 07:12:04       20 阅读

热门阅读

  1. Kafka内外网分流配置listeners和advertised.listeners

    2024-06-17 07:12:04       5 阅读
  2. 【杂记-浅谈以太网IP数据帧】

    2024-06-17 07:12:04       6 阅读
  3. Android.mk的用法

    2024-06-17 07:12:04       4 阅读
  4. 2024.6.15 英语六级 经验与复盘

    2024-06-17 07:12:04       8 阅读
  5. 速盾:网站如何加上cdn?

    2024-06-17 07:12:04       6 阅读
  6. 22.2 正则表达式-数据验证、数据变换

    2024-06-17 07:12:04       6 阅读
  7. golang实现循环队列

    2024-06-17 07:12:04       8 阅读
  8. github基础使用

    2024-06-17 07:12:04       6 阅读
  9. QSharedMemory使用详解

    2024-06-17 07:12:04       7 阅读
  10. Qt 实战(4)信号与槽 | 4.3、信号连接信号

    2024-06-17 07:12:04       6 阅读