Android开发中获取系统时间的几种种方式

Android开发中获取系统时间的几种种方式

 

一.使用Calendar获取系统时间

Calendar获取系统时间首先要用Calendar.getInstance()函数获取一个实例,再为该实例设定时区(中国的时区为GMT+8:00),最后使用Calendar.get()函数获取时间的具体信息,如年,月,日,小时,分,秒,星期几。缺点是获得的这些时间信息都是独立的,如果要一起显示的话,还要组装起来凑成一个字符串,稍显麻烦。不过如果只需要单个时间信息,如星期几,这种方法是比较方便的。并且可以根据Calendar.AMPM属性判断当前是AM还是PM(0为AM,1为PM),然后根据需要显示12小时或24小时的时间。或者直接获得Calendar.HOUROF_DAY显示24小时的时间。

  • 代码

/*

* 使用Calendar获取系统时间

*/

Calendar calendars;

private void getTime4() {

calendars = Calendar.getInstance();

calendars.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));


String year = String.valueOf(calendars.get(Calendar.YEAR));

String month = String.valueOf(calendars.get(Calendar.MONTH));

String day = String.valueOf(calendars.get(Calendar.DATE));

String hour = String.valueOf(calendars.get(Calendar.HOUR));

String min = String.valueOf(calendars.get(Calendar.MINUTE));

String second = String.valueOf(calendars.get(Calendar.SECOND));

Boolean isAm = calendars.get(Calendar.AM_PM)==1 ? true:false;

Boolean is24 = DateFormat.is24HourFormat(getApplication()) ?true:false;


Log.i("md", " 年:"+year+" 月: "+month+" 日:"+day+" 时: "+hour+" 分: "+min+" 秒: "+second +" 是上午吗? "+isAm+" 是24小时制吗? "+is24);

}

 


二.使用date获取系统时间

Date方法比较简单,只需要一条语句:Date().toLocaleString(),就可以获得整个的时间信息,并且格式规范,不用再组装,可以直接显示。缺点是如果想用另外一种格式显示,或者只需要单个的时间信息,就比较麻烦。可以定义SimpleDateFormat,规定哪些信息显示,哪些信息不显示,如显示年、月、日、小时、分钟、星期几,可以定义下面的SimpleDateFormat:

01-01 03:18:24.326: I/md(17508): 年:2015 月: 0 日:1 时: 3 分: 18 秒: 24 是上午吗? false 是24小时制吗? true

代码

Date date = new Date();

String time = date.toLocaleString();

Log.i("md", "时间time为: "+time);

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒 E");

String sim = dateFormat.format(date);

Log.i("md", "时间sim为: "+sim);

 


三.使用currentTimeMills获取系统时间

1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)1秒=1,000,000 微秒(μs) 1微秒=1/1,000,000秒(s)1秒=1,000,000,000 纳秒(ns) 1纳秒=1/1,000,000,000秒(s)1秒=1,000,000,000,000 皮秒(ps) 1皮秒=1/1,000,000,000,000秒(s)1小时=60分钟=3600秒

01-01 03:31:31.458: I/md(18530): 时间time为: Jan 1, 2015 3:31:31 AM

01-01 03:31:31.459: I/md(18530): 时间sim为: 2015年-01月01日-03时31分31秒 Thu

代码

        //获得系统的时间,单位为毫秒,转换为妙
       long totalMilliSeconds = System.currentTimeMillis();
       long totalSeconds = totalMilliSeconds / 1000;
         
        //求出现在的秒
        long currentSecond = totalSeconds % 60;
         
        //求出现在的分
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
         
        //求出现在的小时
        long totalHour = totalMinutes / 60;
        long currentHour = totalHour % 24;
         
        //显示时间
        System.out.println("总毫秒为: " + totalMilliSeconds);
        System.out.println(currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");

 

拓展

在开发过程中,通常很多人都习惯使用new Date()来获取当前时间。new Date()所做的事情其实就是调用了System.currentTimeMillis()。如果仅仅是需要或者毫秒数,那么完全可以使用System.currentTimeMillis()去代替new Date(),效率上会高一点。如果需要在同一个方法里面多次使用new Date(),通常性能就是这样一点一点地消耗掉,这里其实可以声明一个引用。1分钟=60秒

01-01 03:18:24.316: I/md(17508): 当前时间:2015年-01月01日-03时18分24秒

代码

//获得系统的时间,单位为毫秒,转换为妙

long totalMilliSeconds = System.currentTimeMillis();


DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化输出

TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取时区 这句加上,很关键。

dateFormatterChina.setTimeZone(timeZoneChina);//设置系统时区

long totalSeconds = totalMilliSeconds / 1000;


//求出现在的秒

long currentSecond = totalSeconds % 60;

//求出现在的分

long totalMinutes = totalSeconds / 60;

long currentMinute = totalMinutes % 60;

//求出现在的小时

long totalHour = totalMinutes / 60;

long currentHour = totalHour % 24;

Log.i("md", "小时:"+currentHour+" 分钟: "+currentMinute+" 秒 :"+currentSecond);

 

  • 21
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
获取当前绑定的蓝牙设备有以下几种方法: 1. 通过BluetoothAdapter.getDefaultAdapter().getBondedDevices()获取所有已绑定的设备,再遍历获取当前连接的设备。 ```java BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices(); for (BluetoothDevice device : bondedDevices) { if (device.getBondState() == BluetoothDevice.BOND_BONDED && device.isConnected()) { // 当前连接的设备 // device.getName()获取设备名,device.getAddress()获取设备地址 } } ``` 2. 使用BroadcastReceiver监听BluetoothDevice.ACTION_ACL_CONNECTED广播,获取连接的设备。 ```java private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 连接的设备 // device.getName()获取设备名,device.getAddress()获取设备地址 } } }; // 注册广播 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED); registerReceiver(mReceiver, filter); ``` 3. 使用BluetoothProfile.ServiceListener监听BluetoothProfile.HEADSET连接状态,获取连接的设备。 ```java private final BluetoothProfile.ServiceListener mListener = new BluetoothProfile.ServiceListener() { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { if (profile == BluetoothProfile.HEADSET) { List<BluetoothDevice> devices = proxy.getConnectedDevices(); if (!devices.isEmpty()) { BluetoothDevice device = devices.get(0); // 连接的设备 // device.getName()获取设备名,device.getAddress()获取设备地址 } } } @Override public void onServiceDisconnected(int profile) { // 断开连接 } }; // 获取BluetoothHeadset BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); adapter.getProfileProxy(context, mListener, BluetoothProfile.HEADSET); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西伯利亚大橘猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值