Java learning manual: Date operation

Keywords: Java

I. Date

java.util.Date class encapsulates date and time information

setTime()//Setup time
getTime(Long l)//Acquisition time
//Date other methods are almost set to obsolete and are not recommended.

II. SimpleDateFormat

java.text.SimpleDateFormat is a tool class that formats and parses dates in a locale related way. It allows formatting (date text), parsing (text date), and normalization.
The construction method is as follows:

SimpleDateFormat()
SimpleDateFormat(String pattern)
//Usually, when creating SimpleDateFormat, you need to pass in a String, which is the Date format String, so that it can convert between Date and String according to the format

(1)Date→String

String format(Data date)
//This method converts the time represented by this Date into a string according to the Date format specified when SimpleDateFormat was created

An example is as follows:

//Date→String
Date now = new Date();
SimpleDateFormat  sdf = new SimpleDateFormat("yyyy year MM month dd day HH:mm:ss");
String str = sdf.format(now);

(2)String→Date

Date parse(String str)
//This method can parse the given string according to the Date format specified when creating SimpleDateFormat, and then return the corresponding Date object.

An example is as follows (an exception must be thrown):

//String→Date
String str = "2015-5-20 12:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(str);//Exception to be thrown

Attachment: Date pattern matching character table

character Meaning Example
y year Yy yy → 2019; yyyy → 19
M month MM month → January; M month → January
d day D d day → 06 day; d day → 6 day
E week E → Sunday (Sun)
a AM or PM a → PM
H Hour (24-hour system) a h → 12 p.m
h Hour (12 hour system) HH:mm:ss→12:46:33
m Minute hh(a):mm:ss → 12 (afternoon): 47:48
s second
S Millisecond

III. Calendar

java.util.Calendar class is used to encapsulate calendar information. Calendar class is mainly used for operation time.
Get the Calendar instance to use the static method provided by it. The Calendar instance created by default represents the current system time.

Calendar calendar = Calendar.getInstance();

(1)Calendar→Date

void setTime(Data date)
//This method returns the time represented by the current Calendar as a Date

(2)Date→Calendar
You can use the current Calendar to represent the time represented by a given Date

void setTime(Data date)
//Calendar sets a time, providing a unified time setting method
//Example: Calendar calendar = Calendar.getInstance();
//   date = calendar.setTime(date);

void set(int field, int value)
//This method is used to set the time component given in the time of the current Calendar and the corresponding value
//Example: Calendar calendar = Calendar.getInstance();
//   calendar.set(Calendar.DATE, 10)

int get(int field)
//Get the value corresponding to the given time component
//Example: int year = calendar.get(Calendar.YEAR)

void add(int field, int value)
//Accumulate the given value for the given time component

getActualMaximum()
//Get the maximum value allowed for a time component
//For example (see the total number of days in a year): int days = calendar.getActualMaximum(Calendar.DAY_OF_YEAR)

Attachment: time component corresponding to date

date Time component
DATE The day of the sun, commonly known as the number
DAY_OF_MONTH Days in the middle of the month, always with DATE
DAY_OF_WEEK The day of the week is the day of the week
DAY_OF_YEAR Day of the year

Four, Demo

package com.haobi;
/*
 * Enter production date and shelf life days to calculate promotion date
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter production date(yyyy-MM-dd):");
		String dataStr = scanner.nextLine();
		System.out.println("Please enter shelf life(Days):");
		int days = scanner.nextInt();
		try {
			//String→Date
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(dataStr);
			//Date→Calendar
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			//Calculate promotion day
			calendar.add(Calendar.DAY_OF_YEAR, days);//Quality guarantee period
			calendar.add(Calendar.DAY_OF_YEAR, -14);//14 days in advance
			calendar.add(Calendar.DAY_OF_WEEK, 4);//Wednesday of the week (starting from Sunday)
			//Calendar→Date
			date = calendar.getTime();
			//Date→String
			dataStr = sdf.format(date);
			System.out.println("Promotion date is:"+dataStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
//The output of the program is as follows:
//Please enter production date(yyyy-MM-dd):
2019-04-25
//Please enter shelf life(Days):
30
//Promotion date is:2019-05-15

Posted by jesbin on Sat, 23 Nov 2019 12:59:07 -0800