JavaSE-日期相关类

1- jdk8之前的日期相关类

1.1 System类

1.1.1 基本概念
  • Java.lang.System类中提供了一些有用的类字段和方法。
  • System代表了当前系统。
1.1.2 常用方法
方法声明功能介绍
public static long currentTimeMillis( )返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
public static void exit(int status)终止当前正在运行的 Java 虚拟机0代表正常结束,非0代表异常结束!!
public static void arraycopy(Object src , int srcPos ,Object dest, int destPos,int length)数组拷贝,数组复制。

1)代码示例

package cn.guardwhy_05;

import java.text.SimpleDateFormat;
import java.util.Arrays;

public class SystemDemo08 {
    public static void main(String[] args) {
        // 得到此刻时间毫秒值
        long time = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        // 格式化时间:2020/09/17 19:08:10
        System.out.println("格式化时间:" + sdf.format(time));    

        // 终止当前正在运行的Java虚拟机。参数用作状态码.根据惯例,非0的状态码表示异常终止。
        // System.exit(0);

        // 数组拷贝
        int[] arr1 = new int[]{10, 20, 30, 40, 50, 60};
        int[] arr2 = new int[6];

        /**
         * arraycopy(Object src,   参数一:原数组
         *           int  srcPos,  参数二:从哪个元素索引位置开始复制。
         *           Object dest,  参数三:目标数组
         *           int destPos,  参数四:复制到目标数组的哪个位置开始!
         *           int length)   参数五:复制多少个元素
         */
        System.arraycopy(arr1, 0, arr2, 0, 5);
        System.out.println("arr2数组元素:" + Arrays.toString(arr2));

        System.out.println("程序结束....");
    }
}

2) 执行结果

1.2 Date类

1.2.1 基本概念

java.util.Date类主要用于描述特定的瞬间,也就是年月日时分秒,可以精确到毫秒。

1.2.2 常用的方法
方法声明功能介绍
Date()使用无参的方式构造对象,也就是当前系统时间
Date(long date)根据参数指定毫秒数构造对象, 参数为距离1970年1月1日0时0分0秒的毫秒数
long getTime()获取调用对象距离1970年1月1日0时0分0秒的毫秒数
void setTime(long time)设置调用对象为距离基准时间time毫秒的时间点

1)代码示例

package cn.guardwhy_05;

import java.util.Date;

/**
Date类代表了:当前系统的当前此刻日期信息对象。

Date类的API详情:
     a.包 java.util.Date 需要导入包的。
     b.常用构造器:
     -- public Date()
     -- public Date(long time)
     c.方法
     -- public long getTime():返回从1970-01-01 00:00:00走到此刻的总的时间毫秒值。
     1000ms = 1s
Java记录时间的两种方式:
     1.可以直接用Date日期类对象记录:Date d = new Date();
     2.可以直接使用时间毫秒值:从1970-01-01 00:00:00走到此刻的总的时间毫秒值

总结:
     当前此刻日期对象:Date
     表示此刻时间的两种形式:
     -- Date d = new Date()
     -- long time = d.getTime(); 使用时间毫秒值
 */
public class DateDemo01 {
    public static void main(String[] args) {
        // 1.创建一个日期对象
        Date d = new Date();
        // 输出当前系统此刻日期时间信息
        System.out.println(d);  // hu Sep 17 15:20:52 CST 2020

        // 2.获取当前系统此刻时间毫秒值
        long time = d.getTime();
        System.out.println(time);   // 1600327441960
    }
}

2) 时间运算

package cn.guardwhy_05;

import java.util.Date;

/**
 时间毫秒值可以用于做时间的运算。
 */
public class DateDemo02 {
    public static void main(String[] args) {
        // 1.提取此刻当前时间的毫秒值
        Date d1 = new Date();
        long startTime = d1.getTime();

        for(int i=0; i<1000; i++){
            System.out.println(i);
        }

        //2.再提取执行完毕以后的当前时间的时间毫秒值
        Date d2 = new Date();
        long endTime = d2.getTime();

        // 3.输出性能时间
        System.out.println("耗时:" + (endTime - startTime) / 1000.0); // 耗时:0.006
    }
}

3) 有参数构造器

package cn.guardwhy_05;

import java.util.Date;

/**
 Date日期类的有参数构造器的使用。
 public Date(long time): 可以把时间毫秒值转换成日期对象。

 Date日期对象 -> getTimer() -> 时间毫秒值
 时间毫秒值  -> public Date(long time) -> Date日期对象

 总结:
    public Date(long time): 可以把时间毫秒值转换成日期对象。
 */
public class DateDemo03 {
    public static void main(String[] args) {
        // 1.拿到此刻当前日期对象
        Date d1 = new Date();
         // d1的日期对象:Thu Sep 17 15:45:13 CST 2020
        System.out.println("d1的日期对象:" + d1);   

        // 2.拿到此刻的时间毫秒值
        long time = d1.getTime();
        // 3.往后走121s
        time += 121 * 1000;

        // 4.把时间毫秒值转换成日期对象输出
        Date d2 = new Date(time);
        // d2的日期对象:Thu Sep 17 15:47:14 CST 2020
        System.out.println("d2的日期对象:" + d2);    
    }
}

1.3 SimpleDateFormat类

1.3.1 基本概念

java.text.SimpleDateFormat类主要用于实现日期和文本之间的转换。

1.3.2 常用的方法
方法声明功能介绍
SimpleDateFormat()使用无参方式构造对象
SimpleDateFormat(String pattern)根据参数指定的模式来构造对象,模式主要有: y-年 M-月 d-日 H-时 m-分 s-秒
final String format(Date date)用于将日期类型转换为文本类型
Date parse(String source)用于将文本类型转换为日期类型

1) 格式化时间

package cn.guardwhy_05;

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

/**
 DateFormat日期格式化

 DateFormat的作用:
     1.可以把日期对象或者时间毫秒值转换成我们喜欢的字符串时间格式。
     2.也可以把字符串时间解析成日期对象。

 DateFormat:
     包:java.text
     这个类是一个抽象的父类,不能直接使用,我们要用它的子类:SimpleDateFormat

 SimpleDateFormat(简单日期格式化对象):
     a.构造器:public SimpleDateFormat(String pattern):定制格式
     b.方法
     -- public String format(Date d):把日期对象转换成格式化的字符串时间返回
     -- public String format(Object time):把时间毫秒值转换成格式化的字符串时间返回
     -- public Date parse(String time):把字符串的时间解析成日期对象。
 */
public class SimpleDateFormatDemo04 {
    public static void main(String[] args) {
        // 1.得到系统此刻时间
        Date d1 = new Date();
        // 当前系统此刻日期时间:Thu Sep 17 16:03:44 CST 2020
        System.out.println("当前系统此刻日期时间:" + d1); 
        // 2.创建一个简单日期格式化对象用于格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss EEE a");
        // 3.把日期格式化成字符串
        String rs = sdf.format(d1);
        // 格式化时间:2020年09月17 16:06:46 星期四 下午
        System.out.println("格式化时间:" + rs);  
    }
}

1.4 Calendar类

1.4.1 基本概念
  • java.util.Calender类主要用于描述特定的瞬间,取代Date类中的过时方法实现全球化。
  • 该类是个抽象类,因此不能实例化对象,其具体子类针对不同国家的日历系统,其中应用最广泛的
    是GregorianCalendar(格里高利历),对应世界上绝大多数国家/地区使用的标准日历系统。
1.4.2 常用的方法
方法声明功能介绍
static Calendar getInstance()用于获取Calendar类型的引用
void set(int year, int month, int date, int hourOfDay, int<minute, int second)用于设置年月日时分秒信息
Date getTime()用于将Calendar类型转换为Date类型
void set(int field, int value)设置指定字段的数值
void add(int field, int amount)向指定字段增加数值

1) 代码示例

package cn.guardwhy_05;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 Calendar代表的是当前系统此刻时间对应的日历对象。是一个抽象类。

 Calendar创建日历对象的方式:
    -- public static Calendar getInstance():直接返回一个日期对象。
 Calendar的方法:
     -- public int get(int field):根据参数取日期信息
     -- public Date getTime() : 拿日期的此刻日期对象
     -- public long getTimeInMillis() : 拿此刻时间毫秒值
     -- public void add(int field, int amount):让日历中的某个信息多走一个值!!
 */
public class CalendarDemo07 {
    public static void main(String[] args) {
        // 1.创建日历对象,得到一个系统的日历对象
        Calendar c1 = Calendar.getInstance();
        System.out.println("日历对象:" + c1);

        // 获取年
        int year = c1.get(Calendar.YEAR);
        System.out.println("年:" + year);
        // 天数
        int days = c1.get(Calendar.DAY_OF_YEAR);
        System.out.println("天数:" + days);

        // 获取日历当前日期对象
        Date d = c1.getTime();
        System.out.println("当前日期对象:" + d);

        // 得到此刻时间毫秒值
        long time = c1.getTimeInMillis();
        System.out.println("此刻时间毫秒值:" + time);

        // 往后走多少时间信息。
        // 问 62天的日期,让日期中一年中的第几天往后多走62天,往后翻62页.
        c1.add(Calendar.DAY_OF_YEAR, 62);
        SimpleDateFormat sdf = new SimpleDateFormat("yyy/MM/dd HH:mm:ss");
        System.out.println("时间信息:" + sdf.format(c1.getTimeInMillis()));
    }
}

2) 执行结果

2- jdk8中的日期相关类

2.1 jdk8日期类的概述

Java 8通过发布新的Date-Time API来进一步加强对 日期与时间的处理。

  • java.time包:该包日期/时间API的基础包。
  • java.time.chrono包:该包提供对不同日历系统的访问。
  • java.time.format包:该包能够格式化和解析日期时间对象。
  • java.time.temporal包:该包包含底层框架和扩展特性。
  • java.time.zone包:该包支持不同时区以及相关规则的类。

2.2 LocalDate类

2.2.1 基本概念

java.time.LocalDate类主要用于描述年-月-日格式的日期信息,该类不表示时间和时区信息。

2.2.2 常用的方法
方法声明功能介绍
static LocalDate now( )在默认时区中从系统时钟获取当前日期

2.3 LocalTime类

2.3.1 基本概念

java.time.LocalTime 类主要用于描述时间信息,可以描述时分秒以及纳秒。

2.3.2 常用的方法
方法声明功能介绍
static LocalTime now( )从默认时区的系统时间中获取当前时间
static LocalTime now(ZoneId zone)获取指定时区的当前时间

2.4 LocalDateTime类

2.4.1 基本概念

java.time.LocalDateTime类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12-03 T10:15:30。

2.4.2 常用的方法
方法声明功能介绍
static LocalDateTime now()从默认时区的系统时间中获取
当前日期时间
static LocalDateTime of(int year, int month, int
dayOfMonth, int hour, int minute, int second)
根据参数指定的年月日时分秒
信息来设置日期时间
int getYear()获取年份字段的数值
int getMonthValue()获取1到12之间的月份字段
int getDayOfMonth()获取日期字段

1) 代码示例

package cn.guardwhy_07;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class LocalDateTimeTest {
    public static void main(String[] args) {
        // 1.获取当前日期信息并打印
        LocalDate now = LocalDate.now();
        System.out.println("获取到的当前日期是:" + now);
        // 2.获取当前时间信息并打印
        LocalTime now1 = LocalTime.now();
        System.out.println("获取到的当前时间是:" + now1);
        // 3.获取当前日期时间信息并打印,使用最多
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println("获取到的当前日期时间是:" + now2);

        System.out.println("-------------------------------------------------------");
        // 4.使用参数指定的年月日时分秒信息来获取对象并打印
        LocalDateTime of = LocalDateTime.of(2020, 8, 9, 10, 8, 8);
        System.out.println("指定的日期时间是:" + of); // 自动调用toString方法
        System.out.println("获取到的年是:" + of.getYear()); // 2020
        System.out.println("获取到的月是:" + of.getMonthValue()); // 8
        System.out.println("获取到的日是:" + of.getDayOfMonth()); // 9
        System.out.println("获取到的时是:" + of.getHour()); // 10
        System.out.println("获取到的分是:" + of.getMinute()); // 8
        System.out.println("获取到的秒是:" + of.getSecond()); // 8

        System.out.println("-------------------------------------------------------");
        // 5.实现特征的设置并打印
        /* 与String类型相似,调用对象本身的数据内容不会改变,
        返回值相当于创建了一个新的对象,由此证明了不可变性。
        */
        LocalDateTime localDateTime = of.withYear(2020);
        System.out.println("localDateTime = " + localDateTime); // 2020-08-08T20:08:08
        System.out.println("of = " + of); // 2020-08-08T20:08:08
        LocalDateTime localDateTime1 = localDateTime.withMonth(12);
        System.out.println("localDateTime1 = " + localDateTime1); // 2020 12 8 20 8 8

        System.out.println("-------------------------------------------------------");
        // 6.实现特征的增加并打印
        LocalDateTime localDateTime2 = localDateTime1.plusDays(2);
        System.out.println("localDateTime2 = " + localDateTime2); // 2020 12 10 20 8 8
        System.out.println("localDateTime1 = " + localDateTime1); // 2020 12 8 20 8 8
        LocalDateTime localDateTime3 = localDateTime2.plusHours(3);
        System.out.println("localDateTime3 = " + localDateTime3); // 2020 12 10 23 8 8

        System.out.println("-------------------------------------------------------");
        // 7.实现特征的减少并打印
        LocalDateTime localDateTime4 = localDateTime3.minusMinutes(1);
        System.out.println("localDateTime4 = " + localDateTime4); // 2020 12 10 23 7 8
        System.out.println("localDateTime3 = " + localDateTime3); // 2020 12 10 23 8 8
        LocalDateTime localDateTime5 = localDateTime4.minusSeconds(3);
        System.out.println("localDateTime5 = " + localDateTime5); // 2020 12 10 23 7 5

    }
}

2.5 Instant类

2.5.1 基本概念

java.time.Instant类主要用于描述瞬间的时间点信息。

2.5.2 常用的方法
方法声明功能介绍
static Instant now()从系统时钟上获取当前时间
OffsetDateTime
atOffset(ZoneOffset offset)
将此瞬间与偏移量组合以创建偏移日期时间
static Instant ofEpochMilli(long
epochMilli)
根据参数指定的毫秒数来构造对象,参数为距离1970年1月1
日0时0分0秒的毫秒数
long toEpochMilli()获取距离1970年1月1日0时0分0秒的毫秒数

1) 代码示例

package cn.guardwhy_07;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class InstantTest {

    public static void main(String[] args) {

        // 1.使用Instant类来获取当前系统时间
        Instant now = Instant.now();
        System.out.println("获取到的当前时间为:" + now);

        // 2.加上时区所差的8个小时
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println("偏移后的日期时间为:" + offsetDateTime);

        System.out.println("--------------------------------------------------------");
        // 3.获取当前调用对象距离标准基准时间的毫秒数
        long time = now.toEpochMilli();
        System.out.println("获取到的毫秒差为:" + time);

        // 4.根据参数指定的毫秒数来构造对象
        Instant instant = Instant.ofEpochMilli(time);
        System.out.println("根据参数指定的毫秒数构造出来的对象为:" + instant);
    }
}

2) 执行结果

2.6 DateTimeFormatter类

2.6.1 基本概念

java.time.format.DateTimeFormatter类主要用于格式化和解析日期。

2.6.2 常用的方法
方法声明功能介绍
static DateTimeFormatter ofPattern(String pattern)根据参数指定的模式来获取对象
String format(TemporalAccessor temporal)将参数指定日期时间转换为字符串
TemporalAccessor parse(CharSequence text)将参数指定字符串转换为日期时间

1) 代码示例

package cn.guardwhy_07;

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

public class DateTimeFormatterTest {

    public static void main(String[] args) {

        // 1.获取当前系统的日期时间并打印
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);

        // 2.按照指定的格式准备一个DateTimeFormatter类型的对象
        DateTimeFormatter dateTimeFormatter = 
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 3.实现日期时间向字符串类型的转换并打印
        String str = dateTimeFormatter.format(now);
        System.out.println("调整格式后的结果是:" + str);
        // 4.实现字符串类型到日期时间类型的转换并打印
        TemporalAccessor parse = dateTimeFormatter.parse(str);
        System.out.println("转回去的结果是:" + parse);
    }
}

2) 执行结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值