Java 程序以 K 格式显示小时(上午/下午 0-11 点)
Java Date 中的“K”格式用于以上午/下午 0-11 点格式显示小时。使用 SimpleDateFormat("K") 以获取相同格式。
// displaying hour in K format SimpleDateFormat simpleformat = new SimpleDateFormat("K"); String strHour = simpleformat.format(new Date()); System.out.println("Hour in K format = "+strHour);
在上面,我们已经使用了 SimpleDateFormat 类,因此导入了以下包 -
import java.text.SimpleDateFormat;
这是一个例子 -
示例
import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss"); System.out.println("Date and time = "+simpleformat.format(cal.getTime())); // displaying date simpleformat = new SimpleDateFormat("dd/MMMM/yyyy"); String str = simpleformat.format(new Date()); System.out.println("Current Date = "+str); // current time simpleformat = new SimpleDateFormat("HH.mm.ss"); String strTime = simpleformat.format(new Date()); System.out.println("Current Time = "+strTime); // displaying hour in K format simpleformat = new SimpleDateFormat("K"); String strHour = simpleformat.format(new Date()); System.out.println("Hour in K format = "+strHour); } }
输出
Date and time = Mon, 26 Nov 2018 10:19:28 Current Date = 26/November/2018 Current Time = 10.19.28 Hour in K format = 10
广告