Date Operation of Java Basic Knowledge
Classes for date operations in java
1. Introduction to Date
Time in Java uses java.util.Date of the standard class library, which represents specific moments, accurate to milliseconds. A specific time point is expressed by the number of milliseconds (positive or negative, long type) from a fixed time point.
Because Date's design has the problems of "Millennium Bug" and "Time Zone", most of the methods in Date are no longer recommended. They have been replaced by the ** "java.util.Calendar"** class. From the API, we can see that many methods have been abandoned.
Common methods: getTime(), setTime()
//Instance object
Date date = new Date();
System.out.println(date.getTime());
2. Introduction to SimpleDateFormat
SimpleDateFormat is a specific class that formats and parses dates in a context-specific manner. It allows formatting (date - > text), parsing (text - > date) and normalization. Frequently used to format dates.
Common methods: formate(Date date), parse(String s)
//Formatting time strings
String StrD ="2017-04-20 11:22:45";
SimpleDateFormat sdfd =new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
Date dat =sdfd.parse(StrD);
System.out.println(dat);
3. Introduction to Calendar
java.util.Calendar is an abstract class used to encapsulate calendar information. Its main function is that its method can calculate time components.
It provides a method for conversion between a particular instant and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc.
Common methods: getInstance(), set(), get(), getActual Maximum (), add(), gettime(), setTime (Date)
//Set the current time to 2017-04-25
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2017);
calendar.set(Calendar.MONTH,04);
/*
* For numbers, calendar provides two time components
* The meaning is exactly the same. Choose one of them.
* DAY_OF_MONTH
* DATE
*/
calendar.set(Calendar.DATE,25);
calendar.set(Calendar.DAY_OF_MONTH, 25);
Comprehensive Practice
1. Get the current time
public String getCurrentDate(){
String nowDate = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Set date format
nowDate = df.format(new Date());// new Date() to get the current system time
return nowDate;
}
2. Get detailed time
/**
* Get detailed time
*/
public void getDateDetail() {
Calendar calendar = Calendar.getInstance(); // Create an object = Calendar.getInstance(); // You can modify it individually for each time domain
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;// Month, starting from 0, is the output of 5 for June.
int date = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int am_pm = calendar.get(Calendar.AM_PM); // AM=0,PM=1 in the morning or afternoon
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // The day of the month
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // Sunday is the first day of the week
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR); // The Days of the Year
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // Hours of the day
if (calendar.get(Calendar.AM_PM) == Calendar.AM) {
System.out.println("It's morning.");
}
if (calendar.get(Calendar.MONTH) + 1 == Calendar.JULY) {
System.out.println("It's June now.");
}
System.out.println(year + "/" + month + "/" + date + " " + hour + ":"+ minute + ":" + second);
}
3. Date addition and subtraction
/**
* Date add()
* The date plus a positive number means that time moves backwards (in the future), and the date plus a negative number means that time moves forward (in the past).
* When the date is added to 32, it moves forward to the next month.
*/
public void addDate(){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss");
Calendar c = Calendar.getInstance(); //Get an instance of the current date
c.add(Calendar.MONTH, 1); //Current time plus one month
//c.add(Calendar.MONTH, -1); //Current time minus one month
System.out.println(df.format(c.getTime())); //Format output current time
c.add(Calendar.HOUR, 3); // Current time plus 3 hours
//c.add(Calendar.HOUR, -3); // Current time reduced by 3 hours
System.out.println(df.format(c.getTime()));
c.add(Calendar.YEAR, 1); //Current time plus one year
//c.add(Calendar.YEAR, -1); // Current time minus one year
System.out.println(df.format(c.getTime()));
c.add(Calendar.DAY_OF_WEEK, 7); // Current time plus 7 days
//C.add (Calendar.DAY_OF_WEEK,-7); //Current time reduced by 7 days
System.out.println(df.format(c.getTime()));
}
4. Date comparison
/**
* Date comparison
*/
public void compareDate(){
//date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString_01 = "2017-01-01 11:11:11";
String dateString_02 = "2016-01-02 11:11:11";
try {
Date date_01 = sdf.parse( dateString_01 );
Date date_02 = sdf.parse( dateString_02 );
System.out.println( date_01.before(date_02) ); //True, when date_01 is less than date_02, it is true, otherwise it is false
System.out.println( date_02.after(date_01) ); //True, when date_02 is greater than date_01, it is true, otherwise it is false
System.out.println( date_01.compareTo(date_02) ); //- 1, when date_01 is less than date_02, it is -1
System.out.println (date_02.compareTo(date_01) ); //1, when date_02 is greater than date_01, it is 1
System.out.println( date_02.compareTo(date_02) ); //0, when the two dates are equal, 0
} catch (ParseException e) {
e.printStackTrace();
}
}
5. Calculate the number of days between the two dates
/**
* Calculate how many days apart the dates
* @throws ParseException
*/
public void calculateDate() throws ParseException{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate= format.parse("2017-06-24");
Date endDate= format.parse("2017-06-30");
long day = ( endDate.getTime()-beginDate.getTime() )/(24*60*60*1000);
System.out.println(day);
}