java实现10s时间的输出和日期格式

使用线程,输出连续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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值