初涉江湖,还望海涵!

写点东西,纯粹是因为个人的记忆能力较弱,写些笔记罢了,若有错误还望雅正!

对Android中的时间获取做个记录,以下为结果!

代码粘贴

public class MainActivity extends AppCompatActivity {

public static final String TAG = "MainActivity";

@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//timestamp

TextView timestamp = findViewById(R.id.timestamp_show);

timestamp.setText("timestamp:" + System.currentTimeMillis());

//date

Date date = new Date();

TextView date_show = findViewById(R.id.date_show);

date_show.setText("Date:" + date.toString());

//Calendar

TextView calendar_show = findViewById(R.id.calendar_show);

Calendar calendar = Calendar.getInstance();

int year = calendar.get(Calendar.YEAR);

int month = calendar.get(Calendar.MONTH);

int day = calendar.get(Calendar.DATE);

int hour = calendar.get(Calendar.HOUR);

int minute = calendar.get(Calendar.MINUTE);

int second = calendar.get(Calendar.SECOND);

String calendar_show_string = "Calendar:" + year + "-" + month + "-" + day

+ " " + hour + ":" + minute + ":" + second;

calendar_show.setText(calendar_show_string);

//Time

TextView time_show = findViewById(R.id.time_show);

Time time = new Time();

time.setToNow();

int time_year = time.year;

int time_month = time.month;

int time_day = time.monthDay;

int time_hour = time.hour;

int time_minute = time.minute;

int time_second = time.second;

String time_show_string = "Time:" + time_year + "-" + time_month + "-"

+ time_day + " " + time_hour + ":" + time_minute + ":" + time_second;

time_show.setText(time_show_string);

//SimpleDateFormat

TextView simpleDateFormat_show = findViewById(R.id.simpleDateFormat_show);

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

String simpleDateFormat_tring = "SimpleDateFormat:" + format.format(new Date());

simpleDateFormat_show.setText(simpleDateFormat_tring);

Log.d(TAG, "onCreate: Long的最大值:" + Long.MAX_VALUE);

}

根据自己使用过的以及网上搜索得到的结果,整理记录了以下方法

1 timestamp

2 date

3 SimpleDateFormat

4 Calendar

5 Time

1 timestamp

//timestamp

TextView timestamp = findViewById(R.id.timestamp_show);

timestamp.setText("timestamp:" + System.currentTimeMillis());

timestamp,时间戳。

使用时调用System类的currentTimeMillis()方法,该方法的描述是:

/**

* Returns the current time in milliseconds. Note that

* while the unit of time of the return value is a millisecond,

* the granularity of the value depends on the underlying

* operating system and may be larger. For example, many

* operating systems measure time in units of tens of

* milliseconds.

*

*

See the description of the class Date for

* a discussion of slight discrepancies that may arise between

* "computer time" and coordinated universal time (UTC).

*

* @return the difference, measured in milliseconds, between

* the current time and midnight, January 1, 1970 UTC.

* @see java.util.Date

*/

public static native long currentTimeMillis();

可以看出,该方法返回的是long类型的结果,结果记录的是midnight, January 1, 1970 UTC至今经过的毫秒数(milliseconds)。

System.currentTimeMillis()是一个native方法,是一个C/C++方法,由系统测量时间戳并返回测量结果,根据注释描述,测量结果可能偏大,因为有些操作系统测量时间是以十毫秒为单位的,类Date中讨论了关于系统时间和UTC时间产生差异的原因,可自行观看!

Note:

UTC(coordinated universal time)是民用时间的标准,众所周知,地球围绕太阳公转一周的时间定义为一年,地球自转一周定义为一天。有科学报道说,地球漫长的公转中其实是在缓慢的接近太阳,不管是否属实,自转和公转会产生一些变化也是不可避免的,UTC就是正确测量时间的规则,当测量到需要校正时间时,会以毫秒为单位进行调整,称之为闰秒(leap seconds),后面Time会提到!

System.currentTimeMillis()的返回结果是一个记录从1970开始的毫秒数的long型结果,最容易想到的是long是有范围区间的,如果有一天记录的毫秒数超出long的范围怎么办!所以我计算了以下,long的最大值为0x7fff,ffff,ffff,ffff,取整大约为922亿亿,一年算365天,不考虑闰年,一天246060*60毫秒一年取整大约18亿毫秒,922亿/18,大约为50亿年,考虑到太阳的寿命,貌似也有用尽的一天。。。。但是,那么长的时间,鬼知道会发展成什么样!

2 Date

//date

Date date = new Date();

TextView date_show = findViewById(R.id.date_show);

date_show.setText("Date:" + date.toString());

通过实例化Date类获取date实例从而获取时间,简单通过toString()打印结果

Date类的注释特别描述了

日历记时中,一年定为365天,闰年多一天,这表明,时间并不总是一天246060*60毫秒,需要用闰年加一天来调整。在coordinated universal time (UTC)的时间定义中,是通过闰秒(leap second)来调整时间的,并且总是在6月30日或12月31日,具体表现为该类对秒的限制在0 to 61,60和61发生在leap second时。

构造函数

public Date() {

this(System.currentTimeMillis());

}

public Date(long date) {

fastTime = date;

}

/**

* @param year the year minus 1900.

* @param month the month between 0-11.

* @param date the day of the month between 1-31.

* @param hrs the hours between 0-23.

* @param min the minutes between 0-59.

* @param sec the seconds between 0-59.

* @see java.util.Calendar

* @deprecated As of JDK version 1.1,

* replaced by Calendar.set(year + 1900, month, date,

* hrs, min, sec) or GregorianCalendar(year + 1900,

* month, date, hrs, min, sec).

*/

@Deprecated

public Date(int year, int month, int date, int hrs, int min, int sec) {

int y = year + 1900;

// month is 0-based. So we have to normalize month to support Long.MAX_VALUE.

if (month >= 12) {

y += month / 12;

month %= 12;

} else if (month < 0) {

y += CalendarUtils.floorDivide(month, 12);

month = CalendarUtils.mod(month, 12);

}

BaseCalendar cal = getCalendarSystem(y);

cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());

cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0);

getTimeImpl();

cdate = null;

}

无参大Date()直接把System.currentTimeMillis()的时间戳返回给fastTime,另一个就是设定好年月日时分秒来创建对象,其中的设定是年是1900+参数year并且也对月份超出范围做出了处理,但是该构造方法已是@Deprecated(弃用)了

Date类中大部分的方法都已经弃用,要特别是单独获取年或者月等信息的方法,基本上都已经弃用,留下的有打印即toString()和一些比较等功能性的方法

3 SimpleDateFormat

//SimpleDateFormat

TextView simpleDateFormat_show = findViewById(R.id.simpleDateFormat_show);

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

String simpleDateFormat_tring = "SimpleDateFormat:" + format.format(new Date());

simpleDateFormat_show.setText(simpleDateFormat_tring);

SimpleDateFormat类的核心是Text的formatting(格式化)和Time的parsing(解析),SimpleDateFormat()通过传入一个字符串来格式化需要的表现形式,样例中通过调用format()传入Date无参对象,实际上是调用System.currentTimeMillis()获取最基本的时间,SimpleDateFormat类的作用是把传入的Date类时间定制化封装,从而得到需要的结果。

Note:

关于SimpleDateFormat类,可以很自由的定制表现形式,年月日时分秒,时间格式,AD/BC。。。

定制化所用字母的含义:

G => AD/BC(年份为负数时),1+

y => Year,1+

Y => Week year,24+

M => Month in year,1+

w => Week in year,1+

W => Week in month,1+

D => Day in year,1+

d => Day in month,1+

F => Day of week in month,1+

E => Day name in week,1+

u => Day number of week(1 = Monday, ..., 7 = Sunday),24+

a => Am/pm marker,1+

H => Hour in day (0-23),1+

k => Hour in day (1-24),1+

K => Hour in am/pm (0-11),1+

h => Hour in am/pm (1-12),1+

m => Minute in hour,1+

s => Second in minute,1+

S => Millisecond,1+

z => Time zone:General time zone,PST,GMT-08:00,1+

Z => Time zone:RFC 822 time zone,1+

X => Time zone:ISO 8601 time zone,1+

定制化使用"字符串",在该字符串中使用'字符'表示在年月日等数据外的部分,如分隔符

SimpleDateFormat类的时间格式定制包括年月日等数据的表现形式,连接符,日期格式的描述,如Time zone,AM/PM,AD/BC。。。

SimpleDateFormat类中存在的问题是线程同步

/**

* Date formats are not synchronized.

* It is recommended to create separate format instances for each thread.

* If multiple threads access a format concurrently, it must be synchronized

* externally.

*/

SimpleDateFormat是线程不同步的,要在多线程中使用则要在线程外同步.

4 Calendar

//Calendar

TextView calendar_show = findViewById(R.id.calendar_show);

Calendar calendar = Calendar.getInstance();

int year = calendar.get(Calendar.YEAR);

int month = calendar.get(Calendar.MONTH);

int day = calendar.get(Calendar.DATE);

int hour = calendar.get(Calendar.HOUR);

int minute = calendar.get(Calendar.MINUTE);

int second = calendar.get(Calendar.SECOND);

String calendar_show_string = "Calendar:" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

calendar_show.setText(calendar_show_string);

Calendar是一个抽象类通过其内定义的Calendar.getInstance()静态方法实例化对象而该静态方法最终是通过返回一个new GregorianCalendar(zone, aLocale)来实现初始化!

Calendar类内部定义了关于时间需要用到的索引并用一个int数组存储相关数据

public final static int ERA = 0;

public final static int YEAR = 1;

public final static int MONTH = 2;

public final static int WEEK_OF_YEAR = 3;

...

@SuppressWarnings("ProtectedField")

protected int fields[];

public int get(int field)

{

complete();

return internalGet(field);

}

protected final int internalGet(int field)

{

return fields[field];

}

Calendar类的简单实用就是通过调用get方法从数组中获取相应的数据

5 Time

//Time

TextView time_show = findViewById(R.id.time_show);

Time time = new Time();

time.setToNow();

int time_year = time.year;

int time_month = time.month;

int time_day = time.monthDay;

int time_hour = time.hour;

int time_minute = time.minute;

int time_second = time.second;

String time_show_string = "Time:" + time_year + "-" + time_month + "-" + time_day + " " + time_hour + ":" + time_minute + ":" + time_second;

time_show.setText(time_show_string);

把这段代码打入到剪辑器,你会看到Time这个类是弃用了的

官方的注释解释是这样的

/**

* An alternative to the {@link java.util.Calendar} and

* {@link java.util.GregorianCalendar} classes. An instance of the Time class represents

* a moment in time, specified with second precision. It is modelled after

* struct tm. This class is not thread-safe and does not consider leap seconds.

*/

可以看到,描述上说,这是线程不安全的类,同时也没有处理leap seconds(闰秒)的能力,还举出了几个例子。

虽然是弃用的方法,但是还是可以看看怎么使用Time类的,简单地说,就是通过对象.变量的形式获取,也就是说,Time不像Calendar类那样使用数组存储数据,Time就是通过创建public int 数据 的形式来保存数据,也就是这些数据都是public的

总的来说,获取数据的时候,通过Time的形式,如int time_hour = time.hour;这样的写法,其实才是最舒服的(个人感觉),当然,最重要的还是安全问题

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

android获取一天的毫秒时间,Android关于获取时间的记录(小结)相关推荐

  1. 日历 android 周历,Android Studio 基础 之 获取系统Calendar 日历日程 (涉及指定日期时间判断是星期几的方法使用)的方法整理...

    Android Studio 基础 之 获取系统Calendar 日历日程 (涉及指定日期时间判断是星期几的方法使用)的方法整理 目录 Android Studio 基础 之 获取系统Calendar ...

  2. android 根据时间获取周几_android 调用系统 calendars 如何获取一个指定日期的所有日程 event。...

    android 调用系统 (CalendarContract)calendars 如何获取一个指定日期的所有日程(包含周期事件) ? 难点在于,很多event存的时候是有rrule的,也就是周期. 以 ...

  3. android 获取当前时间_js如何获取当前时间并显示

    js可以通过Date对象获取当前日期和时间,使用Date()获取系统当前时间,在使用getFullYear().getMonth().getDate() .getHours()等方法获取特定格式的时间 ...

  4. android获取指定文件夹的穿件时间,最近访问时间和最近修改时间

    目标 检测android手机常用应用时,可以检测应用在指定位置的文件夹属性,来做辅助判断设备的状态. 代码 import org.json.JSONException; import org.json ...

  5. android日期和时间,Android获取手机日期和时间

    1.直接调用Calendar类 private int mHour; private int mMinute; private int mYear; private int mMonth; priva ...

  6. 操作SD卡,获取文件目录及文件工具类Android,listView

    操作SD卡,获取文件目录及文件工具类Android 1.获取文件目录及文件工具类: DirectoryInfo  FileScan 1.1 DirectoryInfo: package com.gls ...

  7. android 用年月日初始化date_Android随笔之——Android时间、日期相关类和方法

    今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...

  8. Android平台Chromium net中的代理配置信息获取

    在计算机网络中,代理服务器 扮演着发起请求的客户端与服务器之间的中间人的角色.客户端连接到代理服务器,请求一些服务,比如文件,网页,或其它可以从服务器获得的资源,代理服务器以简化和控制复杂度的形式获取 ...

  9. android+geturl+方法,浅入浅出Android(014):HTTP GET获取文本内容

    1.建立android项目MyApplication 2.修改布局文件activity_my.xml xmlns:tools="http://schemas.android.com/tool ...

  10. android telephonymanager 电话状态,TelephonyManager类:Android手机及Sim卡状态的获取

    TelephonyManager这个类很有用,可以得到很多关于手机和Sim卡的信息. 直接上注释后的代码,请享用 package net.sunniwell.app; import android.a ...

最新文章

  1. 小程序无限层级路由方案
  2. Ajax异步调用Web服务的例子
  3. ec20 复位命令_《EC20 — AT指令》
  4. java应用高内存占用
  5. 奇葩面试经历分享:喊价25K,HR 却给了30K!
  6. Nginx基础知识之————日志管理
  7. 全虚拟化和半虚拟化的区别 cpu的ring0~ring3又是什么概念?
  8. hackbar 使用教程_hackbar简单安装使用教程
  9. 熊猫删除重复行– drop_duplicates()函数
  10. error CS1617: Invalid option 'latest' for /langversion; must be ISO-1, ISO-2, Default or an...
  11. php官网软件下载,php下载_php官方下载_3DM软件
  12. 【基于pyAudioKits的Python音频信号处理项目(一)】实现音频频谱分析仪并进行交互式滤波器设计
  13. 火星坐标系解密-iDesktopX空间数据处理
  14. PROE 齿轮设计视频教程+直齿 斜齿 人字齿 内外啮齿 行星齿
  15. 如何使用支付宝账号授权登录网站
  16. ubuntu为脚本添加桌面图标
  17. spring-webflux理解
  18. 激活windows10系统提示错误代码0x800705b4怎么解决
  19. 港交所新股发售竞争激烈:网易云音乐暗盘破发,凯莱英、顺丰同城等暂未获得足额申购
  20. aws上创建eks集群

热门文章

  1. Vijos CoVH之柯南开锁 (二分图)
  2. 刘强东放弃京东的股权套现离场
  3. OpenCV+VTK 读书笔记
  4. nginx rewrite if指令剖析
  5. 团队作业-Beta冲刺(2)
  6. 以太网PHY接口直连设计
  7. 如何在python中表示微分_Python实现自动微分(Automatic Differentiation)
  8. FireAlpaca(电脑绘画软件)官方中文版V2.5.9下载 | 电脑绘画软件哪个好用
  9. anyview+4.0java版_anyview4.0手机阅读器
  10. 华为笔记本电脑锐龙版和LINUX版的区别,华为 MateBook 13 锐龙版笔记本电脑新鲜评测...