Java classes (Date, DateFormat, Calendar, TimeZone) date-related and other tools (Scanner, System)

Date class

Affiliation specific packaging:

We usually use the Date class belongs to java.util package, we need to import lead packages.

Inheritance:

In addition to the default inherit java.lang.Object class, but without any other inheritance.

Use:

Here Insert Picture Description
As shown, most of its construction methods have been discarded because this class does not take into account the time zone issues at the time of the beginning of the design Java1.0 version. So there is a new class Java1.1 Calendar in subsequent versions instead.
We can use the no-argument constructor and constructor have parameters with long type parameter (milliseconds) to create the object constructor with no arguments default is the current target date of creation of the time.

Date date = new Date();
//上一行代码等价于:
Date date1 = new Date(System.currentTimeMillis());
System.out.println(date.equals(date1));//true
Date date2 = new Date(1582518754409L);

Date class overrides toStirng () method, print Greenwich Time format:

System.out.println(date);//Mon Feb 24 12:28:42 CST 2020

Common methods:

Most of the methods have been abandoned. But abandoned does not mean you can not use, they can be used but it is strongly not recommended.

These are not obsolete methods:

  • before()
  • after()
  • setTime()
  • getTime
  • compareTo()
Date date1 = new Date(1582518754409L);//5分钟之前的时间
Date date2 = new Date();//默认就是用当前的系统时间构建的date对象
boolean x = date1.before(date2);//date1是否在date2之前
System.out.println(x);//true
boolean y = date1.after(date2);//date1是否在date2之后
System.out.println(y);//false
date1.setTime(1582518754409L);
System.out.println(date1.getTime());//1582518754409
System.out.println(date1.compareTo(date2));//-1  调用对象的时间在参数对象的时间之前
System.out.println(date2.compareTo(date1));//1   调用对象的时间在参数对象的时间之后

How to convert Type Date we want:

Since the Date class print out the effect is Mon Feb 24 12:28:42 CST 2020of a format of Greenwich time, then we print out what we want, such as the type of date: 2018-12-09 09:34:12it?
We need the help of the date format class DateFormat.
With a look.

DateFormat class

Affiliation specific packaging:

java.text包,改包专门用作格式化,需要import导包使用。

继承关系:

直接继承自java.text.Format类。
Here Insert Picture Description

使用方式:

DateFormat类是一个抽象类,不能创建对象 ,我们只能拿它的子类来使用。
它的一个子类:SimpleDateFormat

子类SimpleDateFormat的继承关系

Here Insert Picture Description

子类SimpleDateFormat的使用方式

通过创建SimpleDateFormat类的对象来使用。

 Date date1 = new Date();
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String v = sdf.format(date1);//通过sdf对象将date1格式化成你描述的样子
 System.out.println(v);//2020-02-24 13:18:02

Here Insert Picture Description
需要的字符串参数:
常用的:

  • y:代表年份,写四个y,表示4个占位,如:2018(年)
  • M:月份,写两个M,表示2个占位,如:09(月)
  • d:日期 day in month(天)
  • H:小时,24小时制,如:13:00
  • h:小时,12小时制,如:01:00
  • m:分钟,minute in hour
  • s:秒,seconod in minute

Calendar类

所属的包:

java.util包,需要import导入。
Calendar类里包装了Date属性,Date类里包装了time(毫秒值)。

继承关系:

除默认继承java.lang.Object类外,无其它任何继承关系。
Here Insert Picture Description

使用方式:

Calendar类有构造方法,但是用protected修饰,通常访问不到 。我们通常会调用默认的getInstance()方法来获取它的实例对象去使用。

Calendar calendar = Calendar.getInstance();
System.out.println(calendar);//java.util.GregorianCalendar[name1=value1,name2=value2,·····]

常用方法:

  • after()
  • before()
  • setTime()
  • getTime()
  • getTimeInMillis()
  • getTimeZone()
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime());//Mon Feb 24 13:50:36 CST 2020
System.out.println(calendar.getTimeInMillis());//1582523436712
System.out.println(calendar.getTimeZone());//sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]

Calendar里面包含一个date属性,我们可以去操作date的某一个局部信息:

  • set()
  • get()
 calendar.set(Calendar.YEAR,2015);
 int year = calendar.get(Calendar.YEAR);
 int month = calendar.get(Calendar.MONTH);//从0开始计数
 int day = calendar.get(Calendar.DAY_OF_MONTH);
 System.out.println(year+"--"+month+"--"+day);//2015--1--24
 System.out.println(year+"--"+(month+1)+"--"+day);//2015--2--24

TimeZone类

所属的包:

java.util包,需要import导入。

继承关系:

除默认继承java.lang.Object类外,无其它任何继承关系。
Here Insert Picture Description

使用方式:

可以通过calendar对象.getTimeZone()获取 或 TimeZone.getDefault();获取,二者等价。

TimeZone tz = TimeZone.getDefault();
TimeZone tz1 = Calendar.getInstance().getTimeZone();
System.out.println(tz.equals(tz1));//true

常用方法:

  • getID()
  • getDisplayName()
 TimeZone tz = TimeZone.getDefault();
 String id = tz.getID();// Asia/Shanghai
 String name = tz.getDisplayName();//中国标准时间
 System.out.println(id+"----"+name);// Asia/Shanghai----中国标准时间

另附其他常用工具类(Scanner,System)

Scanner类

所属的包:

java.util包,需要import导入。

继承关系:

除默认继承java.lang.Object类外,无其它任何继承关系。

使用方式:

通过一个带输入流的构造方法创建对象。

常用方法:

  • nextInt()
  • nextFloat()
  • next()
  • nextLine()

它们之间的区别在于:nextInt(),nextFloat(),next()它们读取的方式都是以回车符作为结尾,但它们不读回车符,仍然把回车符放在消息队列里。
而nextLine()会读取回车符,然后又扔掉了回车符。
像下面的代码:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入密码:");
        int psw = input.nextInt();

        System.out.println("请输入用户名:");
        String name = input.nextLine();

        System.out.println("用户名:"+name+"密码:"+psw);
    }

Here Insert Picture Description
刚输入完密码123,还没输入用户名,程序就已终止。
原因在于,nextInt()方法不读回车符,它把123读取并拿走之后,消息队列还剩下一个回车符没有读取,到了nextLine()方法,它会读取回车符,它直接把回车符读取了,而回车符作为终止符,它读到回车后认为用户输入完毕了。然后又将回车符扔掉,相当于啥也没读到。
解决的方法:

  1. 在nextInt()方法后面加一个无变量接收的nextLine()方法,将消息队列中多余的回车符都去掉。(即便消息队列中有两个回车符也会一并读取)
  2. 把nextLine()方法替换为next()方法。但是要注意的是next()方法不能读取空格。

System类

所属的包:

java.lang包,不需要导入。

继承关系:

除默认继承java.lang.Object类外,无其它任何继承关系。

使用方式:

不需要创建对象,通过类名就可以访问。

三个属性:

  • out
  • int
  • err

Here Insert Picture Description

常用方法:

  • gc():运行垃圾回收器。
  • exit (): passing a parameter (int status), to terminate the currently running Java Virtual Machine.
  • currentTimeMillis (): returns the system time in milliseconds and the difference between the current year the computer (1970-1-1 00:08:00).
Published 46 original articles · won praise 15 · views 2597

Guess you like

Origin blog.csdn.net/qq_43598138/article/details/104475170