본문 바로가기

안드로이드

java 공휴일 체크

반응형

http://wintness.tistory.com/55  참고했습니다. 감사합니다. 


-------------------------------------------------------------------

public static boolean isHoliday(String yyyymmdd) throws Exception {

 // 검사년도

 int yyyy = Integer.parseInt(yyyymmdd.substring( 0, 4 ));

 boolean isYun = StaticManager.chkYun(yyyy);  //윤년체크

 

 try {

  // 음력 공휴일을 양력으로 바꾸어서 입력

  String tmp01 = StaticManager.lunarTranse( yyyy + "0101", isYun);// 음력설날

  String tmp02 = StaticManager.lunarTranse( yyyy + "0815", isYun);// 음력추석

  String tmp03 = StaticManager.lunarTranse( yyyy + "0408", isYun);// 석가탄신일


  String[] holidays = {

  yyyy+StaticManager.preDays(tmp01), tmp01,yyyy+StaticManager.afterDays(tmp01), //설날전날,설날,설날다음날

  yyyy+StaticManager.preDays(tmp02), tmp02,yyyy+StaticManager.afterDays(tmp02),  //추석전날,추석,추석다음날

  tmp03,

    yyyy + "0101",  // 양력설날

    yyyy + "0301",  // 삼일절

    yyyy + "0405",  // 식목일

    yyyy + "0505",  // 어린이날

    yyyy + "0606",  // 현충일

    yyyy + "0815",  // 광복절

    yyyy + "1003",  // 개천절

    yyyy + "1009",  // 한글날

    yyyy + "1225",  // 성탄절

   };

  for ( int ii = 0 ; ii < holidays.length ; ++ii ) {

   if ( yyyymmdd.equals(holidays[ii])  ) {

    return true ;

   }

  }

       } catch(Exception ex) {

           throw ex;

       }

 return false;

}



//사용법

if(isHoliday(yyyymmdd)){   

//빨간날 쉬는날임!!!

 }

-------------------------------------------------------------------


public class StaticManager {


public static boolean chkYun(int year) {  //윤년체크

boolean isYun = false;


if ((0 == (year % 4) && 0 != (year % 100)) || 0 == year % 400) {

isYun = true;

} else {

isYun = false;

}


return isYun;

}


public static String lunarTranse(String TranseDay, boolean leapyes)

throws Exception {     //음력변환

int lyear = Integer.parseInt(TranseDay.substring(0, 4));

int lmonth = Integer.parseInt(TranseDay.substring(4, 6));

int lday = Integer.parseInt(TranseDay.substring(6, 8));


if (!leapyes && !verifyDate(lyear, lmonth, lday, "solar-")) {

return "";

}

if (leapyes && !verifyDate(lyear, lmonth, lday, "solar+")) {

return "";

}

int m1 = -1;

long td = 0L;

if (lyear != 1881) {

m1 = lyear - 1882;

for (int i = 0; i <= m1; i++) {

for (int j = 0; j < 13; j++)

td = td + (long) KK[i * 13 + j];

if (KK[i * 13 + 12] == 0)

td = td + 336L;

else

td = td + 362L;

}

}

m1++;

int n2 = lmonth - 1;

int m2 = -1;

do {

m2++;

if (KK[m1 * 13 + m2] > 2) {

td = td + 26L + (long) KK[m1 * 13 + m2];

n2++;

continue;

}

if (m2 == n2)

break;

td = td + 28L + (long) KK[m1 * 13 + m2];

} while (true);

if (leapyes)

td = td + 28L + (long) KK[m1 * 13 + m2];

td = td + (long) lday + 29L;

m1 = 1880;

do {

m1++;

boolean leap = m1 % 400 == 0 || m1 % 100 != 0 && m1 % 4 == 0;

if (leap)

m2 = 366;

else

m2 = 365;

if (td < (long) m2)

break;

td = td - (long) m2;

} while (true);

int syear = m1;

MDayCnt[1] = m2 - 337;

m1 = 0;

do {

m1++;

if (td <= (long) MDayCnt[m1 - 1])

break;

td = td - (long) MDayCnt[m1 - 1];

} while (true);

int smonth = m1;

int sday = (int) td;

long y = (long) syear - 1L;

td = ((y * 365L + y / 4L) - y / 100L) + y / 400L;

boolean leap = syear % 400 == 0 || syear % 100 != 0 && syear % 4 == 0;

if (leap)

MDayCnt[1] = 29;

else

MDayCnt[1] = 28;

for (int i = 0; i < smonth - 1; i++)

td = td + (long) MDayCnt[i];

td = td + (long) sday;

int i = (int) (td % 10L);

i = (i + 4) % 10;

int j = (int) (td % 12L);

j = (j + 2) % 12;

String sValue = String.valueOf(syear);

if (smonth < 10)

sValue += "0";

sValue += String.valueOf(smonth);

if (sday < 10)

sValue += "0";

sValue += String.valueOf(sday);

return sValue;

}


private static boolean verifyDate(int k, int l, int l1, String s) {

if (k < 1881 || k > 2043 || l < 1 || l > 12)

return false;

if (s.equals("lunar") && l1 > MDayCnt[l - 1])

return false;

if (s.equals("solar+")) {

if (KK[(k - 1881) * 13 + 12] < 1)

return false;

if (KK[(k - 1881) * 13 + l] < 3)

return false;

if (KK[(k - 1881) * 13 + l] + 26 < l1)

return false;

}

if (s.equals("solar-")) {

int j = l - 1;

for (int i = 1; i <= 12; i++)

if (KK[((k - 1881) * 13 + i) - 1] > 2)

j++;

if (l1 > KK[(k - 1881) * 13 + j] + 28)

return false;

}

return true;

}


private static final int KK[] = { 1, 2, 1, 2, 1, 2, 2, 3, 2, 2, 1, 2, 1, 1,

2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 0, 1, 1, 2, 1, 1, 2, 1, 2, 2, 2,

1, 2, 0, 2, 1, 1, 2, 1, 3, 2, 1, 2, 2, 1, 2, 2, 2, 1, 1, 2, 1, 1,

2, 1, 2, 1, 2, 2, 0, 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1, 2, 0, 2, 2,

1, 2, 3, 2, 1, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 2,

1, 0, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 1, 2, 3, 2, 1, 2, 2,

1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 0, 1, 1, 2,

1, 1, 2, 3, 2, 2, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2,

0, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 0, 2, 1, 2, 1, 2, 3, 1, 2,

1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 1, 2, 1, 2, 1, 2, 0, 1, 2, 2, 1,

2, 1, 2, 1, 2, 1, 2, 1, 0, 2, 1, 2, 3, 2, 2, 1, 2, 1, 2, 1, 2, 1,

2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 0, 1, 2, 1, 1, 2, 1, 2, 2, 3,

2, 2, 1, 2,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, 1, 2, 2, 1, 2,

2, 1, 2, 0, 1, 2, 1, 1, 2, 1, 2, 3, 2, 2, 2, 1, 2, 1, 2, 1, 1, 2,

1, 2, 1, 2, 2, 2, 1, 0, 2, 1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 0, 2,

2, 1, 2, 1, 1, 4, 1, 1, 2, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 1, 1, 2,

1, 2, 0, 2, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1, 0, 2, 2, 1, 2, 2, 3,

2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 0, 2, 1,

1, 2, 1, 2, 2, 1, 2, 2, 1, 2, 0, 1, 2, 3, 1, 2, 1, 2, 1, 2, 2, 2,

1, 2, 1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 0 };


private static final int MDayCnt[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30,

31, 30, 31 };


public static String afterDays(String yyyymmdd){  //다음날 리턴

String date="";

Calendar cal = Calendar.getInstance();

cal.set(Integer.parseInt(yyyymmdd.substring(0,4)), Integer.parseInt(yyyymmdd.substring(4,6))-1, Integer.parseInt(yyyymmdd.substring(6,8)));

cal.add(Calendar.DATE, +1);

SimpleDateFormat formatter = new SimpleDateFormat ( "MMdd", Locale.KOREA );

date = formatter.format ( cal.getTime() );

return date;

}

public static String preDays(String yyyymmdd){  //이전날 리턴

String date="";

Calendar cal = Calendar.getInstance();

cal.set(Integer.parseInt(yyyymmdd.substring(0,4)), Integer.parseInt(yyyymmdd.substring(4,6))-1, Integer.parseInt(yyyymmdd.substring(6,8)));

cal.add(Calendar.DATE, -1);

SimpleDateFormat formatter = new SimpleDateFormat ( "MMdd", Locale.KOREA );

date = formatter.format ( cal.getTime() );

return date;

}



}


반응형