java生成當前時間
Ⅰ java中獲得當前時間(yyyy-mm-dd)
public static String getSystemTime()
{
return new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
}
這個方法就可以得到, 靜態的, 用類名.方法名.
Ⅱ 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怎麼獲取當前時間
/**
* 獲取系統當前時間 <br>
* 方 法 名:getCurrentDate<br>
*
* @param formatStr
* 需要格式的目標字元串例:yyyy-MM-dd
* @return Date 時間對象
*/
publicstatic Date getCurrentDate() {
returnnew Date();
}
publicString getTodayString() {
Calendarca = Calendar.getInstance();
StringcurrDate = ca.get(Calendar.YEAR) + "-"
+(ca.get(Calendar.MONTH) + 1) + "-"
+ca.get(Calendar.DAY_OF_MONTH);
intweek = ca.get(Calendar.DAY_OF_WEEK);
Stringweekday = "";
if(week == 1) {
weekday= "星期天";
}else if (week == 2) {
weekday= "星期一";
}else if (week == 3) {
weekday= "星期二";
}else if (week == 4) {
weekday= "星期三";
}else if (week == 5) {
weekday= "星期四";
}else if (week == 6) {
weekday= "星期五";
}else if (week == 7) {
weekday= "星期六";
}
returncurrDate + " " + weekday;
}
Ⅳ java 獲取本機當前時間並對小時任意加減
代碼如下:
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
publicclassDateTestUtil{
publicstaticvoidmain(String[]args)throwsException{
SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");
Stringstr="20110823";
Datedt=sdf.parse(str);
CalendarrightNow=Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.YEAR,-1);//日期減1年
rightNow.add(Calendar.MONTH,3);//日期加3個月
rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
Datedt1=rightNow.getTime();
StringreStr=sdf.format(dt1);
System.out.println(reStr);
}
}
註:塵山豎在Calendar對象派大的add方法中,第二個參數為正數表示「加」,負數表示「減」。