28探索学习Java的时间处理相关类

时间处理相关类

时间相关的类如下:

在这里插入图片描述

01Date类

Date类是一个java中时间计算的类,在java中首先设定一个时间点为初试时间点,这个时间点是1970年01月01日00点00分00秒,最小刻度是毫秒,用时间差来表示一个特定的时间点,比如:此刻是2020/10/24/15:36/10秒,那么JRE中会存储当前时间和初始时间点的差值来表示当前的时间。

package oop.oopdemo.dateclass;

import java.util.Date;

/**
 * 测试Date类
 * Date类实例化的对象是日期,这个日期是规范格式的,不是long类型的毫秒差
 *
 * @author 发达的范
 * @date 2020/10/24
 */
public class DateTest {
    public static void main(String[] args) {
        /**
        *不传递参数,就默认初始化成系统当前的时间,date对象存储的是格式化的时间参数Sun Oct 25 20:26:54 CST 2020
        *并不是与初试时间点差值毫秒数,要转成毫秒数要使用DateFormat类的引用指向它的子类SimpleDateFormat进行实例化
        */
        Date date = new Date();
        System.out.println(date);
        long t= System.currentTimeMillis();//long类型的数据可以表示2的63次方
        System.out.println(t);
        Date date1 = new Date(1000);//设置时间差为1000毫秒
        System.out.println(date1);
        System.out.println(date1.toGMTString());//转换成中国的时区,废弃的方法,不建议使用
        date1.setTime(2000);//使用setTime方法设置时间差为2000ms
        System.out.println(date1);
        System.out.println(date.before(date1));
        System.out.println(date1.before(date));
        System.out.println(date.toString()+"1");
    }
}

运行结果:在这里插入图片描述

点击运行的时间是:2020/10/24,15:45:24

看一下部分源码:

Date date = new Date();

不传递参数,默认把date对象初始化成系统当前的时间,源码如下:

/**
 * Allocates a <code>Date</code> object and initializes it so that
 * it represents the time at which it was allocated, measured to the
 * nearest millisecond.
 *
 * @see     java.lang.System#currentTimeMillis()
 */
public Date() {
    this(System.currentTimeMillis());//返回系统当前时间的毫秒数
}
long t= System.currentTimeMillis();

直接获取系统当前时间与初试时间差值的毫秒数,我们看到程序运行的时间是:2020/10/24,15:45:24,返回值是1603525524534ms,可以验证一下对不对。

  • 1603525524534/1000/60/60/24/365~=50.85年
  • 1970+50.85=2020.85
  • (10.24)/12~=0.9

由此可见,正确,当然由于只是粗略计算肯定存在误差。


02DateFormat类和和它的实现类SimpleDateFormat类

功能:完成字符串和时间的转化

下面以程序来解释说明:

package oop.oopdemo.dateclass;

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

/**
 * 测试DateFormat类和SimpleDateFormat类
 *
 * 把时间(注意:不是时间差,直接输出date也是格式化的时间)与字符串相互转换
 * @author 发达的范
 * @date 2020/10/24
 */
public class TestDateFormat {
    public static void main(String[] args){
//        DateFormat dateFormat = new DateFormat();
        /**实例化一个日期格式化器,并指定日期显示格式,使用这个实例化对象可以把日期差值转换成已指定的格式
        *注意:指定日期格式时月份必须是大写的MM,小写的mm会造成月份显示错误。
        */
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SS,属于本年的第w周,属于本月的														第W周");
        //初始化一个时间对象并赋值
        Date date = new Date(486215461684L);
        //使用format方法把时间对象按照格式化转成时间格式的字符串
        String str = dateFormat.format(date); 
        System.out.println(str);
        System.out.println("============");
        String string = "1996-05-30";
        //格式化器,这里的格式必须与字符串的格式匹配
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        //这里必须要有捕获异常的程序,至于为什么我还不太清楚,后续再研究
        try {
            Date date1 = new Date();//实例化一个Date类的对象用来保存从字符串转换过来的时间
            date1 = dateFormat1.parse(string);//parse:分析,解析。使用parse方法把字符串转换成时间
            System.out.println(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

运行结果:在这里插入图片描述

分析:

在使用DateFormat类的时候,我本想直接new一个DateFormat对象,但是发现一直报错,进入DateFormat类的源码发现,DateFormat类是一个抽象类,抽象类是不能被实例化的,只能通过它的子类进行实例化,使用父类的引用指向子类的对象来实现

public abstract class DateFormat extends Format {}
public class SimpleDateFormat extends DateFormat {}

这是一个格式化器,实例化的时候必须指定日期的格式,必须和对应的字符串或者String匹配才能使用:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SS,属于本年的第w周,属于本月的第W周");
DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

但是需要注意的是:并不是说必须要初始化多个格式化器,格式化器是为了满足不同格式的日期转换而创建的。比如可以把程序的日期同意,使用一个格式化器也可以:

package oop.oopdemo.dateclass;

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

/**
 * 测试DateFormat类和SimpleDateFormat类
 *
 * @author 发达的范
 * @date 2020/10/24
 */
public class TestDateFormat {
    public static void main(String[] args){
        //初始化一个日期格式化器,这个不是必须用一次new一个,主要是传递的pattern要匹配,否则就要重新new一个
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(486215461684L);
        String str = dateFormat.format(date);//把时间对象按照格式化转成字符串
        System.out.println(str);
        System.out.println("============");
        String string = "1996-05-30";
        try {
            Date date1;
            date1 = dateFormat.parse(string);//parse:分析,解析
            System.out.println(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

运行结果:在这里插入图片描述


03Calendar类和它的实现类GregorianCalender

  • GregorianCalender运算的是公历

在这里插入图片描述

下面看程序:

package oop.oopdemo.dateclass;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 测试日历类GregorianCalender类
 *
 * @author 发达的范
 * @date 2020/10/24
 */
public class TestGregorianCalender {
    public static void main(String[] args) {
        //父类引用指向子类对象,实例化一个日历对象
        Calendar calendar = new GregorianCalendar();
        //设置日历属性,这些属性都是static final修饰的常量
        calendar.set(2020, 1,20,12,20,20 );
        //calender调用getTime方法获取当前时间(规范格式的时间)
        Date date = calendar.getTime();
        System.out.println(date);
        Calendar calendar1 = new GregorianCalendar();
        calendar1.set(Calendar.YEAR,2000);
        //设置成2月February
        calendar1.set(Calendar.MONTH,01);
        calendar1.set(Calendar.DATE,10);
        //从运行结果来看,小时设定是反过来的,设定为10点,显示22点
        calendar1.set(Calendar.HOUR,22);
        calendar1.set(Calendar.MINUTE,15);
        calendar1.set(Calendar.SECOND,55);
        Date date1 = calendar1.getTime();
        System.out.println(date1);
        //使用DateFormat类把时间转换成特定格式显示
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd,hh:mm:ss");
        String str = dateFormat.format(date1);
        System.out.println(str);
    }
}

运行结果:在这里插入图片描述

我在想一个问题:既然DateFormat类可以实现日期的表示,那Calender类存在的意义是什么?
  • 使用DateFormat类和Date类结合可以实现字符串和日期之间的转换
  • Calender类可以实现日期的计算和设定
经总结发现:SimpleDateFormat类的区别于下面的功能是:实现字符串和时间之间的相互转换
GregorianCalender类区别于上面的功能是实现直接(或分别)设置日期

需要注意的是:SimpleDateFormat类和GregorianCalender类都需要结合Date类进行使用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值