創建一個日期對象
使用系統的當前日期和時間創建一個日期對象並返回一個長整數的簡單例子。 這個時間通常被稱為Java 虛擬機(JVM)主機環境的系統時間。
import java.util.Date; public class DateExample1 { public static void main(String[] args) { // Get the system date/time Date date = new Date(); System.out.println(date.getTime()); } } |
今天是星期一,2005年8月8日,上午8:43,上面的例子在系統輸出設備上顯示的結果是1123461832312。
日期數據的定制格式
使用類java.text.SimpleDateFormat和它的抽象基類 java.text.DateFormat 完成日期數據的格式定制,比方今天星期一-八月-08-2005。下面的例子展示了如何完成這個工作:
import java.text.SimpleDateFormat; import java.util.Date; public class DateExample2 { public static void main(String[] args) { SimpleDateFormat bartDateFormat = new SimpleDateFormat ("EEEE-MMMM-dd-yyyy"); Date date = new Date(); System.out.println(bartDateFormat.format(date)); } } |
只要通過向SimpleDateFormat 的構造函數傳遞格式字符串"EEE-MMMM-dd-yyyy",就能夠指明自己想要的格式。運行結果就是:星期一-八月-08-2005 了。傳遞"EE-MM-dd-yy"會顯示 星期一-08-08-05 。
將文本數據解析成日期對象
假設一個文本字符串包含了一個格式化了的日期對象,而需要解析這個字符串並從文本日期數據創建一個日期對象。下面的例子,將解析文本字符串"8-8-2005"並創建一個值為1123430400000 的日期對象。
例子程序:
import java.text.SimpleDateFormat; import java.util.Date; public class DateExample3 { public static void main(String[] args) { // Create a date formatter that can parse dates of the form MM-dd-yyyy. SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy"); // Create a string containing a text date to be parsed. String dateStringToParse = "8-8-2005"; try { // Parse the text version of the date. //We have to perform the parse method in a //try-catch construct in case dateStringToParse //does not contain a date in the format we are expecting. Date date = bartDateFormat.parse(dateStringToParse); // Now send the parsed date as a long value // to the system output. System.out.println(date.getTime()); } catch (Exception ex){ System.out.println(ex.getMessage()); } } } |
使用標准的日期格式化過程
可以生成和解析定制的日期格式后,現在來看一看如何使用內建的格式化過程。使用方法DateFormat.getDateTimeInstance()可以得到用幾種不同的方法獲得標准的日期格式化過程。在下面的例子中,我們獲取了四個內建的日期格式化過程。它們包括一個短的,中等的,長的,和完整的日期格式。
import java.text.DateFormat; import java.util.Date;
public class DateExample4 { public static void main(String[] args) { Date date = new Date(); DateFormat shortDateFormat = DateFormat.getDateTimeInstance (DateFormat.SHORT,DateFormat.SHORT); DateFormat mediumDateFormat = DateFormat.getDateTimeInstance (DateFormat.MEDIUM,DateFormat.MEDIUM); DateFormat longDateFormat = DateFormat.getDateTimeInstance (DateFormat.LONG,DateFormat.LONG); DateFormat fullDateFormat = DateFormat.getDateTimeInstance DateFormat.FULL,DateFormat.FULL);
System.out.println(shortDateFormat.format(date)); System.out.println(mediumDateFormat.format(date)); System.out.println(longDateFormat.format(date)); System.out.println(fullDateFormat.format(date)); } } |
注意我們在對 getDateTimeInstance的每次調用中都傳遞了兩個值。 第一個參數是日期風格, 而第二個參數是時間風格。 它們都是基本數據類型int(整型)。考慮到可讀性,這里使用了DateFormat 類提供的常量: SHORT, MEDIUM, LONG, 和 FULL。
運行例子程序, 它將向標准輸出設備輸出下面的內容:
05-8-8 上午9:17 2005-8-8 9:17:42 2005年8月8日 上午09時17分42秒 2005年8月8日 09時17分42秒 GMT+08:00 |
補充:如果你想要顯示“2010年08月22日 星期日 23:55:26” 可以使用
Date dd = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm:ss"); dt = sdf.format(dd);
還有一點hh:mm:ss顯示的是12制的時間,HH:mm:ss 顯示的是24制的時間