java系統時間
⑴ java 中獲取時間怎麼格式化
這兩個方法都可以幫助你獲取和格式化Java中的時間,以滿足不同的需求。如果你需要獲取當前系統時間,只需創建一個新的Date對象並配合SimpleDateFormat進行格式化即可。
獲取當前系統時間:
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中對時間進行格式化處理。
⑵ 【Java】怎樣獲取當前系統時間,需要的格式為yyyy-MM-dd HH:mm:ss
1、打開Eclipse的主界面,需要通過圖示的按鈕來引入java包。
⑶ 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中獲取系統當前時間該怎麼寫
一. 獲取當前系統時間和日期並格式化輸出: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")));