Use calendar to get the current date and time

Source: Internet
Author: User
Tags time and date

1  Packagecom.java.test;2 3 ImportJava.text.SimpleDateFormat;4 ImportJava.util.Calendar;5 6  Public classTimes {7 8      Public Static voidMain (string[] args) {9SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss-sss");TenString time =Format.format (Calendar.getinstance (). GetTime ()); OneSystem.out.println ("Full Time and Date:" +Time ); A          -SimpleDateFormat FORMAT2 =NewSimpleDateFormat ("Yyyy-mm-dd"); -String time2 =Format2.format (Calendar.getinstance (). GetTime ()); theSystem.out.println ("Full Date:" +time2); -          -SimpleDateFormat FORMAT3 =NewSimpleDateFormat ("Hh-mm-ss-sss"); -String Time3 =Format3.format (Calendar.getinstance (). GetTime ()); +System.out.println ("Full time:" +time3); -     } +  A}

Execution Result:

Full time and Date: 2016-07-11-17-00-49-861
Full Date: 2016-07-11
Full time: 17-00-49-862

As can be seen from the results, the local current time can be obtained by calendar.getinstance (). GetTime () and then displayed according to the different (Date and time Patterns) in format.

Description of the SimpleDateFormat class in the Date and time patterns reference Java API

Note: Avoid using the following method, because the second time to get the information is the same as the first time.

Calendar cal = Calendar.getinstance ();

Cal.gettime ();//Get Once

Other code

Cal.gettime ();//Get one More time

Use calendar to get the current date and time

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.