java獲取當前日期和時間的二種方法分享
更新時間:2014年03月29日 10:49:38 作者:
這篇文章主要介紹了java獲取當前日期和時間的二種方法,需要的朋友可以參考下
有兩種方法:
方法一:用java.util.Date類來實現(xiàn),并結合java.text.DateFormat類來實現(xiàn)時間的格式化,看下面代碼:
import java.util.*;
import java.text.*;
//以下默認時間日期顯示方式都是漢語語言方式
//一般語言就默認漢語就可以了,時間日期的格式默認為MEDIUM風格,比如:2008-6-16 20:54:53
//以下顯示的日期時間都是再Date類的基礎上的來的,還可以利用Calendar類來實現(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(); //默認語言(漢語)下的默認風格(MEDIUM風格,比如: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風格顯示日期和時間
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風格相比,這種方式最好用
System.out.println("用Date方式顯示時間: " + now);//此方法顯示的結果和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);
}
}
運行結果:
用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類來實現(xiàn),看下面:
import java.util.*;
import java.text.*;
//以下是利用Calendar類來實現(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);//顯示今天是一周的第幾天(我做的這個例子正好是周二,故結果顯示2,如果你再周6運行,那么顯示6)
}
}
運行結果是:
用Calendar.getInstance().getTime()方式顯示時間: Mon Jun 16 21:54:21 CST 2008
用Calendar獲得日期是:2008年5月16日
用Calendar獲得時間是:9時54分21秒
總結:中的來說,方法二是最方便的,方法一顯得分笨拙,不過看個人喜歡了。
還有一種方法利用
System.currentTimeMillis()
方法一:用java.util.Date類來實現(xiàn),并結合java.text.DateFormat類來實現(xiàn)時間的格式化,看下面代碼:
復制代碼 代碼如下:
import java.util.*;
import java.text.*;
//以下默認時間日期顯示方式都是漢語語言方式
//一般語言就默認漢語就可以了,時間日期的格式默認為MEDIUM風格,比如:2008-6-16 20:54:53
//以下顯示的日期時間都是再Date類的基礎上的來的,還可以利用Calendar類來實現(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(); //默認語言(漢語)下的默認風格(MEDIUM風格,比如: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風格顯示日期和時間
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風格相比,這種方式最好用
System.out.println("用Date方式顯示時間: " + now);//此方法顯示的結果和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);
}
}
運行結果:
復制代碼 代碼如下:
用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類來實現(xiàn),看下面:
復制代碼 代碼如下:
import java.util.*;
import java.text.*;
//以下是利用Calendar類來實現(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);//顯示今天是一周的第幾天(我做的這個例子正好是周二,故結果顯示2,如果你再周6運行,那么顯示6)
}
}
運行結果是:
復制代碼 代碼如下:
用Calendar.getInstance().getTime()方式顯示時間: Mon Jun 16 21:54:21 CST 2008
用Calendar獲得日期是:2008年5月16日
用Calendar獲得時間是:9時54分21秒
總結:中的來說,方法二是最方便的,方法一顯得分笨拙,不過看個人喜歡了。
還有一種方法利用
復制代碼 代碼如下:
System.currentTimeMillis()
相關文章
Java實戰(zhàn)之網上書店管理系統(tǒng)的實現(xiàn)
本文將利用Java語言實現(xiàn)網上書店管理系統(tǒng)。其功能一般包括:圖書信息管理、用戶信息管理、圖書購買、圖書訂單查看、圖書添加、圖書維護等等,感興趣的可以了解一下2022-06-06
Spring Cloud使用Feign進行遠程調用的操作指南
本文介紹了Feign作為聲明式HTTP客戶端在SpringCloud中的使用,從簡介、對比RestTemplate的問題、使用步驟,到日志配置、性能優(yōu)化和實際應用進行了詳細講解,包括如何通過Feign簡化接口調用,以及解決啟動時找不到FeignClient的問題,需要的朋友可以參考下2025-02-02
async-excel實現(xiàn)多sheet異步導出方法詳解
這篇文章主要介紹了async-excel實現(xiàn)多sheet異步導出方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-12-12
Java Web項目中使用Socket通信多線程、長連接的方法
很多時候在javaweb項目中我們需要用到Socket通信來實現(xiàn)功能,在web中使用Socket我們需要建立一個監(jiān)聽程序,在程序啟動時,啟動socket監(jiān)聽。接下來通過本文給大家介紹Java Web項目中使用Socket通信多線程、長連接的方法,感興趣的朋友一起學習2016-04-04
SpringBoot整合Hashids實現(xiàn)數據ID加密隱藏的全過程
這篇文章主要為大家詳細介紹了SpringBoot整合Hashids實現(xiàn)數據ID加密隱藏的全過程,文中的示例代碼講解詳細,對大家的學習或工作有一定的幫助,感興趣的小伙伴可以跟隨小編一起學習一下2024-01-01

