android 時間擷取

來源:互聯網
上載者:User

Android中擷取系統時間有多種方法,可分為Java中Calendar類擷取,java.util.date類實現,還有android中Time實現

現總結如下:

方法一;

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);    }

方法二;

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

方法三;

 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);    }

方法四;

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);    }

擷取星期日期:

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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.