導航:首頁 > 編程語言 > java獲取當前系統時間

java獲取當前系統時間

發布時間:2025-04-24 14:46:33

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;

}

(3)java獲取當前系統時間擴展閱讀:

獲取當前系統時間和日期並格式化輸出:

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包。

閱讀全文

與java獲取當前系統時間相關的資料

熱點內容
雲伺服器建設方案 瀏覽:986
jquery源碼講解 瀏覽:280
寶馬app如何發帖 瀏覽:861
重慶伺服器託管商雲空間 瀏覽:439
浦發銀行app如何調流水 瀏覽:677
玉石鑒賞pdf 瀏覽:842
為什麼小度APP一直連不上網路 瀏覽:163
pdf模板java 瀏覽:40
現代瑞納的壓縮比 瀏覽:128
網吧里的ftp伺服器有什麼用 瀏覽:872
程序員年終總結工作體會 瀏覽:153
pdf可以直接列印 瀏覽:661
android刷wp8 瀏覽:912
歷史地圖集pdf 瀏覽:926
快手app極速版怎麼掃碼 瀏覽:805
qq程序員玩法 瀏覽:96
1是什麼門電路app 瀏覽:867
博之輪運動手錶用什麼app 瀏覽:646
asp視頻聊天源碼 瀏覽:85
網路游戲編程pdf 瀏覽:534