안드로이드 개발: 금주, 이달 등의 시작일 조회

2960 단어
사용법:
SearchDate date = new SearchDate(index);//index 값은 0에서 4로 다음 코드String startTime = date를 의미합니다.getStartTime();String endTime = date.getEndTime();
 
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class SearchDate {
	
	private String startTime;
	private String endTime;
	
	public SearchDate(int index){
		int y, m, d;

		Calendar c = Calendar.getInstance();
		y = c.get(Calendar.YEAR);
		m = c.get(Calendar.MONTH) + 1;
		d = c.get(Calendar.DAY_OF_MONTH);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d");

		startTime ="";
		endTime = "";
		
		
		switch(index){
		case 0://  
			
			c = Calendar.getInstance();
			startTime = format.format(c.getTime());
			endTime = format.format(c.getTime());
			
			break;
		case 1://  
			
			c = Calendar.getInstance();
            c.setFirstDayOfWeek(Calendar.SUNDAY);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            startTime = format.format(c.getTime());
            
            c = Calendar.getInstance();
            c.setFirstDayOfWeek(Calendar.SUNDAY);
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            endTime = format.format(c.getTime());
            
			break;
		case 2://  
			
			c = Calendar.getInstance();
			c.add(Calendar.MONTH, 0);
			c.set(Calendar.DAY_OF_MONTH, 1);//    1 ,           
			startTime = format.format(c.getTime());

			c = Calendar.getInstance();
			c.set(Calendar.DAY_OF_MONTH,
					c.getActualMaximum(Calendar.DAY_OF_MONTH));
			endTime = format.format(c.getTime());
			break;
		case 3://   
			
			if(m<=3){
				startTime = y + "-1-1";
				endTime = y + "-3-31";
			}else if(m<=6){
				startTime = y + "-4-1";
				endTime = y + "-6-30";
			}else if(m<=9){
				startTime = y + "-7-1";
				endTime = y + "-9-30";
			}else {
				startTime = y + "-10-1";
				endTime = y + "-12-31";
			}
			break;
		case 4://  
			
			c = Calendar.getInstance();
			startTime = y + "-1-1";
			endTime = y + "-12-31";
			break;	
		}
	}

	public String getStartTime() {
		return startTime;
	}

	public String getEndTime() {
		return endTime;
	}
}

  

좋은 웹페이지 즐겨찾기