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

现总结如下:

方法一:

1
2
3
4
5
6
7
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);
  }

方法二:

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

方法三:

1
2
3
4
5
6
7
8
9
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);
  }

方法四:

1
2
3
4
5
6
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);
  }

获取星期日期:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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类实现,其中的日期格式不能够自己随意定义,主要有以下几种形式:

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

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

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
8
9
10
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获取时间差的方法

有些时候我们需要获取当前时间和某个时间之间的时间差,这时如何获取呢?

1. 引用如下命名空间:

1
2
import java.util.Date;
import android.text.format.DateFormat;

2. 设置时间格式:

1
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

3. 获取时间:

1
2
3
4
Date curDate = new Date(System.currentTimeMillis());
//PROCESSING
Date endDate = new Date(System.currentTimeMillis());
long diff = endDate.getTime() - curDate.getTime();

这样获取的就是时间间隔了,并且是ms级别的。

希望本文所述对大家的Android程序设计有所帮助。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

二、获取网络时间
 很多时候,由于手机的不确定性,导致各个手机的时间都不尽相同,如果开发中需要获取统一的时间来匹配一些规则的时候,比如我之前公司就是通过或去当前时间并且转换之后来匹配网络连接的规则,防止大批量的抓包,那么这个时候就需要一个统一的时间和后台进行匹配,这是简单的使用获取系统时间就可能存在问题,用户手机如果调成自动获取网络时间的话没问题,但是如果不是呢?所以获取网络时间就派上了用处

1
2
3
4
5
6
7
8
9
10
URL url = null;//取得资源对象
try {
  url = new URL("http://www.baidu.com");
  URLConnection uc = url.openConnection();//生成连接对象
  uc.connect(); //发出连接
  ld = uc.getDate(); //取得网站日期时间
  Logger.i(TAG,"ld---->>>>"+ld);
} catch (Exception e) {
  e.printStackTrace();
}

Android获取系统时间的多种方法的更多相关文章

  1. Android获取系统时间方法的总结

    Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...

  2. Android 获取系统时间和网络时间

    有些时候我们的应用中只能使用网络时间,而不能使用系统的时间,这是为了避免用户关闭了使用网络时间的功能后所产生的误差. 直接上代码. 1.清单文件中网络添加权限. <!-- 访问Internet资 ...

  3. WPF 获取系统 DPI 的多种方法

    原文:WPF 获取系统 DPI 的多种方法 WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 ...

  4. Android获取系统时间yyyyMMddHHmmssSSS

    代码改变世界 public String testTime1() throws ParseException { String DEFAULT_TIME_FORMAT = "yyyy-MM- ...

  5. (转+原)android获取系统时间

    参考的网站如下: http://c.biancheng.net/cpp/html/144.html http://www.linuxidc.com/Linux/2012-03/55909.htm 代码 ...

  6. android获取系统应用大小的方法

    <span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-s ...

  7. Android获取网络时间的方法

    一.通过免费或者收费的API接口获取 1.免费 QQ:http://cgi.im.qq.com/cgi-bin/cgi_svrtime 淘宝:http://api.m.taobao.com/rest/ ...

  8. C++11新特性,利用std::chrono精简传统获取系统时间的方法

    一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...

  9. VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

随机推荐

  1. C++中值传递(pass-by-value)和引用传递(pass-by-reference)

    1.pass-by-value的情况: 缺省情况C++以pass-by-value(继承C的方式)传递对象至(或来自)函数.函数参数都是以实际参数的复件为初值,调用端所获得的也是函数返回值的一个复件, ...

  2. jbox如果弹不出,放在body里

    body> <form id="form1" runat="server"> <script type="text/javas ...

  3. [Offer收割]编程练习赛32

    气泡图 两两判断关系,dfs. #include<stdio.h> #include<string.h> #include<stdlib.h> #include&l ...

  4. javaweb 之 代理模式

    一.动态代理 1.1.代理模式 什么是代理模式及其作用 Proxy Pattern(即:代理模式),23种常用的面向对象软件的设计模式之一 代理模式的定义:为其他对象提供一种代理以控制对这个对象的访问 ...

  5. 【Oracle】三种方式查看SQL语句的执行计划

    查看执行计划的方式有三种: EXPLAIN PLAN .V$SQL_PLAN .SQL*PLUS AUTOTRACE 1.EXPLAIN PLAN: 显示执行相应语句时可以使用的理论计划 读取执行计划 ...

  6. 【转载】Java实现word转pdf

    最近遇到一个项目需要把word转成pdf,GOOGLE了一下网上的方案有很多,比如虚拟打印.给word装扩展插件等,这些方案都依赖于ms word程序,在java代码中也得使用诸如jacob或jcom ...

  7. Python基础:编码

    1:先说python2py2里默认编码是ascii文件开头那个编码声明是告诉解释这个代码的程序 以什么编码格式 把这段代码读入到内存,因为到了内存里,这段代码其实是以bytes二进制格式存的,不过即使 ...

  8. 洛谷P3958 奶酪 并查集

    两个空洞可互达当且仅当两个空洞相切,即球心距离小于等于球的直径. 一一枚举两个可互达的空洞,并用并查集连起来即可. Code: #include<cstdio> #include<c ...

  9. 【数据分析学习】Pandas学习记录

    import pandas as pd path = r'F:\数据分析专用\数据分析与机器学习\food_info.csv' with open(path, 'r') as f: data = pd ...

  10. K3 新单到老单关联字段的添加

    新单到老单字段的添加分为两种: 一种为文本字段信息的关联,新单与老单字段的信息均为文本字段: 另一种为基础资料信息的关联,新单与老单均为基础资料字段信息.       K3 WISE 11.0中存储老 ...