Convert into seconds
The date class has a gettime () that can swap back the number of seconds, for example:
Public class datetosecond{publicstaticvoidnew Date ( System.currenttimemillis ()); System.out.println (Date.gettime ());}}
Convert to string format
I. Get the current system time and date and format the output:
Import java.util.Date; Import Java.text.SimpleDateFormat; Public class nowstring {publicstaticvoidnew SimpleDateFormat (" Yyyy-mm-dd HH:mm:ss "); // Set Date format System.out.println (Df.format (new Date ())); // new Date () to get the current system time }}
Two. The date in the database is only output in the form of year-month-day, which can be used in two ways:
1. CONVERT () Convert function:
String sqlst = "SELECT CONVERT (varchar), bookdate,126) as convertbookdate from RoomBook where bookdate between ' 2007-4-1 0 ' and ' 2007-4-25 '; System.out.println (rs.getstring ("convertbookdate"));
2, the use of SimpleDateFormat class:
To enter two Java packages first:
Import Java.util.Date;
Import Java.text.SimpleDateFormat;
And then:
Define date format: SimpleDateFormat SDF = new SimpleDateFormat (YY-MM-DD);
The SQL statement is: String sqlstr = "Select Bookdate from RoomBook where bookdate between ' 2007-4-10 ' and ' 2007-4-25 '";
Output:
System.out.println (Df.format (Rs.getdate ("bookdate"));
************************************************************
Get each event domain separately
Importjava.util.Date;ImportJava.util.Calendar;ImportJava.text.SimpleDateFormat; Public classtestdate{ Public Static voidMain (string[] args) {Date now=NewDate (); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");//easy to modify date formatString hehe=Dateformat.format (now); SYSTEM.OUT.PRINTLN (hehe); Calendar C= Calendar.getinstance ();//can be modified individually for each time domain intYear =C.get (calendar.year);intmonth =C.get (calendar.month);intDate =C.get (calendar.date);inthour =C.get (calendar.hour_of_day);intminute =C.get (calendar.minute);intSecond =C.get (Calendar.second); System.out.println ( Year+ "/" + month + "/" + Date + "" +hour + ":" +minute + ":" +second); } }
Sometimes you convert a string of time to a date type, and you can convert the time string you just got to date type in the following way.
SimpleDateFormat sdf=New SimpleDateFormat ("Yyyy-mm-dd"); java.util.Datetime =null; Try {time = Sdf.parse (Sdf.format (newcatch (ParseException e) {E.printstacktrace ();}
Gets the current system time in Java, and converts the format to the string, or to the number of seconds, days, and so on integer