Android 獲取時(shí)間實(shí)例代碼
更新時(shí)間:2017年05月09日 09:24:16 投稿:lqh
這篇文章主要介紹了Android 獲取時(shí)間實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
Android 獲取時(shí)間實(shí)例代碼
注意:
h:12小時(shí)制小時(shí)數(shù)
H:24小時(shí)制小時(shí)數(shù)
實(shí)例代碼:
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created by Administrator on 2017/5/8. */ public class GetTime { public static void main(String[] args) { Date date = new Date(); System.out.println(date);//Mon May 08 14:27:44 CST 2017 System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date));//2017-05-08 02:27:44 long millis = System.currentTimeMillis(); System.out.println(millis);//1494224864479 System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(millis));//2017-05-08 02:27:44 //yyyy-MM-dd E hh:mm:ss.sss //年-月-日 星期 時(shí):分:秒.毫秒 System.out.println(new SimpleDateFormat("yyyy-MM-dd E hh:mm:ss.sss").format(date));//2017-05-08 星期一 02:27:44.044 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss").format(date));//2017-05-08 14:27:44.044 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));//2017-05-08 14:27:44 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date));//2017-05-08 14:27 System.out.println(new SimpleDateFormat().format(date));//17-5-8 下午2:27 :默認(rèn) compareDataToNow("2017-05-03 12:45:00"); try { Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-05-03 12:45:00"); compareToNowDate(date1); } catch (ParseException e) { e.printStackTrace(); } getWeek(); getTime1(); getTime2(); } static 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); System.out.println("SimpleDateFormat時(shí)間===" + t1);//2017-05-08 12:44:10 SimpleDateFormat f4 = new SimpleDateFormat("今天是" + "yyyy年MM月dd日 E kk點(diǎn)mm分"); System.out.println("f4======" + f4.format(new Date()));//今天是2017年05月08日 星期一 14點(diǎn)15分 SimpleDateFormat f3 = new SimpleDateFormat("今天是" + "hh小時(shí)mm分鐘"); System.out.println("f3======" + f3.format(new Date()));//今天是02小時(shí)15分鐘 SimpleDateFormat f2 = new SimpleDateFormat("今天是" + "kk點(diǎn)mm分鐘"); System.out.println("f2======" + f2.format(new Date()));//今天是14點(diǎn)17分鐘 } static void getTime2() { Calendar calendar = Calendar.getInstance(); String created = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月"http://從0計(jì)算 + calendar.get(Calendar.DAY_OF_MONTH) + "日" + calendar.get(Calendar.HOUR_OF_DAY) + "時(shí)" + calendar.get(Calendar.MINUTE) + "分" + calendar.get(Calendar.SECOND) + "s"; System.out.println("Calendar時(shí)間====" + created);//時(shí)間:2017年5月8日12時(shí)33分24s } static void getWeek() { 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);//Today is:- Monday } //計(jì)算日期之間相隔幾天: static long compareDataToNow(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date passDate, nowDate; long diff = -100l, days = -100l; try { passDate = sdf.parse(date); String nowStr = sdf.format(new Date()); nowDate = sdf.parse(nowStr); diff = passDate.getTime() - nowDate.getTime();//long型的毫秒數(shù) days = diff / (1000 * 60 * 60 * 24); System.out.println("相隔:" + days + "天" + " nowDate.getTime()=====" + nowDate.getTime());//-5天 } catch (ParseException e) { e.printStackTrace(); } return diff; } //計(jì)算日期之間相隔幾天: static long compareToNowDate(Date date) { long diff = -100l, days = -100l; Date nowDate = new Date(); diff = date.getTime() - nowDate.getTime();//long型的毫秒數(shù) days = diff / (1000 * 60 * 60 * 24); System.out.println("相隔:" + days + "天" + " nowDate.getTime()=====" + nowDate.getTime());//-5天 return diff; } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
相關(guān)文章
Android實(shí)現(xiàn)自動變換大小的組件ViewPager2
這篇文章主要介紹了Android實(shí)現(xiàn)自動變換大小的組件ViewPager2,ViewPager2最顯著的特點(diǎn)是基于RecyclerView實(shí)現(xiàn),RecyclerView是目前Android端最成熟的AdapterView解決方案2023-03-03android Tween Animation屬性設(shè)置方法實(shí)例
在Android開發(fā)中,Animation是用來給控件制作效果的,本文介紹二種實(shí)現(xiàn)方法2013-11-11詳解android 視頻圖片混合輪播實(shí)現(xiàn)
這篇文章主要介紹了android 視頻圖片混合輪播實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Android點(diǎn)擊EditText文本框之外任何地方隱藏鍵盤的解決辦法
這篇文章主要介紹了Android點(diǎn)擊EditText文本框之外任何地方隱藏鍵盤的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android遠(yuǎn)程服務(wù)編寫和調(diào)用教程
這篇文章主要介紹了Android遠(yuǎn)程服務(wù)編寫和調(diào)用教程,本文教大家如何編寫或者調(diào)用Android的遠(yuǎn)程服務(wù),感興趣的小伙伴們可以參考一下2016-02-02Android 架構(gòu)之?dāng)?shù)據(jù)庫框架搭建
這篇文章主要給大家介紹的是Android 架構(gòu)之?dāng)?shù)據(jù)庫框架搭建,在本篇中,將會讓你一點(diǎn)一滴從無到有創(chuàng)建一個(gè)不再為數(shù)據(jù)庫而煩惱的框架。需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09