Java中一些擷取當前日期的方法

來源:互聯網
上載者:User

記點小東西。擷取帶格式的當前日期/時間的方法。C#的ToString()果然還是我覺得最有愛的format方 式。不過指令碼語言一般也都會提供非常方便的方法,像PHP、Perl和Ruby等等都有相當不錯的方法; JavaScript雖然沒提供format方法,但自己寫一個不廢什麼事。

我們都知道C/C++裡提供了__DATE__和__TIME__宏,不過這兩個宏記錄的是編譯時間的日期和時間,而且 無法自訂格式,跟這裡討論的可以說是完全沒關係……

============================================================

DOS Batch File(on Windows NT):

主要依靠命令列的date /T與time /T命令來分別擷取當前系統日期和時間。

例子:

2008-01-05 星期六

11:20

http://www.robvanderwoude.com/datetiment.html

============================================================

Java:

Java代碼

import java.util.Date;import java.util.Locale;import java.text.DateFormat;public class Greet {public static void main(String[] args) {DateFormat df= DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, Locale.CHINA);String message = df.format(new Date());System.out.println(message);}}// 2008年1月4日 星期五 下午07:54

Java裡相關的幾個類算是java.util.Date,java.util.Time,java.util.Calendar, java.text.DateFormat,java.text.SimpleDateFormat等。

這裡的例子裡我沒有使用自訂格式,直接用了標準格式中的DateFormat.FULL所指定的格式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.