使用线程,输出连续10s的时间,按要求格式:
package ca.bb; import java.text.SimpleDateFormat; import java.util.Locale; /** * 时间计数类 * */ public class Timer { /** * 实现10s时间的输出 * */ public static void timer(){ int count = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while(count < 10){ try { Thread.sleep(1000); System.out.println(sdf.format(System.currentTimeMillis())); count++; } catch (InterruptedException e) { System.out.println("Thread Interrupted Exception"+e.getMessage()); } } } public static void main(String[] args) { timer(); long currentTime = System.currentTimeMillis(); //大写E:表示星期几;大写S:表示毫秒;小写a:表示上下午 //大写HH:表示24小时制,小写表示12制 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S E a"); SimpleDateFormat sdf1 = new SimpleDateFormat("MMM d,yyyy KK:mm:ss a"); //MMM:月份 local获取地区 SimpleDateFormat sdf2 = new SimpleDateFormat("MMM d,yyyy KK:mm:ss a",Locale.ENGLISH); System.out.println(sdf.format(currentTime)); System.out.println(sdf1.format(currentTime)); System.out.println(sdf2.format(currentTime)); } }此处为中国,输出为:
2015-05-06 23:17:31
2015-05-06 23:17:32
2015-05-06 23:17:33
2015-05-06 23:17:34
2015-05-06 23:17:35
2015-05-06 23:17:36
2015-05-06 23:17:37
2015-05-06 23:17:38
2015-05-06 23:17:39
2015-05-06 23:17:40
2015-05-06 23:17:40:524 星期三 下午
五月 6,2015 11:17:40 下午
May 6,2015 11:17:40 PM