public static String getCurrentTime() {
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)深州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
java.util.Date date = new java.util.Date();
// 取本地系統(tǒng)時間:2000-10-27 09:36:58
String currTime = DateFormat.getDateTimeInstance().format(date);
return currTime;// 返回本地系統(tǒng)時間:2000/10/27 09:36:58
}
在項(xiàng)目中,當(dāng)前系統(tǒng)時間和數(shù)據(jù)庫的時間可能不同步,最好以數(shù)據(jù)庫時間為準(zhǔn)。在插入時,SQL里直接用now()
有兩種方法:
方法一:用java.util.Date類來實(shí)現(xiàn),并結(jié)合java.text.DateFormat類來實(shí)現(xiàn)時間的格式化,看下面代碼:
import java.util.*;
import java.text.*;
//以下默認(rèn)時間日期顯示方式都是漢語語言方式
//一般語言就默認(rèn)漢語就可以了,時間日期的格式默認(rèn)為MEDIUM風(fēng)格,比如:2008-6-16 20:54:53
//以下顯示的日期時間都是再Date類的基礎(chǔ)上的來的,還可以利用Calendar類來實(shí)現(xiàn)見類TestDate2.java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
DateFormat d1 = DateFormat.getDateInstance(); //默認(rèn)語言(漢語)下的默認(rèn)風(fēng)格(MEDIUM風(fēng)格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);
DateFormat d2 = DateFormat.getDateTimeInstance();
String str2 = d2.format(now);
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
DateFormat d4 = DateFormat.getInstance(); //使用SHORT風(fēng)格顯示日期和時間
String str4 = d4.format(now);
DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //顯示日期,周,時間(精確到秒)
String str5 = d5.format(now);
DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //顯示日期。時間(精確到秒)
String str6 = d6.format(now);
DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //顯示日期,時間(精確到分)
String str7 = d7.format(now);
DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //顯示日期,時間(精確到分)
String str8 = d8.format(now);//與SHORT風(fēng)格相比,這種方式最好用
System.out.println("用Date方式顯示時間: " + now);//此方法顯示的結(jié)果和Calendar.getInstance().getTime()一樣
System.out.println("用DateFormat.getDateInstance()格式化時間后為:" + str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化時間后為:" + str2);
System.out.println("用DateFormat.getTimeInstance()格式化時間后為:" + str3);
System.out.println("用DateFormat.getInstance()格式化時間后為:" + str4);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間后為:" + str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間后為:" + str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間后為:" + str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間后為:" + str8);
}
}
運(yùn)行結(jié)果:
用Date方式顯示時間: Mon Jun 16 20:54:53 CST 2008
用DateFormat.getDateInstance()格式化時間后為:2008-6-16
用DateFormat.getDateTimeInstance()格式化時間后為:2008-6-16 20:54:53
用DateFormat.getTimeInstance()格式化時間后為:20:54:53
用DateFormat.getInstance()格式化時間后為:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間后為
:2008年6月16日 星期一 下午08時54分53秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間后為
:2008年6月16日 下午08時54分53秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間后
為:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間
后為:2008-6-16 20:54:53
方法二:用java.util.Calendar類來實(shí)現(xiàn),看下面:
import java.util.*;
import java.text.*;
//以下是利用Calendar類來實(shí)現(xiàn)日期時間的,和Date類相比較比較簡單
public class TestDate2 {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//獲取年份
int month=ca.get(Calendar.MONTH);//獲取月份
int day=ca.get(Calendar.DATE);//獲取日
int minute=ca.get(Calendar.MINUTE);//分
int hour=ca.get(Calendar.HOUR);//小時
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
System.out.println("用Calendar.getInstance().getTime()方式顯示時間: " + ca.getTime());
System.out.println("用Calendar獲得日期是:" + year +"年"+ month +"月"+ day + "日");
System.out.println("用Calendar獲得時間是:" + hour +"時"+ minute +"分"+ second +"秒");
System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個例子正好是周二,故結(jié)果顯示2,如果你再周6運(yùn)行,那么顯示6)
}
}
運(yùn)行結(jié)果是:
用Calendar.getInstance().getTime()方式顯示時間: Mon Jun 16 21:54:21 CST 2008
用Calendar獲得日期是:2008年5月16日
用Calendar獲得時間是:9時54分21秒
2
總結(jié):中的來說,方法二是最方便的,方法一顯得分笨拙,不過看個人喜歡了。
還有一種方法利用System.currentTimeMillis()也可以。
查看API,
getYear
@Deprecated
public int getYear()已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.YEAR) - 1900 取代。
返回一個值,此值是從包含或開始于此 Date 對象表示的瞬間的年份減去 1900 的結(jié)果(用本地時區(qū)進(jìn)行解釋)。
getMonth
@Deprecated
public int getMonth()已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.MONTH) 取代。
返回表示月份的數(shù)字,該月份包含或開始于此 Date 對象所表示的瞬間。返回的值在 0 和 11 之間,值 0 表示 1 月
getDay
@Deprecated
public int getDay()已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.DAY_OF_WEEK) 取代。
返回此日期表示的周中的某一天。返回值 (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday) 表示一周中的某一天,該周包含或開始于此 Date 對象所表示的瞬間(用本地時區(qū)進(jìn)行解釋)。
返回:
此日期所表示的一周中的某一天。
看一下API就明白了,現(xiàn)在這個用法都過時了,參見Calendar的API
java獲取應(yīng)用的運(yùn)行時間,可以利用時間差來獲得,使用System.currentTimeMillis()該方法獲得此時的時間,代碼如下:
package?com.qiu.lin.he;
import?java.text.ParseException;
public?class?Ceshi?{
public?static?void?main(String[]?args)?throws?ParseException?{
double?begin?=?System.currentTimeMillis();?//?程序開始時間,調(diào)用系統(tǒng)的當(dāng)前時間
for?(int?i?=?0;?i??10000;?i++)?{
//?這里執(zhí)行具體的業(yè)務(wù)邏輯
System.out.println(i);
}
//?你要運(yùn)行的程序
double?end?=?System.currentTimeMillis();?//?程序結(jié)束時間,調(diào)用系統(tǒng)當(dāng)前時間
double?time?=?end?-?begin;//?程序的運(yùn)行時間
System.out.println(time?/?60?+?"秒");
}
}
運(yùn)行結(jié)果如下:
此方法為 Java 內(nèi)置的方法,使用 System.currentTimeMillis 來執(zhí)行統(tǒng)計(jì)的時間(統(tǒng)計(jì)單位:毫秒)(統(tǒng)計(jì)單位:毫秒),示例代碼如下:
public class TimeIntervalTest {
public static void main(String[] args) throws InterruptedException {
// 開始時間
long stime = System.currentTimeMillis();
// 執(zhí)行時間(1s)
Thread.sleep(1000);
// 結(jié)束時間
long etime = System.currentTimeMillis();
// 計(jì)算執(zhí)行時間
System.out.printf("執(zhí)行時長:%d 毫秒.", (etime - stime));
}
}
以上程序的執(zhí)行結(jié)果為:
執(zhí)行時長:1000 毫秒.
方法二:System.nanoTime
此方法為 Java 內(nèi)置的方法,使用 System.nanoTime 來統(tǒng)計(jì)執(zhí)行時間(統(tǒng)計(jì)單位:納秒),它的執(zhí)行方法
可以直接通過jdk基本方法,獲取到當(dāng)前的時間
Date date= new Date();//創(chuàng)建一個時間對象,獲取到當(dāng)前的時間
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置時間顯示格式
String str = sdf.format(date);//將當(dāng)前時間格式化為需要的類型
System.out.println(str);//輸出結(jié)果
結(jié)果為:2015-11-06 13:53:54(實(shí)時)。
本文標(biāo)題:windows一件系統(tǒng)的簡單介紹
鏈接地址:http://sd-ha.com/article8/hdhjop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、用戶體驗(yàn)、營銷型網(wǎng)站建設(shè)、建站公司、全網(wǎng)營銷推廣、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)