Java Fundamentals 18-date Classes and calendar classes

Source: Internet
Author: User
Tags time and seconds

First, Java.util.Date date class

Private Integer age;//Employee age
Private String hiredate;//Entry time
The basic information about the employee is stored in the system, including the age of the employee, and it is unreasonable to use the Integer/int type to indicate the age.
Do you have to change the age of the employees in the system by 1 each year?

1. Class Date represents a specific moment, accurate to milliseconds.

There is a method in the date class that returns the millisecond value since 1970 to the present, which is the GetTime method, which is a long type value

There are many obsolete methods in the date class, but these obsolete methods are also useful, such as the below

This representation is more in line with our Chinese habits.

Second, DateFormat date of format

Format: Date type Object--->string type
Parsing: String type time---->object type

1. Formatting methods in the Date class

2. Format: Date type Object--->string type

3. Parsing: String type time---->object type

4. Custom Date Mode

Third, Calendar class

1. Common methods

We create a Calendar object

found that the output of a lot of things are not very good to understand, what can be done?

2. This will use the Get method to get the Time field value

3. How do I add and set dates?

This uses the add and set methods

What's the number of months after 100 days?

4. Calendar classes can also be converted to dates

Note: in the Calendar class, the month representation is represented by 0-11 for 1-12 months.

Case 1:

Calculate Start and end time
Query the last week's information, how to indicate the most recent week

Date current =NewDate (); //time of the current time as a calendarCalendar c3=calendar.getinstance ();        C3.settime (current); //increase the day by 1 days, and then set the time and seconds to 0C3.add (Calendar.day_of_month, 1); C3.set (Calendar.hour_of_day,0);//when set to 0C3.set (calendar.minute, 0);//Sub-set to 0C3.set (Calendar.second, 0);//seconds set to 0Date endtime=C3.gettime (); C3.add (Calendar.day_of_month,-7); Date BeginTime=C3.gettime (); System.out.println ("Start time =" +begintime.tolocalestring ()); System.out.println ("End time =" +endtime.tolocalestring ());

Case 2: Get the number of days you've been born to now

 Public Static voidMain (string[] args) {Calendar my=calendar.getinstance (); Calendar C=calendar.getinstance (); //Set Birth date 1995-05-10My.set (Calendar.year, 1995); My.set (Calendar.month,4); My.set (Calendar.date,10); //get the number of days in a time    intDay =C.get (calendar.date); intMyday =My.get (calendar.date); System.out.println ( Day-myday);}

Case 3: Find out how many days a day is away from a certain month

 Public Static voidMain (string[] args) {Calendar my=calendar.getinstance (); Calendar C=calendar.getinstance (); //Set Month Day 2020-01-01My.set (Calendar.year, 2020); My.set (Calendar.month,0); My.set (Calendar.date,1); //get the number of days in a time    intDay =C.get (calendar.date); intMyday =My.get (calendar.date); System.out.println (Myday-Day );}

public static void Main (string[] args) {

Calendar my = Calendar.getinstance ();

Calendar C = calendar.getinstance ();

Set Month day 2020-01-01

My.set (calendar.year, 2020);

My.set (calendar.month, 0);

My.set (calendar.date, 1);

get the number of days in a time

int day = C.get (calendar.date);

int myday = My.get (calendar.date);

System.out.println (Myday-day);

}

Java Fundamentals 18-date Classes and calendar classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.