Java 獲取當前日期和時間


原文地址:http://www.blogjava.net/parable-myth/archive/2013/01/17/394364.html

有三種方法:

方法一:用java.util.Date類來實現,並結合java.text.DateFormat類來實現時間的格式化,看下面代碼:

 1 import java.util.*; 
 2 import java.text.*;
 3 //以下默認時間日期顯示方式都是漢語語言方式
 4 //一般語言就默認漢語就可以了,時間日期的格式默認為MEDIUM風格,比如:2008-6-16 20:54:53
 5 //以下顯示的日期時間都是再Date類的基礎上的來的,還可以利用Calendar類來實現見類TestDate2.java
 6 public class TestDate { 
 7    public static void main(String[] args) { 
 8       Date now = new Date(); 
 9       Calendar cal = Calendar.getInstance(); 
10       
11       DateFormat d1 = DateFormat.getDateInstance(); //默認語言(漢語)下的默認風格(MEDIUM風格,比如:2008-6-16 20:54:53)
12       String str1 = d1.format(now);
13       DateFormat d2 = DateFormat.getDateTimeInstance(); 
14       String str2 = d2.format(now); 
15       DateFormat d3 = DateFormat.getTimeInstance(); 
16       String str3 = d3.format(now); 
17       DateFormat d4 = DateFormat.getInstance(); //使用SHORT風格顯示日期和時間
18       String str4 = d4.format(now);
19 
20       DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //顯示日期,周,時間(精確到秒)
21       String str5 = d5.format(now);
22       DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //顯示日期。時間(精確到秒)
23       String str6 = d6.format(now);
24       DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //顯示日期,時間(精確到分)
25       String str7 = d7.format(now);
26       DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //顯示日期,時間(精確到分)
27       String str8 = d8.format(now);//與SHORT風格相比,這種方式最好用
28 
29  
30 
31 
32       
33       System.out.println("用Date方式顯示時間: " + now);//此方法顯示的結果和Calendar.getInstance().getTime()一樣
34       
35       
36       System.out.println("用DateFormat.getDateInstance()格式化時間后為:" + str1);
37       System.out.println("用DateFormat.getDateTimeInstance()格式化時間后為:" + str2);
38       System.out.println("用DateFormat.getTimeInstance()格式化時間后為:" + str3);
39       System.out.println("用DateFormat.getInstance()格式化時間后為:" + str4);
40       
41       System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間后為:" + str5);
42       System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間后為:" + str6);
43       System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間后為:" + str7);
44       System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間后為:" + str8);
45    }
46 
47 }

運行結果:

用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類來實現,看下面:

 1 import java.util.*; 
 2 import java.text.*;
 3 //以下是利用Calendar類來實現日期時間的,和Date類相比較比較簡單
 4 
 5 public class TestDate2 { 
 6    public static void main(String[] args) { 
 7       
 8       Calendar ca = Calendar.getInstance();
 9       int year = ca.get(Calendar.YEAR);//獲取年份
10       int month=ca.get(Calendar.MONTH);//獲取月份 
11       int day=ca.get(Calendar.DATE);//獲取日
12       int minute=ca.get(Calendar.MINUTE);//
13       int hour=ca.get(Calendar.HOUR);//小時 
14       int second=ca.get(Calendar.SECOND);//
15       int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK); 
16       
17       
18       System.out.println("用Calendar.getInstance().getTime()方式顯示時間: " + ca.getTime());
19       System.out.println("用Calendar獲得日期是:" + year +"年"+ month +"月"+ day + "日");
20       
21       System.out.println("用Calendar獲得時間是:" + hour +"時"+ minute +"分"+ second +"秒");
22       System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個例子正好是周二,故結果顯示2,如果你再周6運行,那么顯示6)
23       
24    }
25 
26 }

運行結果是:
用Calendar.getInstance().getTime()方式顯示時間: Mon Jun 16 21:54:21 CST 2008
用Calendar獲得日期是:2008年5月16日    注意:這里是5月,而實際是6月,因為Calendar里的月份是0~11對應1~12月。
用Calendar獲得時間是:9時54分21秒        注意:這里是12小時制,要想獲得24小時制,需要使用 int hour=ca.get(Calendar.HOUR_OF_DAY);//小時  

方法三:

 

SimpleDateFormat 24小時制時間顯示

 

字母 日期或時間元素 表示 示例
G Era 標志符 Text AD
y Year 199696
M 年中的月份 Month JulyJul07
w 年中的周數 Number 27
W 月份中的周數 Number 2
D 年中的天數 Number 189
d 月份中的天數 Number 10
F 月份中的星期 Number 2
E 星期中的天數 Text TuesdayTue
a Am/pm 標記 Text PM
H 一天中的小時數(0-23) Number 0
k 一天中的小時數(1-24) Number 24
K am/pm 中的小時數(0-11) Number 0
h am/pm 中的小時數(1-12) Number 12
m 小時中的分鍾數 Number 30
s 分鍾中的秒數 Number 55
S 毫秒數 Number 978
z 時區 General time zone Pacific Standard TimePSTGMT-08:00
Z 時區 RFC 822 time zone

-0800

 

 

它有個優點就是支持兩位月份,兩位日期。帶前綴0.
Date date = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String sDateSuffix = dateformat.format(date);
System.out.println("[+]sDateSuffix:"+sDateSuffix);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM