摘要:使用時(shí)調(diào)用類(lèi)的方法,該方法的描述是可以看出,該方法返回的是類(lèi)型的結(jié)果,結(jié)果記錄的是至今經(jīng)過(guò)的毫秒數(shù)。關(guān)于類(lèi),可以很自由的定制表現(xiàn)形式,年月日時(shí)分秒,時(shí)間格式,。。。
序
初涉江湖,還望海涵!
寫(xiě)點(diǎn)東西,純粹是因?yàn)閭€(gè)人的記憶能力較弱,寫(xiě)些筆記罷了,若有錯(cuò)誤還望雅正!
代碼粘貼
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); }根據(jù)自己使用過(guò)的以及網(wǎng)上搜索得到的結(jié)果,整理記錄了以下方法
1 timestamp1 timestamp
2 date
3 SimpleDateFormat
4 Calendar
5 Time
//timestamp TextView timestamp = findViewById(R.id.timestamp_show); timestamp.setText("timestamp:" + System.currentTimeMillis());
timestamp,時(shí)間戳。
使用時(shí)調(diào)用System類(lèi)的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類(lèi)型的結(jié)果,結(jié)果記錄的是midnight, January 1, 1970 UTC至今經(jīng)過(guò)的毫秒數(shù)(milliseconds)。
System.currentTimeMillis()是一個(gè)native方法,是一個(gè)C/C++方法,由系統(tǒng)測(cè)量時(shí)間戳并返回測(cè)量結(jié)果,根據(jù)注釋描述,測(cè)量結(jié)果可能偏大,因?yàn)橛行┎僮飨到y(tǒng)測(cè)量時(shí)間是以十毫秒為單位的,類(lèi)Date中討論了關(guān)于系統(tǒng)時(shí)間和UTC時(shí)間產(chǎn)生差異的原因,可自行觀看!
Note:
UTC(coordinated universal time)是民用時(shí)間的標(biāo)準(zhǔn),眾所周知,地球圍繞太陽(yáng)公轉(zhuǎn)一周的時(shí)間定義為一年,地球自轉(zhuǎn)一周定義為一天。有科學(xué)報(bào)道說(shuō),地球漫長(zhǎng)的公轉(zhuǎn)中其實(shí)是在緩慢的接近太陽(yáng),不管是否屬實(shí),自轉(zhuǎn)和公轉(zhuǎn)會(huì)產(chǎn)生一些變化也是不可避免的,UTC就是正確測(cè)量時(shí)間的規(guī)則,當(dāng)測(cè)量到需要校正時(shí)間時(shí),會(huì)以毫秒為單位進(jìn)行調(diào)整,稱(chēng)之為閏秒(leap seconds),后面Time會(huì)提到!
System.currentTimeMillis()的返回結(jié)果是一個(gè)記錄從1970開(kāi)始的毫秒數(shù)的long型結(jié)果,最容易想到的是long是有范圍區(qū)間的,如果有一天記錄的毫秒數(shù)超出long的范圍怎么辦!所以我計(jì)算了以下,long的最大值為0x7fff,ffff,ffff,ffff,取整大約為922億億,一年算365天,不考慮閏年,一天246060*60毫秒一年取整大約18億毫秒,922億/18,大約為50億年,考慮到太陽(yáng)的壽命,貌似也有用盡的一天。。。。但是,那么長(zhǎng)的時(shí)間,鬼知道會(huì)發(fā)展成什么樣!
2 Date//date Date date = new Date(); TextView date_show = findViewById(R.id.date_show); date_show.setText("Date:" + date.toString());
通過(guò)實(shí)例化Date類(lèi)獲取date實(shí)例從而獲取時(shí)間,簡(jiǎn)單通過(guò)toString()打印結(jié)果
Date類(lèi)的注釋特別描述了
日歷記時(shí)中,一年定為365天,閏年多一天,這表明,時(shí)間并不總是一天246060*60毫秒,需要用閏年加一天來(lái)調(diào)整。在coordinated universal time (UTC)的時(shí)間定義中,是通過(guò)閏秒(leap second)來(lái)調(diào)整時(shí)間的,并且總是在6月30日或12月31日,具體表現(xiàn)為該類(lèi)對(duì)秒的限制在0 to 61,60和61發(fā)生在leap second時(shí)。
構(gòu)造函數(shù)
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 byCalendar.set(year + 1900, month, date, * hrs, min, sec)
orGregorianCalendar(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; }
無(wú)參大Date()直接把System.currentTimeMillis()的時(shí)間戳返回給fastTime,另一個(gè)就是設(shè)定好年月日時(shí)分秒來(lái)創(chuàng)建對(duì)象,其中的設(shè)定是年是1900+參數(shù)year并且也對(duì)月份超出范圍做出了處理,但是該構(gòu)造方法已是@Deprecated(棄用)了
Date類(lèi)中大部分的方法都已經(jīng)棄用,要特別是多帶帶獲取年或者月等信息的方法,基本上都已經(jīng)棄用,留下的有打印即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類(lèi)的核心是Text的formatting(格式化)和Time的parsing(解析),SimpleDateFormat()通過(guò)傳入一個(gè)字符串來(lái)格式化需要的表現(xiàn)形式,樣例中通過(guò)調(diào)用format()傳入Date無(wú)參對(duì)象,實(shí)際上是調(diào)用System.currentTimeMillis()獲取最基本的時(shí)間,SimpleDateFormat類(lèi)的作用是把傳入的Date類(lèi)時(shí)間定制化封裝,從而得到需要的結(jié)果。
Note:
關(guān)于SimpleDateFormat類(lèi),可以很自由的定制表現(xiàn)形式,年月日時(shí)分秒,時(shí)間格式,AD/BC。。。
定制化所用字母的含義:
G => AD/BC(年份為負(fù)數(shù)時(shí)),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+
定制化使用"字符串",在該字符串中使用"字符"表示在年月日等數(shù)據(jù)外的部分,如分隔符
SimpleDateFormat類(lèi)的時(shí)間格式定制包括年月日等數(shù)據(jù)的表現(xiàn)形式,連接符,日期格式的描述,如Time zone,AM/PM,AD/BC。。。
SimpleDateFormat類(lèi)中存在的問(wèn)題是線(xiàn)程同步
/** * 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是線(xiàn)程不同步的,要在多線(xiàn)程中使用則要在線(xiàn)程外同步.
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是一個(gè)抽象類(lèi)通過(guò)其內(nèi)定義的Calendar.getInstance()靜態(tài)方法實(shí)例化對(duì)象而該靜態(tài)方法最終是通過(guò)返回一個(gè)new GregorianCalendar(zone, aLocale)來(lái)實(shí)現(xiàn)初始化!
Calendar類(lèi)內(nèi)部定義了關(guān)于時(shí)間需要用到的索引并用一個(gè)int數(shù)組存儲(chǔ)相關(guān)數(shù)據(jù)
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類(lèi)的簡(jiǎn)單實(shí)用就是通過(guò)調(diào)用get方法從數(shù)組中獲取相應(yīng)的數(shù)據(jù)
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);
把這段代碼打入到剪輯器,你會(huì)看到Time這個(gè)類(lèi)是棄用了的
官方的注釋解釋是這樣的
/** * 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. */
可以看到,描述上說(shuō),這是線(xiàn)程不安全的類(lèi),同時(shí)也沒(méi)有處理leap seconds(閏秒)的能力,還舉出了幾個(gè)例子。
雖然是棄用的方法,但是還是可以看看怎么使用Time類(lèi)的,簡(jiǎn)單地說(shuō),就是通過(guò)對(duì)象.變量的形式獲取,也就是說(shuō),Time不像Calendar類(lèi)那樣使用數(shù)組存儲(chǔ)數(shù)據(jù),Time就是通過(guò)創(chuàng)建public int 數(shù)據(jù) 的形式來(lái)保存數(shù)據(jù),也就是這些數(shù)據(jù)都是public的
總的來(lái)說(shuō),獲取數(shù)據(jù)的時(shí)候,通過(guò)Time的形式,如int time_hour = time.hour;這樣的寫(xiě)法,其實(shí)才是最舒服的(個(gè)人感覺(jué)),當(dāng)然,最重要的還是安全問(wèn)題
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/77614.html
摘要:說(shuō)好的分類(lèi)集合來(lái)啦,采用標(biāo)簽云的方式來(lái)展示掘金閱讀提示點(diǎn)擊下文中返回到頂部有分類(lèi)不合理的地方請(qǐng)?zhí)帷7淳幾g這個(gè)后發(fā)現(xiàn)其使用個(gè)優(yōu)質(zhì)的開(kāi)源項(xiàng)目掘金是由的開(kāi)源的一個(gè)庫(kù),用于構(gòu)建可預(yù)期的和聲明式的用戶(hù)界面。 想不想通過(guò)一線(xiàn)互聯(lián)網(wǎng)公司面試? - Android - 掘金國(guó)內(nèi)一線(xiàn)互聯(lián)網(wǎng)公司內(nèi)部面試題庫(kù) 以下面試題來(lái)自于百度、小米、樂(lè)視、美團(tuán)、58、獵豹、360、新浪、搜狐內(nèi)部題庫(kù) 熟悉本文中列出的知...
閱讀 1221·2021-09-22 15:24
閱讀 2314·2019-08-30 15:44
閱讀 2693·2019-08-30 10:55
閱讀 3387·2019-08-29 13:25
閱讀 1673·2019-08-29 13:09
閱讀 1425·2019-08-26 14:05
閱讀 1423·2019-08-26 13:58
閱讀 2012·2019-08-26 11:57