Java gets the system time, java gets the system

Source: Internet
Author: User

Java gets the system time, java gets the system
1. Date class

1 Date day = new Date (); 2 SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 3 System. out. println (df. format (day); 4 // or 5 Date date = new Date (); 6 String year = String. format ("% tY", date); 7 String month = String. format ("% tB", date); 8 String day = String. format ("% te", date); 9 System. out. println (year + "-" + month + "-" + day );
2. System class
1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");2 System.out.println(df.format(System.currentTimeMillis()));
3. Calendar class
1 Calendar c = Calendar. getInstance (); // you can modify 2 int year = c. get (Calendar. YEAR); 3 int month = c. get (Calendar. MONTH); 4 int date = c. get (Calendar. DATE); 5 int hour = c. get (Calendar. HOUR_OF_DAY); 6 int minute = c. get (Calendar. MINUTE); 7 int second = c. get (Calendar. SECOND); 8 System. out. println (year + "-" + month + "-" + date + "" + hour + ":" + minute + ":" + second );

 Summary:The System time can be obtained through the Date class, the Calendar class, And the currentTimeMillis in the System. The time format can be set by calling the SimpleDateFormat class and the format method in the String.

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.