在Java中,如何格式化24小时格式的时间??
此示例使用SimpleDateFormat
类的sdf.format(date)
方法将时间格式化为24小时格式(00:00-24:00
)。
package com.yiibai; import java.text.SimpleDateFormat; import java.util.*; public class FormatTimeIn24Hour { public static void main(String[] args) { // 示例1 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("h"); System.out.println("hour in h format : " + sdf.format(date)); // 示例2 Date d2 = new Date(); SimpleDateFormat simpDate2; simpDate2 = new SimpleDateFormat("kk:mm:ss"); System.out.println(simpDate2.format(d2)); // 示例3 Date d3 = new Date(); SimpleDateFormat simpDate3; simpDate3 = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); System.out.println(simpDate3.format(d3)); } }
上述代码示例将产生以下结果,结果将根据当前系统时间而有变化。
hour in h format : 20
20:43:15
2017-09-17 20:43:15