在 Java 中使用 Formatter 顯示完整日期和時間資訊


首先,建立一個 Formatter 和 Calendar 物件。

Formatter f = new Formatter();
Calendar cal = Calendar.getInstance();

現在顯示當前日期和時間。我們在此以小寫和大寫顯示日期 −

f = new Formatter();
System.out.println(f.format("
Date and Time (lowercase): %tc
", cal)); f = new Formatter(); System.out.println(f.format("Date and Time (uppercase): %Tc
", cal));

以下是一個示例 −

示例

使用 Live Demo

import java.util.Calendar;
import java.util.Formatter;
public class Demo {
   public static void main(String args[]) {
      Formatter f = new Formatter();
      Calendar cal = Calendar.getInstance();
      System.out.println("Current date and time: "+cal.getTime());
      f = new Formatter();
      System.out.println(f.format("
Date and Time (lowercase): %tc
", cal)); f = new Formatter(); System.out.println(f.format("Date and Time (uppercase): %Tc
", cal)); } }

輸出

Current date and time: Mon Nov 26 07:24:21 UTC 2018

Date and Time (lowercase): Mon Nov 26 07:24:21 UTC 2018

Date and Time (uppercase): MON NOV 26 07:24:21 UTC 2018

更新於: 30-Jul-2019

110 Views

開創您的 事業

完成課程,獲得認證

開始
廣告