美文网首页
Android中获取时间的几种方式

Android中获取时间的几种方式

作者: 古道寒鸦 | 来源:发表于2017-04-01 17:42 被阅读0次

获取毫秒的方式

  • 第一种方式
    long longTime=System.currentTimeMillins()
  • 第二种方式
    Calendar calendar=Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,13); calendar.set(Calendar.MINUTE,30); calendar.getTimeInMillins();//获取当天13:30对应的毫秒

获取时分秒的方式

  • 第一种方式
    Calendar calendar=Calendar.getInstance(); calendar.setTime(longTime); int hour=calendar.get(Calendar.HOUR);//获取小时 int minute=calendar.get(Calendar.MINUTE);//获取分 int second=calendar.get(Calendar.SECOND);//获取秒
  • 第二种方式
    Time time=new Time();//或者new Time("GMT-8"); time.setToNow();//设置当前时间 int hour=time.hour;//0-23 int minute=time.minute; int second=time.second;

获取特定格式的时间

DataFormat dataFormat=new SimpleDataFormat(yyyy-MM-dd kk:mm,Locale.getDefault()); String date=dataFormat.format(longTime);//获取例如 2017-4-1 17:38这样的时间

相关文章

网友评论

      本文标题:Android中获取时间的几种方式

      本文链接:https://www.haomeiwen.com/subject/ygoiottx.html