I. Get the current system time and date and format the output:
Import Java.util.date;import Java.text.simpledateformat;public class Nowstring {public static void Main (string[] args) { SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format System.out.println ( Df.format (new Date ()));//new Date () to get 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:
Enter two Java packages first: <br>import java.util.date;import java.text.simpledateformat;<br>//Then: Define the date format:<br> 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"));
************************************************************
Methods for getting the current date and time in Java
Import Java.util.Date; Import Java.util.Calendar; Import Java.text.SimpleDateFormat; public class testdate{public static void Main (string[] args) {Date now = new Date (); SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");//You can easily modify the date format String hehe = Dateformat.format (now); SYSTEM.OUT.PRINTLN (hehe); Calendar C = calendar.getinstance ();//Can be modified individually for each time domain int year = c.get (calendar.year); int month = C.get (calendar.month); int date = C.get (calendar.date); int hour = C.get (Calendar.hour_of_day); int minute = C.get (Calendar.minute); int second = C.get (Calendar.second); SYSTEM.OUT.PRINTLN (Year + "/" + month + "/" + Date + "" +hour + ":" +minute + ":" + second); }
}//Sometimes to convert a string of time to a date type, 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.Date Time=null; try {time= Sdf.parse (Sdf.format (New Date ())); } catch (ParseException e) {e.printstacktrace (); }
MySQL Time type