23.日期类

JAVA 对日期的处理

1. Date类

java.util下有个Date类,通过该类可以获取系统当前时间

The class Date represents a specific instant in time, with millisecond precision.(毫秒级精度)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5cGZtUOj-1620744696728)(F:\JAVA_learning\动力节点——md笔记\24.日期\1.png)]

Date类中大部分构造方法已经过时了,无参构造方法没有过时。还有一个Date(long date)没有过时,下面会学习

其作用是:Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

import java.util.Date;

public class DateTest01 {
    public static void main(String[] args) {
        Date date = new Date();
        //Date类已经重写了toString()方法了
        System.out.println(date);//Wed May 05 11:43:44 CST 2021
    }
}
//Date的无参构造方法会创建一个时间对象,该对象代表了创建时的时间

但是,考虑到上面打印的时间不适合国际化,需要进行格式化处理

负责对时间进行格式化处理的类是 SimpleDateFormat ,属于 java.text包下,专门负责日期的格式化

2. SimpleDateFormat类

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text), parsing (text → date), and normalization.

1. SimpleDateFormat类的构造方法和format(格式化)方法(Date --> String)

public SimpleDateFormat(String pattern)
{
    this(pattern, Locale.getDefault(Locale.Category.FORMAT));
}
//Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default FORMAT locale.

上面的有参构造器中**,String pattern表示模式,格式化处理时会按照pattern表示的模式进行处理**。

将日期类型Date,按照指定的格式进行转换:Date --转换成具有一定格式的日期字符串-->String

举例:

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

public class DateTest01 {
    public static void main(String[] args) {
        Date date = new Date();
        //Date类已经重写了toString()方法了
        System.out.println(date);//Wed May 05 11:43:44 CST 2021

        /*
        yyyy 年(年是4位)
        MM 月(月是2位)
        dd 日
        HH 时
        mm 分
        ss 秒
        SSS 毫秒(毫秒3位,最高999。1000毫秒代表1秒)
        注意:在日期格式中,除了y M d H m s S这些字符不能随便写之外,剩下的符号格式自己随意组织。
         */
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss sss");
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss sss");
        String s = simpleDateFormat.format(date);
        System.out.println(s);//2021/05/05 11:58:23 023
    }
}

总结就是:SimpleDateFormat类创建对象后,参数可以是一个格式化模板,该对象的format(Date date)方法会将接收到的Date对象按照模板pattern的方式进行格式化处理,并返回时间的字符串:

实现了Date —> String!

2. SimpleDateFormat类的parse(String)解析方法(String --> Date)

假设现在有一个日期字符串String,怎么转换成Date类型?String --> Date

注意:使用parse解析时,SimpleDateFormat对象的参数模板pattern必须和待解析的字符串一致,否则会出现异常 java.text.ParseException

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

public class DateTest01 {
    public static void main(String[] args) throws Exception{
        String ss = "2021/05/05 12:08:55 555";
         //SimpleDateFormat sdf2 = new SimpleDateFormat("格式不能随便写,要和日期字符串格式相同");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss sss");//格式化模板必须和字符串一致,这样才能完成解析
        Date d = sdf.parse(ss);
        System.out.println(d);//Wed May 05 12:17:15 CST 2021
        
        /*
        String ss = "2021/05/05 12:08:55 555";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss sss");//格式化模板         的个是必须和字符串一致,这样才能完成解析不一致就会抛出异常:
        // java.text.ParseException 解析异常
        Date d = sdf.parse(ss);
        System.out.println(d);//Wed May 05 12:17:15 CST 2021
        */
    }
}

3. System类的currentTimeMillis()方法

public static long currentTimeMillis()//静态方法,直接通过类名调用,返回long类型

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

该方法获取自1970年1月1日 00:00:00 000到当前系统时间的总毫秒数

这个方法有什么用

可以利用时间差计算代码执行的时间

public class DateTest02 {
    public static void main(String[] args) {
        //System.currentTimeMillis()方法
        //获取自1970年1月1日 00:00:00 000到当前系统时间的总毫秒数
        long l = System.currentTimeMillis();
        System.out.println(l);//1620213811480

        //上面的方法有什么用呢?
        //利用返回的时间差可以计算一个代码执行的时间
        // 在调用目标方法之前记录一个毫秒数
        long begin = System.currentTimeMillis();
        print();
        // 在执行完目标方法之后记录一个毫秒数
        long end = System.currentTimeMillis();
        System.out.println("执行时间:"+(end-begin)+"毫秒");

    }
    // 需求:统计一个方法执行所耗费的时长
    public static void print(){
        for (int i = 0; i < 100000000; i++) {
            //System.out.println(i);
        }
    }
}

4. Date类的有参构造方法(只有一个有参构造器没过时)

public Date(long date){}

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as “the epoch”, namely January 1, 1970, 00:00:00 GMT.

该构造方法按照GMT标准,返回一个从1970 00:00:00 000 偏移long date毫秒数的时间对象

package date;

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

public class DateTest03 {
    public static void main(String[] args) {
        Date date = new Date(1);//1970-01-01 00:00:00 001(这是我的格式化后的)
        //八点是因为按照GMT标准,北京属于东八区
        System.out.println(date);//Thu Jan 01 08:00:00 CST 1970
        //格式化处理
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String timeStr = sdf.format(date);
        //偏移了1毫秒
        System.out.println(timeStr);//1970-01-01 08:00:00 001

        //利用上面的构造方法怎么获取昨天此时的时间?
        Date date1 = new Date(System.currentTimeMillis() - 24*60*60*1000);
        //格式化
        String s = sdf.format(date1);
        //本代码于2021-05-05 23:16:18 278执行
        System.out.println(s);//2021-05-04 23:16:18 278
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值