TShopping

 找回密碼
 註冊
搜索
查看: 1608|回復: 0

[教學] Java Android 日期時間應用筆記

[複製鏈接]
發表於 2018-1-16 18:21:18 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
日期時間主要運用到的三個類別
01.SimpleDateFormat

定義日期時間格式

將字串轉為Date型  parse(字串)回傳Date

將Date依照設定格式轉為字串 format(Date)回傳字串

02.Date(java.util.Date)

日期時間類別

Date dt=new Date() 取得當下時間

Date dt=new Date(1356689117695) 可傳入unixTime取得

dt.getTime() 取得unixTime毫秒 Long型別

03.Calendar

日曆類別

Calendar calendar = Calendar.getInstance() 取得當下時間

calendar(Date) 傳入date,指定時間

calendar.getTime() 取得Date形別

常見用法

01.取得現在日期時間字串

  1. <font size="3">//先行定義時間格式</font>

  2. <font size="3">SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");</font>

  3. <font size="3">//取得現在時間</font>

  4. <font size="3">Date dt=new Date();</font>

  5. <font size="3">//透過SimpleDateFormat的format方法將Date轉為字串</font>

  6. <font size="3">String dts=sdf.format(dt);</font>
複製代碼


02.取得某時間字串的 星期,上中下午

  1. <font size="3">//定義好時間字串的格式</font>
  2. <font size="3">SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");</font>


  3. <font size="3">//將字串轉成Date型</font>
  4. <font size="3">Date dt =sdf.parse("2013/01/07 11:49:00");</font>

  5. <font size="3">//定義要取的內容</font>
  6. <font size="3">SimpleDateFormat sdf5 = new SimpleDateFormat("E");//星期</font>
  7. <font size="3">SimpleDateFormat sdf6 = new SimpleDateFormat("a");//時段</font>

  8. <font size="3">//取出</font>
  9. <font size="3">String day=sdf5.format(dt);//星期</font>
  10. <font size="3">String td=sdf6.format(dt);//時段(會依照各手機系統不同,有差異.有些只有上午下午,有些則是有凌晨,中午等更詳細的描述)</font>
複製代碼


03.讀取Unix時間格式,轉以字串顯示

  1. <font size="3">//讀取Unix時間</font>
  2. <font size="3">Date dt=new Date(1356689117695);//UnixTime毫秒</font>
  3. <font size="3">//定義時間格式</font>
  4. <font size="3">SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");</font>
  5. <font size="3">//轉換字串</font>
  6. <font size="3">String time=sdf.format(dt);</font>
複製代碼

04.讀取日期字串,透過calendar做日期時間加減的動作,再轉回日期字串

  1. <font size="3">//定義好時間字串的格式</font>
  2. <font size="3">SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");</font>

  3. <font size="3">//將字串轉成Date型</font>
  4. <font size="3">Date dt =sdf.parse("2013/01/07 11:49:00");</font>

  5. <font size="3">//新增一個Calendar,並且指定時間</font>
  6. <font size="3">Calendar calendar = Calendar.getInstance();</font>
  7. <font size="3">calendar.setTime(dt);</font>
  8. <font size="3">calendar.add(Calendar.MONTH, 2);//月份+2</font>
  9. <font size="3">calendar.add(Calendar.HOUR, -1);//小時-1</font>
  10. <font size="3">Date tdt=calendar.getTime();//取得加減過後的Date</font>

  11. <font size="3">//依照設定格式取得字串</font>
  12. <font size="3">String time=sdf.format(tdt);</font>
複製代碼


05.計算兩個時間差距

  1. <font size="3">//定義時間格式</font>

  2. <font size="3">SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");</font>

  3. <font size="3">//取的兩個時間</font>

  4. <font size="3">Date dt1 =sdf.parse("2012/12/31 11:49:00");</font>

  5. <font size="3">Date dt2 =sdf.parse("2013/01/10 11:49:00");</font>

  6. <font size="3">//取得兩個時間的Unix時間</font>

  7. <font size="3">Long ut1=dt1.getTime();</font>

  8. <font size="3">Long ut2=dt2.getTime();</font>

  9. <font size="3">//相減獲得兩個時間差距的毫秒</font>

  10. <font size="3">Long timeP=ut2-ut1;//毫秒差</font>

  11. <font size="3">Long sec=timeP/1000;//秒差</font>

  12. <font size="3">Long min=timeP/1000*60;//分差</font>

  13. <font size="3">Long hr=timeP/1000*60*60;//時差</font>

  14. <font size="3">Long day=timeP/1000*60*60*24;//日差</font>
複製代碼

06.取得星期

  1. <font size="3">Date date=new Date();</font>

  2. <font size="3">int day=date.getDay();</font>
複製代碼


0~6表示禮拜日到禮拜六

但是根據api描述

This method is deprecated.此方法已經過時

建議使用Calender


  1. <font size="3">SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");</font>

  2. <font size="3">Date dt =sdf.parse("2012/12/31 11:49:00");</font>
  3. <font size="3">Calendar calendar = Calendar.getInstance();//取得目前時間</font>
  4. <font size="3">calendar.setTime(dt);//或是設定指定時間</font>

  5. <font size="3">int day=calendar.get(Calendar.DAY_OF_WEEK);</font>
複製代碼

1~7代表禮拜日至禮拜六


文章出處

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-18 14:46 , Processed in 0.096424 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表