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