package com.link2me.android.util;
import com.ibm.icu.util.ChineseCalendar; import com.link2me.android.item.Holidays_Item;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap;
public class CalendarHelper { static HashMap<String, Holidays_Item> holidaysArrayList = new HashMap<String,Holidays_Item>();
/** * 음력날짜를 양력날짜로 변환 * @param 음력날짜 (yyyyMMdd) * @return 양력날짜 (yyyyMMdd) */ public static String Lunar2Solar(String yyyymmdd) { ChineseCalendar cc = new ChineseCalendar(); Calendar cal = Calendar.getInstance();
if (yyyymmdd == null) return "";
String date = yyyymmdd.trim(); if (date.length() != 8) { if (date.length() == 4) date = date + "0101"; else if (date.length() == 6) date = date + "01"; else if (date.length() > 8) date = date.substring(0, 8); else return ""; }
cc.set(ChineseCalendar.EXTENDED_YEAR, Integer.parseInt(date.substring(0, 4)) + 2637); cc.set(ChineseCalendar.MONTH, Integer.parseInt(date.substring(4, 6)) - 1); cc.set(ChineseCalendar.DAY_OF_MONTH, Integer.parseInt(date.substring(6)));
cal.setTimeInMillis(cc.getTimeInMillis());
int y = cal.get(Calendar.YEAR); int m = cal.get(Calendar.MONTH) + 1; int d = cal.get(Calendar.DAY_OF_MONTH);
StringBuffer ret = new StringBuffer(); ret.append(String.format("%04d", y)); ret.append(String.format("%02d", m)); ret.append(String.format("%02d", d));
return ret.toString(); }
/** * 양력날짜를 음력날짜로 변환 * @param 양력날짜 (yyyyMMdd) * @return 음력날짜 (yyyyMMdd) */ public static String Solar2Lunar(String yyyymmdd) { ChineseCalendar cc = new ChineseCalendar(); Calendar cal = Calendar.getInstance();
if (yyyymmdd == null) return "";
String date = yyyymmdd.trim() ; if( date.length() != 8 ) { if( date.length() == 4 ) date = date + "0101" ; else if( date.length() == 6 ) date = date + "01" ; else if( date.length() > 8 ) date = date.substring(0,8) ; else return "" ; }
cal.set( Calendar.YEAR, Integer.parseInt(date.substring(0,4)) ) ; cal.set( Calendar.MONTH, Integer.parseInt(date.substring(4,6))-1 ) ; cal.set( Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(6)) ) ;
cc.setTimeInMillis( cal.getTimeInMillis() ) ;
// ChinessCalendar.YEAR 는 1~60 까지의 값만 가지고 , // ChinessCalendar.EXTENDED_YEAR 는 Calendar.YEAR 값과 2637 만큼의 차이를 가진다. int y = cc.get(ChineseCalendar.EXTENDED_YEAR)-2637 ; int m = cc.get(ChineseCalendar.MONTH)+1 ; int d = cc.get(ChineseCalendar.DAY_OF_MONTH) ;
StringBuffer ret = new StringBuffer() ; if( y < 1000 ) ret.append( "0" ) ; else if( y < 100 ) ret.append( "00" ) ; else if( y < 10 ) ret.append( "000" ) ; ret.append( y ) ;
if( m < 10 ) ret.append( "0" ) ; ret.append( m ) ;
if( d < 10 ) ret.append( "0" ) ; ret.append( d ) ;
return ret.toString() ; }
public static HashMap<String,Holidays_Item> holidayArray(String yyyy){ holidaysArrayList.clear(); // 데이터 초기화 // 양력 휴일 addHolidaysItem(yyyy,"0101" ,"신정"); addHolidaysItem(yyyy,"0301" ,"삼일절"); addHolidaysItem(yyyy,"0505" ,"어린이날"); addHolidaysItem(yyyy,"0606" ,"현충일" ); addHolidaysItem(yyyy,"0815" ,"광복절"); addHolidaysItem(yyyy,"1003" ,"개천절"); addHolidaysItem(yyyy,"1009" ,"한글날"); addHolidaysItem(yyyy,"1225" ,"성탄절");
// 음력 휴일 String prev_seol = String.valueOf(Integer.parseInt(Lunar2Solar(yyyy+"0101")) -1); addHolidaysItem(yyyy,prev_seol.substring(4) ,""); addHolidaysItem(yyyy,SolarDays(yyyy, "0101"),"설날"); addHolidaysItem(yyyy,SolarDays(yyyy, "0102"),""); addHolidaysItem(yyyy,SolarDays(yyyy, "0408"),"석탄일"); addHolidaysItem(yyyy,SolarDays(yyyy, "0814"),""); addHolidaysItem(yyyy,SolarDays(yyyy, "0815"),"추석"); addHolidaysItem(yyyy,SolarDays(yyyy, "0816"),"");
try { // 어린이날 대체공휴일 검사 : 어린이날은 토요일, 일요일인 경우 그 다음 평일을 대체공유일로 지정 int childDayChk = WeekendValue(yyyy+"0505"); if(childDayChk == 1) addHolidaysItem(yyyy,"0506" ,"대체공휴일"); if(childDayChk == 7) addHolidaysItem(yyyy,"0507" ,"대체공휴일");
// 설날 대체공휴일 검사 if(WeekendValue(Lunar2Solar(yyyy+"0101"))==1) addHolidaysItem(yyyy,SolarDays(yyyy, "0103"),"대체공휴일"); if(WeekendValue(Lunar2Solar(yyyy+"0101"))==2) addHolidaysItem(yyyy,SolarDays(yyyy, "0103"),"대체공휴일"); if(WeekendValue(Lunar2Solar(yyyy+"0102"))==1) addHolidaysItem(yyyy,SolarDays(yyyy, "0103"),"대체공휴일");
// 추석 대체공휴일 검사 if(WeekendValue(Lunar2Solar(yyyy+"0814"))==1) addHolidaysItem(yyyy,SolarDays(yyyy, "0817"),"대체공휴일"); if(WeekendValue(Lunar2Solar(yyyy+"0815"))==1) addHolidaysItem(yyyy,SolarDays(yyyy, "0817"),"대체공휴일"); if(WeekendValue(Lunar2Solar(yyyy+"0816"))==1) addHolidaysItem(yyyy,SolarDays(yyyy, "0817"),"대체공휴일"); } catch (ParseException e) { e.printStackTrace(); }
//Collections.sort(holidaysArrayList); // 오름차순 정렬
return holidaysArrayList; }
private static String SolarDays(String yyyy, String date){ return Lunar2Solar(yyyy+date).substring(4); }
private static void addHolidaysItem(String year, String date, String name ){ Holidays_Item item = new Holidays_Item(); item.setYear(year); item.setDate(date); item.setName(name); holidaysArrayList.put(date,item); }
public static int WeekendValue(String date) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(date)); return cal.get(Calendar.DAY_OF_WEEK); // Calendar.SUNDAY : 1 // Calendar.SATURDAY : 7 }
/** * 양력날짜의 요일을 리턴 * @param 양력날짜 (yyyyMMdd) * @return 요일(int) */ public static int getDayOfWeek(String day) { int y = Integer.parseInt(day.substring(0, 4)); int m = Integer.parseInt(day.substring(4, 6)) - 1; int d = Integer.parseInt(day.substring(6)); Calendar c = Calendar.getInstance(); c.set(y, m, d); return c.get(Calendar.DAY_OF_WEEK); }
public static String Sol2Lun(String year, String month, String day) { ChineseCalendar cc = new ChineseCalendar(); Calendar cal = Calendar.getInstance();
if (year == null || month == null || day == null) return "";
cal.set( Calendar.YEAR, Integer.parseInt(year) ) ; cal.set( Calendar.MONTH, Integer.parseInt(month)) ; cal.set( Calendar.DAY_OF_MONTH, Integer.parseInt(day)) ;
cc.setTimeInMillis( cal.getTimeInMillis() ) ;
// ChinessCalendar.YEAR 는 1~60 까지의 값만 가지고 , // ChinessCalendar.EXTENDED_YEAR 는 Calendar.YEAR 값과 2637 만큼의 차이를 가진다. int y = cc.get(ChineseCalendar.EXTENDED_YEAR)-2637 ; int m = cc.get(ChineseCalendar.MONTH)+1 ; int d = cc.get(ChineseCalendar.DAY_OF_MONTH) ;
StringBuffer ret = new StringBuffer() ; if( y < 1000 ) ret.append( "0" ) ; else if( y < 100 ) ret.append( "00" ) ; else if( y < 10 ) ret.append( "000" ) ; ret.append( y ) ;
if( m < 10 ) ret.append( "0" ) ; ret.append( m ) ;
if( d < 10 ) ret.append( "0" ) ; ret.append( d ) ;
return ret.toString().substring(4,6)+"."+ret.toString().substring(6,8); }
// 해당 달의 첫 요일을 구해서 돌려줌. private static int getStartDay(int year, int month) { int monthSum = 0; int leapYear = 0; int daySum = 1;
for (int i = 1; i < year; i++) { monthSum += 365; if (isLeapYear(i) == true) { leapYear += 1; } }
for (int j = 1; j < month; j++) { daySum += getLastDay(year, j); }
return (monthSum + leapYear + daySum) % 7;
}
// 달의 마지막 일을 구함 public static int getLastDay(int month, int year) {
if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } else if (month == 2 && isLeapYear(year) == true) { return 29; } else if (month == 2 && isLeapYear(year) == false) { return 28; } else { return 31; } }
// 해당 년도가 윤년인지 판별 private static boolean isLeapYear(int year) { return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } } |