Android获取系统时间的多种方法

所属分类: 软件编程 / Android 阅读数: 77
收藏 0 赞 0 分享

Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现。

现总结如下:

方法一:

void getTime1(){ 
    long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis(); 
    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date d1=new Date(time); 
    String t1=format.format(d1); 
    Log.e("msg", t1); 
  } 

方法二:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss"); 
    String t=format.format(new Date()); 
    Log.e("msg", t); 

方法三:

void getTime3(){ 
  Calendar calendar = Calendar.getInstance(); 
  String created = calendar.get(Calendar.YEAR) + "年" 
      + (calendar.get(Calendar.MONTH)+1) + "月"//从0计算 
      + calendar.get(Calendar.DAY_OF_MONTH) + "日" 
      + calendar.get(Calendar.HOUR_OF_DAY) + "时" 
      + calendar.get(Calendar.MINUTE) + "分"+calendar.get(Calendar.SECOND)+"s"; 
  Log.e("msg", created); 
  } 

方法四:

void getTime4(){ 
    Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。 
    t.setToNow(); // 取得系统时间。 
    String time=t.year+"年 "+(t.month+1)+"月 "+t.monthDay+"日 "+t.hour+"h "+t.minute+"m "+t.second; 
    Log.e("msg", time); 
  } 

获取星期日期:

Calendar calendar = Calendar.getInstance(); 
      int day = calendar.get(Calendar.DAY_OF_WEEK); 
      String today = null; 
      if (day == 2) { 
        today = "Monday"; 
      } else if (day == 3) { 
        today = "Tuesday"; 
      } else if (day == 4) { 
        today = "Wednesday"; 
      } else if (day == 5) { 
        today = "Thursday"; 
      } else if (day == 6) { 
        today = "Friday"; 
      } else if (day == 7) { 
        today = "Saturday"; 
      } else if (day == 1) { 
        today = "Sunday"; 
      } 
      System.out.println("Today is:- " + today); 

最后说一下日期格式化,日期格式化通常使用SimpleDateFormat类实现,其中的日期格式不能够自己随意定义,主要有以下几种形式:

SimpleDateFormat f1= new SimpleDateFormat(); //其中没有些格式化参数,我们使用默认的日期格式。
System.out.println(f.formate(new Date()));

代码输出的日期格式为:12-3-22 下午4:36

SimpleDateFormat f4= new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分");
//可根据不同样式请求显示不同日期格式,要显示星期可以添加E参数
System.out.println(f4.format(new Date()));
//代码输出的日期格式为:今天是2012年03月22日 星期四 16点46分
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println("Date to String "+formater.format(new Date()));
//相近的常用形式还有 yyMMdd hh:mm:ss yyyy-MM-dd hh:mm:ss dd-MM-yyyy hh:mm:ss


应有的时候通常还会需要把具体日期转换为毫秒或者Timestamp形式,如下:

文本 - > Timestamp,日期 -> Timestamp

 Timestamp t ;
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 try ...{
  t = new Timestamp(format.parse("2007-07-19 00:00:00").getTime());
 } catch (ParseException e) ...{
  e.printStackTrace();
 }
 Timestamp t ;
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 t = new Timestamp(new Date().getTime());

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多