1 用java中Date类与Formatter进行格式化即可,代码如下:
Date date=new Date(); SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss"); System.out.println(formatter.format(date));
2 用System.currentTimeMillis()方法
返回的是当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。
1 long time=System.currentTimeMillis(); 2 System.out.println(time); //这行显示毫秒数 3 4 final Calendar calendar=Calendar.getInstance(); 5 calendar.setTimeInMillis(time); 6 int mHour=calendar.get(Calendar.HOUR); 7 int mMinuts=calendar.get(Calendar.MINUTE); 8 System.out.println(mHour+"点"+mMinuts+"分");
用这个方法还可以记录一些时间间隔,比如某个线程联网的时间间隔确定在4000 毫秒如下:
new Thread() { public void run() String path = "http://192.168.1.103:8080/updata.json"; Message msg = Message.obtain(); // 记录联网的开始时间 long start = System.currentTimeMillis(); try { 进行联网操作... } catch (Exception e) { e.printStackTrace(); msg.what = URL_ERROR;} finally { // 指定睡眠时间,请求网络的时长超过4秒则不做处理 // 请求网络时间小于4秒,强制为4秒 long end = System.currentTimeMillis(); try { Thread.sleep(4000 - (start - end)); } catch (Exception e) { e.printStackTrace(); } } }; }.start();
3 随着我的成长我会不断更新的哦!