[Java] Java-based Date class, DateFormat class, Calendar class

Date class

Date represents time in java, and the unit of time is milliseconds. There are two construction methods

Date class construction method
method Description
public Date() Create object with current time
public Date(long date) Create a time object with a millisecond value, the millisecond value represents the difference time from the reference time

                                                        Base time: January 1, 1970 00:00:00 GMT

public class Test01 {
    public static void main(String[] args) {
        //Date的构造方法
        //new Date():代表当前时间
        Date d = new Date();
        System.out.println(d);//
        //new Date(long date):代表距离基准时间的差值时间
        //1970年1月1日8点0分1秒
        Date d2 = new Date(1000);
        System.out.println(d2);
        //1970年1月1日9点0分0秒
        Date d3 = new Date(1000*60*60);
        System.out.println(d3);
    }
}

 Calculation result:

                                                    

Common methods of Date:

Common method
method Description
getTime() Get the time in milliseconds
setTime(long time) Set the time in milliseconds
before(Date date) Judge before a certain time
after(Date date) After a certain time
import java.util.Date;
public class Test02 {
    public static void main(String[] args) {
        //创建对象
        Date d = new Date();
        Date d2 = new Date();

        //getTime()
        //获取时间的毫秒值
        long time = d.getTime();
        System.out.println(time);   //1597027887127      1970年1月1日到现在的毫秒值差值

        //setTime(long  time)
        //设置时间的毫秒值
        d.setTime(235252352377L);
        System.out.println(d);

        //判断
        System.out.println(d.after(d2)); //1977是否在2020之后    false
        System.out.println(d.before(d2)); //1977是否在2020之前   true

    }
}

DateFormat class

DateFormat is a date formatting class that can convert dates into strings in different formats.

Since DateFormat is an abstract class , its subclass object is created when it is created: SimpleDateFormat ;

There are two commonly used methods:

Common methods:
method Description
String format(Date date) Convert the date into a string according to the specified format
Date parse(String s) Convert a string into a date according to the specified format

The usage of the two methods is as follows:

                       

               

Calendar class

The Calendar class turns all the values ​​related to time into one field, which is convenient for operating each field

Create an object: Calendar c=Calendar.getInstance();

Common method
method Description
int get(int field) Get the value of a field
void set(int field,int value) Set the value of a field
void add(int field,int amount) Offset the value of a field (addition and subtraction on the original basis)
setTime(Date date ) Assign the time of the Date object to Calendar
package com.itheima;

import java.util.Calendar;

public class DemoCalendar {
    public static void main(String[] args) {
        //创建calendar对象
        Calendar calendar=Calendar.getInstance();
        //获取年
        System.out.println(calendar.get(Calendar.YEAR));
        //获取月(月的取值范围0~11)
        System.out.println(calendar.get(Calendar.MONTH)+1);
        //获取日
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        //获取时
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
        //获取分
        System.out.println(calendar.get(Calendar.MINUTE));
        //获取秒
        System.out.println(calendar.get(Calendar.SECOND));
        //获取周几,结合getWeek方法使用
        System.out.println(getWeek(calendar.get(Calendar.DAY_OF_WEEK)));
    }
    //获取周几
    public static String getWeek(int n){
        String[] strings={"","周一","周二","周三","周四","周五","周六","周日"};
        return strings[n];
    }
}

 

Guess you like

Origin blog.csdn.net/weixin_43267344/article/details/107920867