optimized codes and fix negative month bug @ Lunar.getXiu()
This commit is contained in:
@@ -8,7 +8,7 @@ import com.nlf.calendar.util.SolarUtil;
|
||||
|
||||
/**
|
||||
* 阳历日期
|
||||
*
|
||||
*
|
||||
* @author 6tail
|
||||
*
|
||||
*/
|
||||
@@ -31,7 +31,7 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 通过年月日初始化
|
||||
*
|
||||
*
|
||||
* @param year 年
|
||||
* @param month 月,1到12
|
||||
* @param day 日,1到31
|
||||
@@ -46,7 +46,7 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 通过日期初始化
|
||||
*
|
||||
*
|
||||
* @param date 日期
|
||||
*/
|
||||
public Solar(Date date){
|
||||
@@ -56,10 +56,10 @@ public class Solar{
|
||||
month = calendar.get(Calendar.MONTH)+1;
|
||||
day = calendar.get(Calendar.DATE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过日历初始化
|
||||
*
|
||||
*
|
||||
* @param calendar 日历
|
||||
*/
|
||||
public Solar(Calendar calendar){
|
||||
@@ -103,7 +103,7 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 是否闰年
|
||||
*
|
||||
*
|
||||
* @return true/false 闰年/非闰年
|
||||
*/
|
||||
public boolean isLeapYear(){
|
||||
@@ -112,7 +112,7 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 获取星期,0代表周日,1代表周一
|
||||
*
|
||||
*
|
||||
* @return 0123456
|
||||
*/
|
||||
public int getWeek(){
|
||||
@@ -121,7 +121,7 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 获取星期的中文
|
||||
*
|
||||
*
|
||||
* @return 日一二三四五六
|
||||
*/
|
||||
public String getWeekInChinese(){
|
||||
@@ -130,51 +130,69 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 获取节日,有可能一天会有多个节日
|
||||
*
|
||||
*
|
||||
* @return 劳动节等
|
||||
*/
|
||||
public List<String> getFestivals(){
|
||||
List<String> l = new ArrayList<String>();
|
||||
//获取几月几日对应的节日
|
||||
String f = SolarUtil.FESTIVAL.get(month+"-"+day);
|
||||
if(null!=f) l.add(f);
|
||||
if(null!=f){
|
||||
l.add(f);
|
||||
}
|
||||
//计算几月第几个星期几对应的节日
|
||||
//第几周
|
||||
int weekInMonth = calendar.get(Calendar.WEEK_OF_MONTH);
|
||||
//星期几,0代表星期天
|
||||
int week = getWeek();
|
||||
//星期天很奇葩,会多算一周,需要减掉
|
||||
if(0==week) weekInMonth--;
|
||||
if(0==week){
|
||||
weekInMonth--;
|
||||
}
|
||||
f = SolarUtil.WEEK_FESTIVAL.get(month+"-"+weekInMonth+"-"+week);
|
||||
if(null!=f) l.add(f);
|
||||
if(null!=f){
|
||||
l.add(f);
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取星座
|
||||
*
|
||||
*
|
||||
* @return 星座
|
||||
*/
|
||||
public String getXingzuo(){
|
||||
int index = 11,m = month,d = day;
|
||||
int y = m*100+d;
|
||||
if(y>=321&&y<=419) index = 0;
|
||||
else if(y>=420&&y<=520) index = 1;
|
||||
else if(y>=521&&y<=620) index = 2;
|
||||
else if(y>=621&&y<=722) index = 3;
|
||||
else if(y>=723&&y<=822) index = 4;
|
||||
else if(y>=823&&y<=922) index = 5;
|
||||
else if(y>=923&&y<=1022) index = 6;
|
||||
else if(y>=1023&&y<=1121) index = 7;
|
||||
else if(y>=1122&&y<=1221) index = 8;
|
||||
else if(y>=1222||y<=119) index = 9;
|
||||
else if(y<=218) index = 10;
|
||||
if(y>=321&&y<=419){
|
||||
index = 0;
|
||||
}else if(y>=420&&y<=520){
|
||||
index = 1;
|
||||
}else if(y>=521&&y<=620){
|
||||
index = 2;
|
||||
}else if(y>=621&&y<=722){
|
||||
index = 3;
|
||||
}else if(y>=723&&y<=822){
|
||||
index = 4;
|
||||
}else if(y>=823&&y<=922){
|
||||
index = 5;
|
||||
}else if(y>=923&&y<=1022){
|
||||
index = 6;
|
||||
}else if(y>=1023&&y<=1121){
|
||||
index = 7;
|
||||
}else if(y>=1122&&y<=1221){
|
||||
index = 8;
|
||||
}else if(y>=1222||y<=119){
|
||||
index = 9;
|
||||
}else if(y<=218){
|
||||
index = 10;
|
||||
}
|
||||
return SolarUtil.XINGZUO[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取年份
|
||||
*
|
||||
*
|
||||
* @return 如2015
|
||||
*/
|
||||
public int getYear(){
|
||||
@@ -183,7 +201,7 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 获取月份
|
||||
*
|
||||
*
|
||||
* @return 1到12
|
||||
*/
|
||||
public int getMonth(){
|
||||
@@ -192,13 +210,13 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 获取日期
|
||||
*
|
||||
*
|
||||
* @return 1到31之间的数字
|
||||
*/
|
||||
public int getDay(){
|
||||
return day;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取农历
|
||||
* @return 农历
|
||||
@@ -209,17 +227,18 @@ public class Solar{
|
||||
|
||||
/**
|
||||
* 获取日历
|
||||
*
|
||||
*
|
||||
* @return 日历
|
||||
*/
|
||||
public Calendar getCalendar(){
|
||||
return calendar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return year+"-"+(month<10?"0":"")+month+"-"+(day<10?"0":"")+day;
|
||||
}
|
||||
|
||||
|
||||
public String toFullString(){
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(toString());
|
||||
@@ -253,4 +272,4 @@ public class Solar{
|
||||
return new Solar(c);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user