在 Java 中顯示三位數的年中的天數
使用 ‘j’ 日期轉換字元顯示年中的三位數天數。
System.out.printf("Three-digit Day of the Year: %tj/%Tj
", d, d);上面,d 是個日期物件 −
Date d = new Date();
以下是示例 −
示例
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Demo {
public static void main(String[] args) throws Exception {
Date d = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String format = dateFormat.format(d);
System.out.println("Current date and time = " + format);
System.out.printf("Four-digit Year = %TY
",d);
System.out.printf("Two-digit Year = %ty
",d);
System.out.printf("Three-digit Day of the Year: %tj/%Tj
", d, d);
}
}輸出
Current date and time = 26/11/2018 12:13:14 PM Four-digit Year = 2018 Two-digit Year = 18 Three-digit Day of the Year: 330/330
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP