Java Date Api

Source: Internet
Author: User
Tags set time time and date

1  Public classTimetest {2 3      Public Static voidMain (string[] args) {4Date D1 =NewDate ();5SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd:hh-mm-ss");6 System.out.println (Sdf.format (D1));7 8Calendar C1 =calendar.getinstance ();9         intYear =C1.get (calendar.year);Ten         intmonth = C1.get (calendar.month) + 1; One         intDay =C1.get (calendar.day_of_month); A         inthour =C1.get (calendar.hour); -         intminute =C1.get (calendar.minute); -         intSecond =C1.get (calendar.second); theSYSTEM.OUT.PRINTLN (Year + "years" + month + "Month" + Day + "days" + Hour + "time" + Minute + "min" + Second + "SEC"); -  -         intHourofday =C1.get (calendar.hour_of_day); -System.out.println ("The day of the first" + Hourofday + "hours"); +  -         intDayOfWeek =C1.get (calendar.day_of_week); +System.out.println ("First week" + DayOfWeek + "Day")); A  at         intDayOfMonth =C1.get (calendar.day_of_month); -System.out.println ("January" + DayOfMonth + "Day"); -  -         intDayOfYear =C1.get (calendar.day_of_year); -SYSTEM.OUT.PRINTLN ("First year" + DayOfYear + "Day")); -  in         intWeekOfYear =C1.get (calendar.week_of_year); -SYSTEM.OUT.PRINTLN ("First year" + WeekOfYear + "Week")); to  +         intWeeksinweekyear =c1.getweeksinweekyear (); -SYSTEM.OUT.PRINTLN ("One year with" + Weeksinweekyear + "Week")); the  *Date time =c1.gettime (); $System.out.println ("Date:" +Time );Panax Notoginseng  -TimeZone TimeZone =C1.gettimezone (); theSystem.out.println ("Time zone:" +timeZone); +  A         LongTimeinmillis =C1.gettimeinmillis (); the System.out.println (timeinmillis); +  -  $         intWeekyear =c1.getweekyear (); $SYSTEM.OUT.PRINTLN ("This year is:" +weekyear); -  -     } the}

Test results:

2016-09-06:14-41-1614:41:16 CST time zone: Sun.util.calendar.zoneinfo[id= "Asia/shanghai", offset= 28800000,dstsavings=0,usedaylight=false, transitions=19,lastrule=null]1473144076561 this year is:2016

There are 2 classes of dates in Java

1. Date + SimpleDateFormat

The most important function of the Date class is to get the current time, and this class also has set time and some other functions, but because of its own design problems, these methods have been criticized many, not recommended, more recommended to use the Calendar class for time and date processing

2. Calender

When you encounter date handling in your code, it is recommended that you get to the instance by using the GetInstance () method.

Reference: http://www.apihome.cn/api/java/Calendar.html

Java Date Api

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.