当前位置:   article > 正文

Android 获取系统时间、网络时间、时区时间_android timezone.gettimezone

android timezone.gettimezone

Android 获取网络时间、时区时间、同步时间

 

方法一:SimpleDateFormat  24小时制

  1. SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2. formart.setTimeZone(TimeZone.getTimeZone("GMT+08"));
  3. String date = dff.format(new Date());

 

方法二:Calendar  非24小时制

  1. Calendar calendar = Calendar.getInstance();
  2. SimpleDateFormat formart = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
  3. formart.setTimeZone(TimeZone.getTimeZone("GMT+8"));
  4. String time = sdf.format(calendar.getTime());

 

方法三: GregorianCalendar 

  1. public static String getLocalDatetime(String local) {
  2. Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone(local));
  3. calendar.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
  4. String date = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
  5. String time = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND);
  6. return date + " " + time;
  7. }

 

方法四:获取网络时间(http://www.bjtime.cn

  1. // 取得 bjtime url
  2. URL url = new URL("http://www.bjtime.cn");
  3. // 生成 URLConnection对象
  4. URLConnection connection = url.openConnection();
  5. connection.connect();
  6. // 取得网站日期时间
  7. long date = connection.getDate();
  8. // 转换为标准时间对象
  9. Date date = new Date(date);
  10. String dateTime = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());

 

方法五:获取本地(当地)时间 LocationManager

  1. LocationManager manager = (LocationManager) this.getSystemService(MainActivity.LOCATION_SERVICE);
  2. long networkTime = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
  3. // 获取当前时间
  4. manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
  5. // 当我们使用requestLocationUpdates时,我们需要实现LocationListener接口。
  6. // 在LocationListen的回调onLocationChanged当中获取时间
  7. @Override
  8. public void onLocationChanged(Location location) {
  9. long time = location.getTime();
  10. Date date = new Date(time);
  11. Sring dateTime = time + " NETWORK_PROVIDER " + date);
  12. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/294946
推荐阅读
相关标签
  

闽ICP备14008679号