Packagedate;ImportJava.text.DateFormat;ImportJava.util.Calendar;Importjava.util.Date;/*2015-9-9*/ Public classDatedemo {/** * @paramargs*/ Public Static voidMain (string[] args) {Date now=NewDate (); System.out.println ("Show Time in Date:" + now);//This method displays the same results as calendar.getinstance (). GetTime ().DateFormat D1= Dateformat.getdateinstance ();//Default language (medium style, for example: 2008-6-16 20:54:53)String str1 =D1.format (now); System.out.println (After formatting the time with Dateformat.getdateinstance (): "+str1); DateFormat D2=dateformat.getdatetimeinstance (); String str2=D2.format (now); System.out.println (After formatting the time with Dateformat.getdatetimeinstance (): "+str2); DateFormat D3=dateformat.gettimeinstance (); String STR3=D3.format (now); System.out.println (After formatting the time with Dateformat.gettimeinstance (): "+STR3); DateFormat D4= Dateformat.getinstance ();//display date and time using short styleString STR4 =D4.format (now); System.out.println (After formatting the time with Dateformat.getinstance (): "+STR4); DateFormat d5= Dateformat.getdatetimeinstance (Dateformat.full, dateformat.full);//display date, week, time (accurate to seconds)String STR5 =D5.format (now); System.out.println (After formatting the time with Dateformat.getdatetimeinstance (dateformat.full,dateformat.full): "+STR5); DateFormat d6= Dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long);//Displays the date. Time (accurate to seconds)String STR6 =D6.format (now); System.out.println (After formatting the time with Dateformat.getdatetimeinstance (Dateformat.long,dateformat.long): "+STR6); DateFormat D7= Dateformat.getdatetimeinstance (Dateformat.short, Dateformat.short);//Display date, time (accurate to minute)String STR7 =D7.format (now); System.out.println (After formatting the time with Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short): "+STR7); DateFormat D8= Dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.medium);//Display date, time (accurate to minute)String str8 = D8.format (now);//This approach is best compared to the short styleSYSTEM.OUT.PRINTLN ("Format time with Dateformat.getdatetimeinstance (Dateformat.medium,dateformat.medium):" +str8); DateFormat D9=dateformat.getdatetimeinstance (Dateformat.long, Dateformat.medium); String STR9=D9.format (now); System.out.println ("Dateformat.getdatetimeinstance (Dateformat.long, dateformat.medium)" +STR9); Calendar CA=calendar.getinstance (); intYear = Ca.get (calendar.year);//Get year intmonth = Ca.get (calendar.month);//Get Month intDay = Ca.get (calendar.date);//Get Day intminute = Ca.get (Calendar.minute);//points inthour = Ca.get (Calendar.hour);//hours intSecond = Ca.get (Calendar.second);//seconds intWeekOfYear =Ca.get (Calendar.day_of_week); System.out.println ("Use Calendar.getinstance (). GetTime () mode to display time:" +ca.gettime ()); System.out.println ("Calendar get Date is:" + year + "years" + month + "Month" + Day + "Days"); System.out.println ("Use calendar to get time is:" + hour + "when" + Minute + "min" + second + "seconds"); System.out.println (weekofyear);//Show today is the day of the week (I do this example is exactly Tuesday, so the results show 2, if you run again 6 weeks, then show 6) }}
Output:
Display time in Date: Wed Sep 21:37:04 CST 2015 with dateformat.getdateinstance () After the format time is: 2015-9-9 with dateformat.getdatetimeinstance () format time is: 2015-9-9 21:37:04 with Dateformat.gettimeinstance () After the format time is: 21:37:04 with dateformat.getinstance () format time after: 15-9-9 9:37 with Dateformat.getdatetimeinstance (Dateformat.full, Dateformat.full) After the format time is: September 9, 2015 Wednesday 09:37 P.M. 04 second CST with Dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long) After the format time: September 9, 2015 09:37 P.M. 04 seconds with Dateformat.getdatetimeinstance (Dateformat.short,dateformat.short) After formatting time: 15-9-9 9:37 after formatting time with Dateformat.getdatetimeinstance (dateformat.medium,dateformat.medium):2015-9-9 21:37:04dateformat.getdatetimeinstance (Dateformat.long, Dateformat.medium)September 9, 2015 21:37:04 Use Calendar.getinstance (). GetTime () mode to display time: Wed Sep 21:37:04 CST 2015 with calendar date is: August 9, 2015 get time with calendar is: 9:37 4 seconds 4
Java Date API Demo