`

calendar 类的使用

 
阅读更多

使用calendar类设定指定的时间:

 

下周,或者下个月

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2013);   //设定年份
//      calendar.set(Calendar.MONTH, 10);   //设定月份,显示的比当前多一个月
      calendar.set(Calendar.DAY_OF_MONTH, 20);  //设定日期
      calendar.set(Calendar.HOUR_OF_DAY, 17);   //设定 小时
      calendar.set(Calendar.MINUTE, 30);     //设定分钟
        
//      calendar.add(Calendar.DATE, 7);  //下周的
      calendar.add(Calendar.MONTH, 2);   //下个月
        
      Date date =  calendar.getTime();
        date.getTime();
        SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mDateFormat.format(date);
        Log.d("test",  mDateFormat.format(date));

 

   得到下周一的时间

public static long getNextWeekTime(int type, String hh, String mm){
		SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hh));
		calendar.set(Calendar.MINUTE,Integer.parseInt(mm));
		
		switch (type) {
		case 0:
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			break;
		case 1:
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			break;
		case 2:
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
			break;
		case 3:
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
			break;
		case 4:
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
			break;
		case 5:
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
			break;
		case 6:
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			break;
		}
		
		long currentTime = System.currentTimeMillis();
		long scanTime = calendar.getTime().getTime();
		if(currentTime > scanTime){ 			calendar.add(Calendar.DATE, 7);
		}
		
		Log.d("test"+ mDateFormat.format(calendar.getTime()));
		return calendar.getTime().getTime();
	}

 得到下周一的时间:

 

 得到当前时间:

private String getTime() {
		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();
		date.getTime();
		SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mDateFormat.format(date);
		return mDateFormat.format(date);
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics