栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

Android 获取系统时间、网络时间、时区时间

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 获取系统时间、网络时间、时区时间

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

 

方法一:SimpleDateFormat  24小时制

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

 

方法二:Calendar  非24小时制

Calendar calendar = Calendar.getInstance();

SimpleDateFormat formart = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");

formart.setTimeZone(TimeZone.getTimeZone("GMT+8"));

String time = sdf.format(calendar.getTime());

 

方法三: GregorianCalendar 

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

 

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

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

 

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

LocationManager manager = (LocationManager) this.getSystemService(MainActivity.LOCATION_SERVICE);
long networkTime = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();
// 获取当前时间
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 

// 当我们使用requestLocationUpdates时,我们需要实现LocationListener接口。
// 在LocationListen的回调onLocationChanged当中获取时间

@Override
public void onLocationChanged(Location location) {
    long time = location.getTime();
    Date date = new Date(time);
    Sring dateTime = time + " NETWORK_PROVIDER " + date);
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268623.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号