java class 06_ used time processing class (a)

1. Overview of processing time class

"Time is like water, gone," Time is a one-dimensional stuff. So, we need to express and measure time scale to a zone. In the computer world, we January 1, 1970 00:00:00 as a reference time, each unit of measure is milliseconds (one thousandth of one second).
Here Insert Picture Description
We used to represent a variable of type long time, back to the reference time hundreds of millions of years, hundreds of millions of years later can be expressed. If you want to get the current time "time value", you can use:

long now = System.currentTimeMillis();

This "time value" is the core value of the all-time class, the date is calculated on the basis of this "value." We work and study time related classes covered are as these:
Here Insert Picture Description

2.Date class time

Java.util.Date class contains a standard Java class library in which the object represents a specific moment, with millisecond precision.

Date sent JDK1.0 class began to exist, but because it has a long history, so most of its constructors, methods are outdated and no longer recommended for use.

Date class provides six constructors, four of which are outdated deprecated (no longer recommended for use constructor the compiler will raise a warning and cause problems in application performance, security, etc.), and the remaining two a constructor as follows:

Date() Create a Date object represents the current date and time.
Date(long date) Specify the type of long integer to create a Date object to represent a specified number of milliseconds since the standard base time.

Standard reference time: also known as "epoch (epoch)", namely January 1, 1970 00:00:00 GMT.

[Example] way to create a Date object

// 创建一个代表当前日期时间的Date对象。
Date date1 = new Date();
System.out.println(date1); // 输出:Wed Jan 03 22:22:38 CST 2018
// 指定毫秒数来创建一个Date对象。
Date date2 = new Date(1514984127939L);
System.out.println(date2); // 输出:Wed Jan 03 20:55:27 CST 2018

Most methods of the Date object has also been abandoned, leaving only a small number of methods:

boolean before(Date when): Test if this date is after the specified date

boolean after(Date when): Test if this date is before the specified date.

boolean equals(Object obj): Compares two dates for equality.

long getTime(): Returns the number of milliseconds since January 1, 1970 00:00:00 GMT represented by this Date object.

void setTime(long time): The given time value in milliseconds prior Date object.

[Example] Returns the number of days difference between two dates The objects

public int method(Date date1, Date date2) {
	// 1.把日期对象转化为毫秒数
	long time1 = date1.getTime();
	long time2 = date2.getTime();
	// 2.获取毫秒数的查值
	long result = Math.abs(time1 - time2);
	// 3.转化为天数
	int day = (int) Math.floor(result/1000/60/60/24);
	// 4.返回天数
	return day;
}

3.DateFormat class (understand)

DateFormat date / time formatting subclasses of the abstract class, which formats and parses dates or time in a language-independent manner. In fact, it is the Date formatting tool that can help us Date format, and then convert the Date into String string we want for our use.

However, by definition can be found, DateFormat class is an abstract class, according to conventional thinking, the direct use to subclass is instantiated, but the internal DateFormat class itself provides for direct operation instantiated.

Get a DateFormat object date:public final static DateFormat getDateTimeInstance()

DateFormat objects to get the date and time:public final static DateFormat getDateTimeInstance()

DateFormat class using the process of converting an object to a String Date function **: **public final String format(Date date)

Use DateFormat class to complete converting String to Date function: public Date parse(String source) throws ParseExceptionwhen converted to the String matches a specified pattern, or can not be converted.

[Example] the default date format conversion case

public static void main(String[] args) throws ParseException {
	// Date对象转化为String
	dateToString(new Date());
	// String转化为Date对象
	stringToDate("2017-12-03");
}
// String转化为Date对象
public static void stringToDate(String time) throws ParseException {
	// 实例化日期DateFormat对象
	DateFormat df = DateFormat.getDateInstance();
	// 字符串转化为Date对象,此处字符串格式应为:yyyy-MM-dd,否则抛出异常
	Date date = df.parse(time);
	System.out.println(date); // 输出:Sun Dec 03 00:00:00 CST 2017
}
// Date对象转化为String
public static void dateToString(Date date) {
	// 实例化日期DateFormat对象
	DateFormat df1 = DateFormat.getDateInstance();
	// 把Date对象转化为字符串
	String time1 = df1.format(date);
	System.out.println(time1); // 输出:2018-1-4

	// 实例化日期时间DateFormat对象
	DateFormat df2 = DateFormat.getDateTimeInstance();
	// 把Date对象转化为字符串
	String time2 = df2.format(date);
	System.out.println(time2); // 输出:2018-1-4 14:05:28
}

By such a class can be displayed Date reasonably direct formatting operation, it supports formatting styles include FULL, LONG, MEDIUM, and SHORT total of 4:

DateFormat.SHORT : constant short styles, such as "18-1-4" or "18-1-4 2:24 pm."

DateFormat.MEDIUM : Medium constant style pattern, for example, "2018-1-4" or "2018-1-4 14:25:12."

DateFormat.LONG : long-lasting style, such as "January 4th, 2018" or "at 14:24:20 on January 4th 2018."

DateFormat.FULL : constant full style pattern, for example, "January 2018 Thursday, 4 December" or "January 4, 2018 Thursday 14:25:35 CST".

[Example] for a specific date format conversion case

public static void main(String[] args) throws ParseException {
	// Date对象转化为String
	dateToString1(new Date());
	// String转化为Date对象
	stringToDate1("2017年12月18日");
}
// String转化为Date对象
public static void stringToDate(String time) throws ParseException {
	// 实例化日期DateFormat对象
	DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
	// 字符串转化为Date对象,此处字符串格式应为:yyyy年MM月dd日,否则抛出异常
	Date date = df.parse(time);
	System.out.println(date); // 输出:Mon Dec 18 00:00:00 CST 2017
}
// Date对象转化为String
public static void dateToString(Date date) {
	// 指定格式实例化日期DateFormat对象,LONG格式
	DateFormat df1 = DateFormat.getDateInstance(DateFormat.LONG);
	String time1 = df1.format(date);
	System.out.println(time1); // 输出:2018年1月4日

	// 指定格式实例化日期时间DateFormat对象,LONG格式
	DateFormat df2 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
	String time2 = df2.format(date);
	System.out.println(time2); // 输出:2018年1月4日 下午02时29分41秒
}

4.SimpleDateFormat类

Because of the limited format Date DateFormat function SimpleDateFormat is far from strong. SimpleDateFormat class is a subclass of abstract class DateFormat for locale sensitive manner to formatting and parsing dates.

About conversion between Date, time stamp (long) and String type, mainly used SimpleDateFormat classes to implement, when you create a SimpleDateFormat object, we can specify the format to format operations on date and time, and then to Date and conversion target string.

Specify the format rules are as follows:

  • When y appears, it will replace y adulthood.

  • When M appears, it will replace M month.

  • When d occurs, it will replace Chengri d.

  • When h occurs, it will be replaced when h to (12-hour).

  • When H occurs, it will be replaced by H (24-hour).

  • When m occurs, it will replace the component m.

  • When there is s, s will replace seconds.

  • It occurs when S, S will replace milliseconds.

  • When D appears, to get the current time is the first few days of the year.

  • When w appears, to get the current time is the first few weeks of the year.

  • When W appears, to get the current time is the first few weeks of the month.

[Example] custom date format conversion case

public static void main(String[] args) throws ParseException {
	// Date对象转化为String
	dateToString(new Date());
	// String转化为Date对象
	stringToDate("2017年12月18日 23:11:20");
}
// String转化为Date对象
public static void stringToDate(String time) throws ParseException {
	DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
	Date date = df.parse(time);
	System.out.println(date); // 输出:Mon Dec 18 23:11:20 CST 2017
}
// Date对象转化为String
public static void dateToString(Date date) {
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	String time = df.format(date);
	System.out.println(time); // 输出:2018-01-04
}

[Example] to get the current time is the first few days of the year

DateFormat df = new SimpleDateFormat("D");
String day = df.format(new Date());
System.out.println("今天是今年的第" + day + "天");

5.Calendar class calendar

Calendar is a calendar class, provides the features on the date of the calculation for us, such as: year, month, day, hour, minute, second display and computing (appearance of the Calendar class, replacing a number of methods Date).

Calendar is an abstract class, Calendar class is not created directly when you create an object, but created by the static method, and then return the created object is a subclass of GregorianCalendar.

[Example] to create the current calendar Object Case

// 通过静态方法创建当前日期对象
Calendar calendar = Calendar.getInstance(); 
System.out.println(calendar);

GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most countries / regions in the world to use.

Calendar class common methods:

According to the time field number, the date of acquisition-related element values:public int get(int field)

The following is a common get field number:

  • Calendar.YEAR acquisition Year

  • Calendar.MONTH get the month, 0 for January, 1 February, 2 March, ..., 11 represents December

  • Calendar.DAY_OF_MONTH get the first few days of the month

  • Calendar.DAY_OF_YEAR get the first few days of the year

  • Calendar.HOUR_OF_DAY hour, 24-hour

  • Calendar.HOUR hour, 12-hour

  • Calendar.MINUTE get minutes

  • Calendar.SECOND get seconds

  • Calendar.MILLISECOND get ms

  • Calendar.DAY_OF_YEAR get how many days a year

  • Calendar.DAY_OF_WEEK acquisition of the week, Sunday = 1, Monday = 2, ..., 7 for Saturday

[Example] the date of acquisition-related elements of the case

// 通过静态方法创建当前日期对象
Calendar calendar = Calendar.getInstance(); 
int year = calendar.get(Calendar.YEAR); // 获取年份
// 0表示1月,1表示2月,2表示3月,...,11表示12月
int month = calendar.get(Calendar.MONTH);// 获取月份
int day = calendar.get(Calendar.DAY_OF_MONTH); // 获取本月第几天
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 获取小时
int minute = calendar.get(Calendar.MINUTE); // 获取分钟
int second = calendar.get(Calendar.SECOND); // 获取秒
int milliSecond = calendar.get(Calendar.MILLISECOND); // 获取毫秒
// 1表示星期日,2表示星期一,...,7表示星期六
int week = calendar.get(Calendar.DAY_OF_WEEK); // 获取星期几
int days = calendar.get(Calendar.DAY_OF_YEAR); // 获取本年度第几天

Set date associated element value l

The time field number, date element provided correlation values. Field fill in the date field

public void set(int field, int value)

[Example] The time field number, date elements provided separately Case

Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.YEAR, 2017);  
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DAY_OF_MONTH, 22);
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 12);
calendar.set(Calendar.SECOND, 22);
calendar.set(Calendar.MILLISECOND, 2);

Minutes and seconds to set the date according to the date when the element value

public final void set(int year, int month, int date)

public final void set(int year, int month, int date, int hour, int minute)

public final void set(int year, int month, int date, int hour, int minute, int second)

[Example] minutes and seconds to set the date element values ​​based on the date when the case

Calendar calendar = Calendar.getInstance(); 
calendar.set(2018, 1, 23, 23, 23, 23);

According to the time field numbers increase a value: public void add(int field, int amount)Case

// 通过静态方法创建当前日期对象
Calendar calendar = Calendar.getInstance(); 
// 把日期对象设置为指定时间
calendar.set(2018, 1, 8, 23, 10, 23);
// 修改当前时间为 4 天后
calendar.add(Calendar.DAY_OF_MONTH, 4);
// 修改当前时间为 5 小时前
calendar.add(Calendar.HOUR_OF_DAY, -5);
// 做完上述操作后,此时calendar对象保存的时间为:2018-02-12 18:10:23

l time calendar object and convert the object

The method of obtaining the Date object from a Calendar object:public final Date getTime()

The reaction Date object to a Calendar object:public final void setTime(Date date)

[Example] Date and Calendar swap case

// 通过静态方法创建当前日期对象
Calendar calendar = Calendar.getInstance(); 
// 获取日期对象中的Date对象
Date date = calendar.getTime();
// 给日期对象设置指定Date对象
calendar.setTime(date);

l time calendar object and convert the object

The method of obtaining the Date object from a Calendar object:public final Date getTime()

The reaction Date object to a Calendar object:public final void setTime(Date date)

[Example] Date and Calendar swap case

// 通过静态方法创建当前日期对象
Calendar calendar = Calendar.getInstance(); 
// 获取日期对象中的Date对象
Date date = calendar.getTime();
// 给日期对象设置指定Date对象
calendar.setTime(date);

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 92 original articles · won praise 0 · Views 2594

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104784313