Public classDemo01 {in//javaIntroduction to the Date class and calendar Public Static voidMain (string[] args) {Longnow=System.currenttimemillis (); System.out.println ("Now=" +Now ); Date D1=NewDate (now); System.out.println ("D1=" +D1); Calendar C1=calendar.getinstance (); System.out.println ("C1=" +c1.gettime ()); }}
2. Basic operation of time and date
ImportJava.text.Format;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classDemo02_date_string_calendar { Public Static voidMain (string[] args)throwsparseexception {Date D1=NewDate (System.currenttimemillis ()); Calendar C1=calendar.getinstance ();//It's all about getting the current system time .//FormattingSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); System.out.println ("D1= before formatting" +d1.tostring ()); System.out.println ("D1= before formatting" +Sdf.format (D1)); //string and date conversionsString date= "2015-11-16"; SimpleDateFormat SDF1=NewSimpleDateFormat ("Yyyy-mm-dd"); Date D2=sdf1.parse (date);//use the parse function to convert a string type to a date type//you need to create an object to receive the converted objectSystem.out.println ("string converted to date,d2=" +D2); //Date converted to StringString s1=Sdf1.format (D2); System.out.println ("Date is converted to string type, s1=" +S1); }}
Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.GregorianCalendar; Public classDemo03 { Public Static voidMain (string[] args)throwsParseException {//Set Birthday StringString birthday= "2015-05-30"; //Convert a string type to a dateSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Date=sdf.parse (birthday); //to calculate a dateDate spec=preparebirthdayday (date); //Convert calculation results to string type outputString S2=sdf.format (spec);//Dateto StringSystem.out.println (birthday+ "2 weeks ahead of Saturday for:" +S2); } /*** Pass in a date that calculates the date of week 6 of 2 weeks before the date * *@parambirthdaydate Incoming Date *@return2 weeks before the date of Saturday*/ Public StaticDate preparebirthdayday (date birthdaydate) {Calendar cal=NewGregorianCalendar ();//Parent class declaration Create subclass ObjectCal.settime (birthdaydate);//Receive incoming date dates converted to calendarCal.add (Calendar.week_of_month,-2);//the first week of the month 2 weeks in advanceCal.set (Calendar.day_of_week, calendar.saturday);//adjust to the current week of Week 6 returnCal.gettime ();//Calendar converted to date }}
Java date and time processing/date,string,calendar conversion