This article explains the time in Java in the acquisition and formatting,
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 output only in the year-month-day mode, and can be used in the following two ways:
1, convert () conversion 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;
Then define the 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"));
Methods for obtaining 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 ();//You can modify the int year
= C.get (calendar.year) individually for each time field;
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 you convert a string of time to a date type, and you can convert the time string you just obtained to the date type in the following ways.
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 ();
}
Thank you for reading, I hope to help you, thank you for your support for this site!