使用自定义格式格式化时间的 Java 程序
首先,使用SimpleDateFormat类设置时间
Format dateFormat = new SimpleDateFormat("h:m:s");现在,对于自定义格式,让我们分别获取小时、分钟和秒
小时
//小时
dateFormat = new SimpleDateFormat("h");
String strHour = dateFormat.format(new Date());
System.out.println("Hour: "+strHour);分钟
//分钟
dateFormat = new SimpleDateFormat("m");
String strMinute = dateFormat.format(new Date());
System.out.println("Minute: "+strMinute);第二
//第二
dateFormat = new SimpleDateFormat("s");
String strSecond = dateFormat.format(new Date());
System.out.println("Second: "+strSecond);以下是一个例子-
示例
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
public static void main(String[] argv) throws Exception {
Format dateFormat = new SimpleDateFormat("h:m:s");
String str = dateFormat.format(new Date());
System.out.println("Time: "+str);
//小时
dateFormat = new SimpleDateFormat("h");
String strHour = dateFormat.format(new Date());
System.out.println("Hour: "+strHour);
//分钟
dateFormat = new SimpleDateFormat("m");
String strMinute = dateFormat.format(new Date());
System.out.println("Minute: "+strMinute);
//第二
dateFormat = new SimpleDateFormat("s");
String strSecond = dateFormat.format(new Date());
System.out.println("Second: "+strSecond);
}
}输出结果Time: 10:58:52 Hour: 10 Minute: 58 Second: 52
热门推荐
10 六一加端午祝福语简短
11 写给兄弟的祝福语简短
12 项目建设春节祝福语简短
13 上司生日祝福语简短 女性
14 同事调动的祝福语简短
15 给老人的祝福语简短
16 春节拜年祝福语儿童简短
17 经典祝福语简短情侣句子
18 女性的生日祝福语简短