Android计时功能


1.设置开始时间:


// 设置请求买单的时间
		String createTime = order.getCreateTime();
		SimpleDateFormat tableDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date tableDate = null;
		try {
			tableDate = tableDateFormat.parse(createTime);
		} catch (ParseException e) {
			tableDate = new Date();
		}
		SimpleDateFormat tableFormat = new SimpleDateFormat("HH:mm:ss");
		String tableTime = tableFormat.format(tableDate);
		tvTableTime.setText(tableTime);

		// 设置买单计时
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		//1.以服务器时间为准开始计时
		// try {
		// startData = dateFormat.parse(createTime);
		// } catch (ParseException e) {
		// startData = new Date();
		// } finally {
		// 开始计时
		// new Thread(this).start();
		// }
		
		//2.以手机时间为准开始计时
		startData = new Date();
		new Thread(this).start();




2.更新到UI显示效果:


@Override
	public void run() {
		while (!isStop) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				String time = StringUtils.calcCurrentTime(startData);
				Message msg = handler.obtainMessage();
				Bundle bundle = new Bundle();
				bundle.putString("time", time);
				msg.setData(bundle);
				msg.sendToTarget();
			}
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			Bundle bundle = msg.getData();
			String time = bundle.getString("time");
			tvTime.setText(time);
		};
	};




3.与手机时间比对计算时间差并转换成时分秒(优点 :节省流量缺点:手机时间可能不是正常时间,因为可以随便改):


/**
	 * 计算当前时间差
	 * 
	 * @param startTime
	 * @return
	 */
	public static String calcCurrentTime(Date startData) {
		Date now = new Date(System.currentTimeMillis());// 获取当前时间
		long l = now.getTime() - startData.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		return String.format("%02d:%02d:%02d", hour, min, s);
	}




4.与官网或网络时间比对计算时间差并转换成时分秒(优点 :时间准确缺点:每秒要请求一次服务端):


/**
	 * 计算当前时间差
	 * 
	 * @param startTime
	 * @return
	 */
	@SuppressWarnings("finally")
	public static String calcCurrentTime(Date startData) {
		Date now = null;// 获取当前时间
		try {
			now = getNetDate();
		} catch (IOException e) {
			now = new Date(System.currentTimeMillis());// 获取当前时间
		} finally {
			long l = now.getTime() - startData.getTime();
			long day = l / (24 * 60 * 60 * 1000);
			long hour = (l / (60 * 60 * 1000) - day * 24);
			long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
			long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
			return String.format("%02d:%02d:%02d", hour, min, s);
		}

	}

	/**
	 * 获取网络时间
	 * 
	 * @return
	 * @throws IOException
	 */
	private static Date getNetDate() throws IOException {
		URL url = new URL("http://open.baidu.com/special/time/");// 取得资源对象
		URLConnection uc = url.openConnection();// 生成连接对象
		uc.connect();// 发出连接
		long ld = uc.getDate(); // 取得网站日期时间
		Date date = new Date(ld); // 转换为标准时间对象
		return date;
	}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值