"Java" uses the Calendar class to get the current date

Source: Internet
Author: User

In "Java" about System.currenttimemillis () thought (click the Open link) has been described, if the use of System.currenttimemillis () to take out the current period. In Java, you can actually use the Calendar class to get the current date. Only its cumbersome, to take out the current year, month, day, time, minutes, seconds to splice it together.

The following code:

Import Java.util.calendar;import Java.util.gregoriancalendar;public class Calendartest {public static void main (String [] args) {Calendar calendar=calendar.getinstance (); System.out.println ("Now Is:" +calendar.get (gregoriancalendar.year) + "year" + (Calendar.get (gregoriancalendar.month) +1) + " Month "+calendar.get (gregoriancalendar.day_of_month) +" Day "+calendar.get (gregoriancalendar.hour) +" Time "+calendar.get ( Gregoriancalendar.minute) + "min" +calendar.get (Gregoriancalendar.second) + "SEC");}}

The results of the operation are as follows:


As you can see, the result of the run corresponds to the current time.

In the above code, the Calendar.get (gregoriancalendar.xx) is used to remove the month-date and minute-by-day, different from the previous calendar.get (CALENDAR.XXX), which prevents eclipse from warning. If written as Calendar.get (CALENDAR.XXX), the following hint appears in eclipse: calendar.xxx should is accessed in a static.

Also, note that taking the current month, you must check out the result of +1, which is the correct current month. Otherwise, the result is 1 less than the current month.

The current day is Calendar.get (gregoriancalendar.day_of_month), not Calendar.get (Gregoriancalendar.day).

"Java" uses the Calendar class to get the current date

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.