Ⅰ java中獲取系統當前時間該怎麼寫
一. 獲取當前系統時間和日期並格式化輸出:x0dx0ax0dx0aimport java.util.Date; x0dx0aimport java.text.SimpleDateFormat;x0dx0ax0dx0apublic class NowString { x0dx0a public static void main(String[] args) { x0dx0a SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式x0dx0a System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間x0dx0a } x0dx0a} x0dx0ax0dx0a二. 在資料庫里的日期只以年-月-日的方式輸出,可以用下面兩種方法:x0dx0ax0dx0a1、用convert()轉化函數:x0dx0ax0dx0aString sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between -4-10' and -4-25'";x0dx0ax0dx0aSystem.out.println(rs.getString("convertBookDate")); x0dx0ax0dx0a2、利用SimpleDateFormat類:x0dx0ax0dx0a先要輸入兩個java包:x0dx0ax0dx0aimport java.util.Date; x0dx0aimport java.text.SimpleDateFormat;x0dx0ax0dx0a然後:x0dx0ax0dx0a定義日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);x0dx0ax0dx0asql語句為:String sqlStr = "select bookDate from roomBook where bookDate between -4-10' and -4-25'";x0dx0ax0dx0a輸出:x0dx0ax0dx0aSystem.out.println(df.format(rs.getDate("bookDate")));
Ⅱ java中如何取系統時間精確到秒
1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式 System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間
2 Calendar c = Calendar.getInstance();//可以對每個時間域單獨修改
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);
3 Date nowTime = new Date(System.currentTimeMillis());
SimpleDateFormat
sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String
retStrFormatNowDate = sdFormatter.format(nowTime);
Ⅲ java怎樣獲得系統當前的年份
public static String getSysYear() {
Calendar date = Calendar.getInstance();
String year = String.valueOf(date.get(Calendar.YEAR));
return year;
}
獲取當前系統時間和日期並格式化輸出:
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");//設置日期格式
System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間
}
}
Ⅳ java怎麼獲取當前系統時間並寫進mysql資料庫,如下:獲取得到的時間跟系統不一樣,不知道為什麼求指教
new Date(0)改成new Date()
new Date()是系統時間,
Date()
分配 Date 對象並初始化此對象,以表示分配它的時間(精確到毫秒)。
它的實際代碼是:
public Date()
{
this(System.currentTimeMillis()); //可以看出他也是調用的Date(long date)構造函數,傳入的參數是System.currentTimeMillis()),從1970..到現在的毫秒數
}
new Date(0)是距離1970年1月1日 n毫秒的日期,api說明:
Date(long date)
分配 Date 對象並初始化此對象,以表示自從標准基準時間(稱為「歷元(epoch)」,即 1970 年 1 月 1 日 00:00:00 GMT)以來的指定毫秒數。
Ⅳ 【Java】怎樣獲取當前系統時間,需要的格式為yyyy-MM-dd HH:mm:ss
1、打開Eclipse的主界面,需要通過圖示的按鈕來引入java包。