Java (DATE and Calendar class) from scratch, and calendar from scratch

Source: Internet
Author: User

Java (DATE and Calendar class) from scratch, and calendar from scratch
I. Date is a relatively simple operation class. You can directly use java. util. the construction method of the Date class can get a complete Date 2. The Calendar class can get the exact time to milliseconds. However, this class itself is an abstract class. If you want to use an abstract class, you must rely on the object polymorphism to instantiate the parent class through the subclass. The subclass of Calendar is the GregorianCalendar class.

Package com. pb. demo3; import java. util. calendar; import java. util. gregorianCalendar;/** use the Calendar class to display the current time, * use the get method to represent the time, and use the set method to set the date field. * use the get method to represent the time, set the current time to July 15, September * use the add method to add or subtract the specified time for a given calendar field, add 10 days to the current time * use the isLeapYear method of GregorianCalendar to determine whether it is a leap year */public class CalendarDemo {public static void main (String [] args) {/** set the current date to year, month, day, hour, minute, second */Calendar calendar = Calendar ar. getInstance (); // Calendar Object int Year = calendar. get (Calendar. YEAR); int month = calendar. get (Calendar. MONTH) + 1; // The default MONTH is 0-11. You need to manually add 1 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); System. out. println ("today is:" + year + "year" + month + "month" + day + "day"); System. out. println ("current time:" + hour + "hour" + minute + "minute" + second + "second");/* set the month of the current time to September */Calendar. set (Calendar. MONTH, 8); // when the MONTH is set to-1 int year1 = calendar. get (Calendar. YEAR); int month1 = calendar. get (Calendar. MONTH) + 1; // The default MONTH is 0-11. You need to manually add 1 int day1 = calendar. get (Calendar. DAY_OF_MONTH); System. out. println ("today is:" + year1 + "year" + month1 + "month" + day1 + "day"); // Add the current time to the 10-day calendar. add (Calendar. DAY_OF_MONTH, 10); int year2 = calendar. get (Calendar. YEAR); int mon2= calendar. get (Calendar. MONTH) + 1; // The default MONTH is 0-11. Add 1 int day2 = calendar. get (Calendar. DAY_OF_MONTH); System. out. println ("today is:" + year2 + "year" + mon2+ "month" + day2 + "day "); /* determine whether it is a leap year */GregorianCalendar gregorianCalendar = new GregorianCalendar (); boolean flag = gregorianCalendar. isLeapYear (gregorianCalendar. get (gregorianCalendar. YEAR); if (flag = true) {System. out. println ("this year is a leap year! ");} Else {System. out. println (" this year is not a leap year! ");}}}

 

Iii. SimpleDateFormat format template
No. Mark Description
1 Y Year, the year is a four-digit number, so you need to use "yyyy" to represent the year.
2 M The month in the Year. The month is a double digit. Therefore, you must use "MM" to represent the month.
3 D The number of days in a month. The number of days is two digits. Therefore, "dd" must be used to indicate the day.
4 H The hour in a Day (24 hours). The hour is a double digit and "HH" is used to indicate the hour.
5 M The number of minutes in an hour. The minute is a two-digit number. "mm" indicates the minute.
6 S The number of seconds in the minute. The second is a double digit, and "ss" is used to represent the second.
7 S The number of milliseconds. The number of milliseconds is a three-digit number, and "SSS" is used to represent milliseconds.
Iv. Example
Package com. pb. demo3; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; public class DateTest {public static void main (String [] args) {// declare the SimpleDateFormat object and specify the format SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); // obtain the current time Date = new date (); System. out. println ("current time:" + sdf. format (date); String newStr = "14:07:26"; try {Date newdate = sdf. parse (newStr); System. out. println ("converted time:" + newdate);} catch (ParseException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}

 

Related Article

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.