1. 程式人生 > >Android 如何獲取當前的時間與星期幾

Android 如何獲取當前的時間與星期幾

  1. public class DataString {
        private static String mYear;
        private static String mMonth;
        private static String mDay;
        private static String mWay;
        
    	public static String StringData(){
    		final Calendar c = Calendar.getInstance();
    		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    		mYear = String.valueOf(c.get(Calendar.YEAR)); // 獲取當前年份
    		mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 獲取當前月份
    		mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 獲取當前月份的日期號碼
    		mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
    		if("1".equals(mWay)){
    			mWay ="天";
    		}else if("2".equals(mWay)){
    			mWay ="一";
    		}else if("3".equals(mWay)){
    			mWay ="二";
    		}else if("4".equals(mWay)){
    			mWay ="三";
    		}else if("5".equals(mWay)){
    			mWay ="四";
    		}else if("6".equals(mWay)){
    			mWay ="五";
    		}else if("7".equals(mWay)){
    			mWay ="六";
    		}
    		return mYear + "年" + mMonth + "月" + mDay+"日"+"/星期"+mWay;
    	}
    	
    }

    第一種方法

  2. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
    //獲取當前時間
    Date date = new Date(System.currentTimeMillis());
    time1.setText("Date獲取當前日期時間"+simpleDateFormat.format(date));

    第二種方法

  3. Calendar calendar = Calendar.getInstance();
    //獲取系統的日期
    //年
    int year = calendar.get(Calendar.YEAR);
    //月
    int month = calendar.get(Calendar.MONTH)+1;
    //日
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    //獲取系統時間
    //小時
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    //分鐘
    int minute = calendar.get(Calendar.MINUTE);
    //秒
    int second = calendar.get(Calendar.SECOND);
    
    time2.setText("Calendar獲取當前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);

    第三種方法

  4. Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone資料。
    t.setToNow(); // 取得系統時間。
    int year = t.year;
    int month = t.month+1;
    int day = t.monthDay;
    int hour = t.hour; // 0-23
    int minute = t.minute;
    int second = t.second;
    time3.setText("Calendar獲取當前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);
    1

    收集整理網上的資料學習,這樣一點一點的就能會很多東西!