/**
     * 2012.10.09 add
     * 현재 일을 기준으로 현재 week(월~토) 기간으로 세팅
     *    - 일요일 일 경우 명일의 week(월~토) 기간으로 세팅
     *
     * return [ 0 : 년도, 1 : 시작년월일, 2 : 종료년월일 ]
     */
    private String[] getSrchDate() {
       
        String []resultValue = new String[3];
       
        /**
         *  oCalendar.get(Calendar.DAY_OF_WEEK)
         *   1     2    3     4     5     6    7
         *  "일", "월", "화", "수", "목", "금", "토"
         */
        Calendar oCalendar = Calendar.getInstance( );

        Calendar oCalendar2 = Calendar.getInstance();                 
        
        String thisYear  = new java.text.SimpleDateFormat ("yyyy").format(new java.util.Date());
        String thisMonth = new java.text.SimpleDateFormat ("MM").format(new java.util.Date());
        String thisday   = new java.text.SimpleDateFormat ("dd").format(new java.util.Date());
        String srchDaychk = "";


        int year  = Integer.parseInt( thisYear );
        int month = Integer.parseInt( thisMonth );
        int day   = Integer.parseInt( thisday );

       

/*     test 용  -------

        year = 2012;
        month = 12;
        day = 2;
        oCalendar.set(year, month-1, day);  

*/


        int nowWeekDay  = oCalendar.get( Calendar.DAY_OF_WEEK );
        int lastDay   = oCalendar.getActualMaximum( Calendar.DAY_OF_MONTH );    //달의 마지막일
        int lastDay2 =  0;


        int []start_end_day = { day-(nowWeekDay-2), day+(7-nowWeekDay) };  //[0] this_srch_sdate, [1] this_srch_edate
       
        int srchMonth = 0;
        int srchYear  = 0;
        int j = 0;
        for( int srchDay : start_end_day ) {
           
            srchMonth = month;
            srchYear  = year;
            if( srchDay > lastDay ) {
               
                srchDay = srchDay - lastDay;
                srchMonth++;
               
                if( srchMonth > 12 ) {
                    srchMonth = 1;
                    srchYear++;
                }
            }
            
            srchDaychk = String.valueOf( srchDay );
            if( srchDay < 10 ) {
                if( srchDay < 0 ) {   //주중에 달이 변할경우

                     int in_year = year;
                     int in_month = month;
                     if(month == 2) {
                         in_month = 12;
                         in_year = year - 1;
                     }else if(month == 1){
                         in_month = 11;
                         in_year = year - 1;
                     }
                     
                     oCalendar2.set(in_year, in_month-2, 1);   
                     lastDay2 = oCalendar2.getActualMaximum(Calendar.DATE);    //이전달의 마지막 일자


                     srchDaychk = String.valueOf( lastDay2 - (-srchDay)  );
                     srchMonth = srchMonth - 1;
                 }else {
                     srchDaychk = "0" + srchDay;
                 }

            }
           
            if( (j++) == 0 ) {
                resultValue[0]  = String.valueOf( srchYear );                                 //년도
                resultValue[1] = srchYear + "-" + srchMonth + "-" + srchDaychk;    //시작년월일
            }else {
                resultValue[2] = srchYear + "-" + srchMonth + "-" + srchDaychk;    //종료년월일
            }
        }
       
        return resultValue;
    }

=============================================================

String resultValue[] = getSrchDate();
년도          : resultValue[0]
시작년월일 : resultValue[1]
종료년월일 : resultValue[2]


 

'Java' 카테고리의 다른 글

[Java] getBytes 테스트  (0) 2013.02.27
[Java] session 중복 처리 문제  (0) 2012.12.13
[Java] FileInputStream, FileOutputStream  (0) 2012.09.26
[Java] jdk, eclipse  (0) 2012.08.08
[Java] 함께하는 개발표준 만들어 가기  (0) 2012.04.06

+ Recent posts