Date class, SimpleDateFormat class, Calendar class

Date class

Date class represents a specific moment, with millisecond precision.
Note that the month represented by an integer of 0 to 11; 0 is January, February, etc. 1

Construction method

public Date ()
assign a Date object and initializes it, so that it represents the time allocated, measured to the nearest millisecond.
public Date (long date)
assign a Date object and initializes it to represent the standard reference time (known as "Time") which specifies the number of milliseconds since January 1, 1970 00:00:00 GMT.

public class Demo {
    public static void main(String[] args) {
    	//创建一个Date对象,表示当前时间
        Date date1=new Date();
        System.out.println(date1);
        //创建一个Date对象,表示从1970年1月1日00:00:00 GMT起的指定毫秒数。
        Date date2=new Date(1000*60*60*24);
        System.out.println(date2);
    }
}
/*
运行结果:
Sun Jan 12 18:55:22 CST 2020
Fri Jan 02 08:00:00 CST 1970
*/
method

public long getTime ()
returns the number of milliseconds since 00:00:00 GMT January 1, 1970, by the Date object represents.
public void setTime (long time)
Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

public class Demo {
    public static void main(String[] args) {
        Date date1=new Date();
        long time=date1.getTime();
        System.out.println(time);
        Date date2=new Date();
        //给计算机元年增加毫秒值
        date2.setTime(1000*60*60*24); 
        System.out.println(date2);
    }
}
/*
运行结果:
1578826772098
Fri Jan 02 08:00:00 CST 1970
*/

SimpleDateFormat类

SimpleDateFormat is a concrete class for the locale sensitive manner to formatting and parsing dates.

Construction method

public SimpleDateFormat ()
Constructs a SimpleDateFormat using symbol and date format default mode as the default FORMAT region.
SimpleDateFormat (String pattern)
using a given mode SimpleDateFormat using the default date format symbols FORMAT default locale.

public class Demo4 {
    public static void main(String[] args) throws ParseException {
        //指定日期显示的格式
        Date date=new Date();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat();
        //按照默认格式对日期进行格式化
        String format1=simpleDateFormat1.format(date);
        System.out.println(format1);
        
        //在初始化时指定格式
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy年M月d日 HH:mm:ss");
        //将日期按照格式格式化为一个字符串
        String format2=simpleDateFormat2.format(date);
        System.out.println(format2);
	}
}
/*
运行结果:
20-1-12 下午7:04
2020年1月12日 19:04:33
*/

In the Java help documentation, has given us rules on the format, we can show the format we want a review of documents

method

public String format (Date date)
to format a date object into a string
has been used in the above
public Date parse (String dateStr)
to parse a date string to a target date to be resolved in a specified format Note

public class Demo4 {
    public static void main(String[] args) throws ParseException {
        String dateStr="2020-01-11 10:00:00";
        //注意格式要对应字符串,否则会报错
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //把一个日期字符串解析成一个日期对象
        Date date = simpleDateFormat.parse(dateStr);
        System.out.println(date);
	}
}
/*
运行结果:
Sat Jan 11 10:00:00 CST020
*/       

Class Calendar

Calendar class is an abstract class that provides a specific instant and the conversion between a group such as a calendar field YEAR, MONTH, DAY_OF_MONTH, HOUR some other method, and for manipulating the calendar fields (e.g., date of obtaining the next week) provided some method. Instantly available millisecond value to indicate that it is from the epoch (ie GMT 00 January 1, 1970 are: 00: 00.000, Gregorian calendar) offset.

method

Because the Calendar class is an abstract class, so we have to use the method to get the object of the Calendar class
public static Calendar getInstance ()
uses the default time zone and locale to get a calendar.
public int get (int field)
Returns the value of a given calendar field.
public abstract void add (int field, int amount)
in accordance with the rules of the calendar, add or subtract the amount of time specified for the given calendar field.
public final void set (int year, int month, int date)
disposed calendar field value YEAR, MONTH, and the DAY_OF_MONTH.
There are many overloaded methods set method can query the document obtained.

public class Demo {
    public static void main(String[] args) {
        Calendar instance=Calendar.getInstance();
        /*
        如果我们输出instance怎会得知它是GregorianCalendar类的对象,该类是Calendar的子类
        所以getInstance方式实际上返回的是GregorianCalendar类的对象
        */
        
       	//field是Calendar类中的字段,具体可查询帮助文档
        int year=instance.get(Calendar.YEAR);
        System.out.println(year);
        int month = instance.get(Calendar.MONTH);
        System.out.println(month);
       
        Calendar instance1=Calendar.getInstance();
        instance1.add(Calendar.YEAR,-1);
        int year1 = instance1.get(Calendar.YEAR);
        System.out.println(year1);
        
        Calendar instacne2=Calendar.getInstance();
        instacne2.set(2021,6,5);
        int day=instacne2.get(Calendar.DAY_OF_MONTH);
        System.out.println(day);
    }
}
/*
运行结果:
2020
0
2019
5
*/
发布了26 篇原创文章 · 获赞 1 · 访问量 330

Guess you like

Origin blog.csdn.net/weixin_45919908/article/details/103948265