优化代码
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# lunar [](https://github.com/6tail/lunar/blob/master/LICENSE)
|
||||
|
||||
lunar是一款无第三方依赖的日历工具,支持公历和农历、星座、干支、生肖、节气、节日、彭祖百忌、吉神方位、冲煞、纳音、星宿、八字、五行、十神等。
|
||||
lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)工具,支持星座、干支、生肖、节气、节日、彭祖百忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道黑道日及吉凶等。
|
||||
|
||||
> 支持java 1.5及以上版本。
|
||||
|
||||
|
||||
@@ -24,10 +24,24 @@ public class Lunar{
|
||||
private Solar solar;
|
||||
/** 相对于基准日的偏移天数 */
|
||||
private int dayOffset;
|
||||
/** 日对应的天干下标,0-10 */
|
||||
/** 时对应的天干下标,0-9 */
|
||||
private int timeGanIndex;
|
||||
/** 时对应的地支下标,0-11 */
|
||||
private int timeZhiIndex;
|
||||
/** 日对应的天干下标,0-9 */
|
||||
private int dayGanIndex;
|
||||
/** 日对应的地支下标,0-12 */
|
||||
/** 日对应的地支下标,0-11 */
|
||||
private int dayZhiIndex;
|
||||
/** 月对应的天干下标,0-9 */
|
||||
private int monthGanIndex;
|
||||
/** 月对应的地支下标,0-11 */
|
||||
private int monthZhiIndex;
|
||||
/** 年对应的天干下标,0-9 */
|
||||
private int yearGanIndex;
|
||||
/** 年对应的地支下标,0-11 */
|
||||
private int yearZhiIndex;
|
||||
/** 周下标,0-6 */
|
||||
private int weekIndex;
|
||||
/** 阳历小时 */
|
||||
private int hour;
|
||||
/** 阳历分钟 */
|
||||
@@ -66,10 +80,7 @@ public class Lunar{
|
||||
this.day = lunarDay;
|
||||
this.hour = hour;
|
||||
this.minute = minute;
|
||||
this.dayOffset = LunarUtil.computeAddDays(year,month,day);
|
||||
int addDays = (dayOffset + LunarUtil.BASE_DAY_GANZHI_INDEX)%60;
|
||||
dayGanIndex = addDays%10;
|
||||
dayZhiIndex = addDays%12;
|
||||
compute();
|
||||
this.solar = toSolar();
|
||||
}
|
||||
|
||||
@@ -125,10 +136,30 @@ public class Lunar{
|
||||
day = lunarDay;
|
||||
hour = solar.getHour();
|
||||
minute = solar.getMinute();
|
||||
compute();
|
||||
}
|
||||
|
||||
private void compute(){
|
||||
yearGanIndex = (year-4)%10;
|
||||
yearZhiIndex = (year-4)%12;
|
||||
|
||||
int m = Math.abs(month);
|
||||
int leapMonth = LunarUtil.getLeapMonth(year);
|
||||
if(0==leapMonth||m<leapMonth||month==leapMonth){
|
||||
m-=1;
|
||||
}
|
||||
monthGanIndex = (m+(yearGanIndex%5+1)*2)%10;
|
||||
monthZhiIndex = (m+LunarUtil.BASE_MONTH_ZHI_INDEX)%12;
|
||||
|
||||
dayOffset = LunarUtil.computeAddDays(year,month,day);
|
||||
int addDays = (dayOffset + LunarUtil.BASE_DAY_GANZHI_INDEX)%60;
|
||||
dayGanIndex = addDays%10;
|
||||
dayZhiIndex = addDays%12;
|
||||
|
||||
timeZhiIndex = LunarUtil.getTimeZhiIndex((hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute);
|
||||
timeGanIndex = timeZhiIndex%10;
|
||||
|
||||
weekIndex = (dayOffset+LunarUtil.BASE_WEEK_INDEX)%7;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,7 +205,7 @@ public class Lunar{
|
||||
* @deprecated 使用getYearGan
|
||||
*/
|
||||
public String getGan(){
|
||||
return LunarUtil.GAN[(year-4)%10+1];
|
||||
return getYearGan();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,7 +214,7 @@ public class Lunar{
|
||||
* @return 天干,如辛
|
||||
*/
|
||||
public String getYearGan(){
|
||||
return LunarUtil.GAN[(year-4)%10+1];
|
||||
return LunarUtil.GAN[yearGanIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,7 +224,7 @@ public class Lunar{
|
||||
* @deprecated 使用getYearZhi
|
||||
*/
|
||||
public String getZhi(){
|
||||
return LunarUtil.ZHI[(year-4)%12+1];
|
||||
return getYearZhi();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,7 +233,7 @@ public class Lunar{
|
||||
* @return 地支,如亥
|
||||
*/
|
||||
public String getYearZhi(){
|
||||
return LunarUtil.ZHI[(year-4)%12+1];
|
||||
return LunarUtil.ZHI[yearZhiIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,7 +251,7 @@ public class Lunar{
|
||||
* @deprecated 使用getYearShengXiao
|
||||
*/
|
||||
public String getShengxiao(){
|
||||
return LunarUtil.SHENGXIAO[(year-4)%12+1];
|
||||
return getYearShengXiao();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,7 +260,7 @@ public class Lunar{
|
||||
* @return 年生肖,如虎
|
||||
*/
|
||||
public String getYearShengXiao(){
|
||||
return LunarUtil.SHENGXIAO[(year-4)%12+1];
|
||||
return LunarUtil.SHENGXIAO[yearZhiIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -238,13 +269,7 @@ public class Lunar{
|
||||
* @return 月生肖,如虎
|
||||
*/
|
||||
public String getMonthShengXiao(){
|
||||
String zhi = getMonthZhi();
|
||||
for(int i=0,j=LunarUtil.ZHI.length;i<j;i++){
|
||||
if(LunarUtil.ZHI[i].equals(zhi)){
|
||||
return LunarUtil.SHENGXIAO[i];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return LunarUtil.SHENGXIAO[monthZhiIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,13 +278,7 @@ public class Lunar{
|
||||
* @return 日生肖,如虎
|
||||
*/
|
||||
public String getDayShengXiao(){
|
||||
String zhi = getDayZhi();
|
||||
for(int i=0,j=LunarUtil.ZHI.length;i<j;i++){
|
||||
if(LunarUtil.ZHI[i].equals(zhi)){
|
||||
return LunarUtil.SHENGXIAO[i];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return LunarUtil.SHENGXIAO[dayZhiIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,13 +287,7 @@ public class Lunar{
|
||||
* @return 时辰生肖,如虎
|
||||
*/
|
||||
public String getTimeShengXiao(){
|
||||
String zhi = getTimeZhi();
|
||||
for(int i=0,j=LunarUtil.ZHI.length;i<j;i++){
|
||||
if(LunarUtil.ZHI[i].equals(zhi)){
|
||||
return LunarUtil.SHENGXIAO[i];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return LunarUtil.SHENGXIAO[timeZhiIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -294,59 +307,10 @@ public class Lunar{
|
||||
/**
|
||||
* 获取中文的月
|
||||
*
|
||||
* @return 中文月,如正月
|
||||
* @return 中文月,如正
|
||||
*/
|
||||
public String getMonthInChinese(){
|
||||
if(month>0){
|
||||
return LunarUtil.MONTH[month];
|
||||
}else{
|
||||
return "闰"+LunarUtil.MONTH[-month];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时辰(地支)
|
||||
* @return 时辰(地支)
|
||||
*/
|
||||
public String getTimeZhi(){
|
||||
String time = (hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute;
|
||||
return LunarUtil.convertTime(time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时辰(天干)
|
||||
* @return 时辰(天干)
|
||||
*/
|
||||
public String getTimeGan(){
|
||||
String zhi = getTimeZhi();
|
||||
for(int i=1,j=LunarUtil.ZHI.length;i<j;i++){
|
||||
if(LunarUtil.ZHI[i].equals(zhi)){
|
||||
return LunarUtil.GAN[1+(i-1)%10];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时辰干支(时柱)
|
||||
* @return 时辰干支(时柱)
|
||||
*/
|
||||
public String getTimeInGanZhi(){
|
||||
String zhi = getTimeZhi();
|
||||
for(int i=1,j=LunarUtil.ZHI.length;i<j;i++){
|
||||
if(LunarUtil.ZHI[i].equals(zhi)){
|
||||
return LunarUtil.GAN[1+(i-1)%10]+zhi;
|
||||
}
|
||||
}
|
||||
return zhi;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取季节
|
||||
* @return 农历季节
|
||||
*/
|
||||
public String getSeason(){
|
||||
return LunarUtil.SEASON[Math.abs(month)];
|
||||
return (month<0?"闰":"")+LunarUtil.MONTH[Math.abs(month)];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -358,6 +322,38 @@ public class Lunar{
|
||||
return LunarUtil.DAY[day];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时辰(地支)
|
||||
* @return 时辰(地支)
|
||||
*/
|
||||
public String getTimeZhi(){
|
||||
return LunarUtil.ZHI[timeZhiIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时辰(天干)
|
||||
* @return 时辰(天干)
|
||||
*/
|
||||
public String getTimeGan(){
|
||||
return LunarUtil.GAN[timeGanIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时辰干支(时柱)
|
||||
* @return 时辰干支(时柱)
|
||||
*/
|
||||
public String getTimeInGanZhi(){
|
||||
return getTimeGan()+getTimeZhi();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取季节
|
||||
* @return 农历季节
|
||||
*/
|
||||
public String getSeason(){
|
||||
return LunarUtil.SEASON[Math.abs(month)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节
|
||||
*
|
||||
@@ -423,7 +419,7 @@ public class Lunar{
|
||||
* @return 0123456
|
||||
*/
|
||||
public int getWeek(){
|
||||
return (dayOffset+LunarUtil.BASE_WEEK_INDEX)%7;
|
||||
return weekIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -530,10 +526,8 @@ public class Lunar{
|
||||
*/
|
||||
private Solar toSolar(){
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.set(SolarUtil.BASE_YEAR,SolarUtil.BASE_MONTH-1,SolarUtil.BASE_DAY);
|
||||
c.set(SolarUtil.BASE_YEAR,SolarUtil.BASE_MONTH-1,SolarUtil.BASE_DAY,hour,minute);
|
||||
c.add(Calendar.DATE,dayOffset);
|
||||
c.set(Calendar.HOUR_OF_DAY,hour);
|
||||
c.set(Calendar.MINUTE,minute);
|
||||
return new Solar(c);
|
||||
}
|
||||
|
||||
@@ -553,14 +547,7 @@ public class Lunar{
|
||||
* @return 月天干,如己
|
||||
*/
|
||||
public String getMonthGan(){
|
||||
int m = Math.abs(month);
|
||||
int leapMonth = LunarUtil.getLeapMonth(year);
|
||||
if(0==leapMonth||m<leapMonth||month==leapMonth){
|
||||
m-=1;
|
||||
}
|
||||
int yearGanIndex = (year-4)%10;
|
||||
int offset = (yearGanIndex%5+1)*2;
|
||||
return LunarUtil.GAN[(m+offset)%10+1];
|
||||
return LunarUtil.GAN[monthGanIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -568,12 +555,7 @@ public class Lunar{
|
||||
* @return 月地支,如卯
|
||||
*/
|
||||
public String getMonthZhi(){
|
||||
int m = Math.abs(month);
|
||||
int leapMonth = LunarUtil.getLeapMonth(year);
|
||||
if(0==leapMonth||m<leapMonth||month==leapMonth){
|
||||
m-=1;
|
||||
}
|
||||
return LunarUtil.ZHI[(m+LunarUtil.BASE_MONTH_ZHI_INDEX)%12+1];
|
||||
return LunarUtil.ZHI[monthZhiIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -704,7 +686,7 @@ public class Lunar{
|
||||
* @return 冲,如申
|
||||
*/
|
||||
public String getChong(){
|
||||
return LunarUtil.CHONG.get(getDayZhi());
|
||||
return LunarUtil.CHONG[dayZhiIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -712,7 +694,7 @@ public class Lunar{
|
||||
* @return 无情之克的冲天干,如甲
|
||||
*/
|
||||
public String getChongGan(){
|
||||
return LunarUtil.CHONG_GAN.get(getDayGan());
|
||||
return LunarUtil.CHONG_GAN[dayGanIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -720,7 +702,7 @@ public class Lunar{
|
||||
* @return 有情之克的冲天干,如甲
|
||||
*/
|
||||
public String getChongGanTie(){
|
||||
return LunarUtil.CHONG_GAN_TIE.get(getDayGan());
|
||||
return LunarUtil.CHONG_GAN_TIE[dayGanIndex+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -791,26 +773,7 @@ public class Lunar{
|
||||
*/
|
||||
public List<String> getBaZi(){
|
||||
List<String> l = new ArrayList<String>(4);
|
||||
String dayGan = getDayGan();
|
||||
int dayGanIndex = 1;
|
||||
for(int i=0,j=LunarUtil.GAN.length;i<j;i++){
|
||||
if(LunarUtil.GAN[i].equals(dayGan)){
|
||||
dayGanIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
dayGanIndex--;
|
||||
dayGanIndex%=5;
|
||||
String timeZhi = getTimeZhi();
|
||||
int timeZhiIndex = 1;
|
||||
for(int i=0,j=LunarUtil.ZHI.length;i<j;i++){
|
||||
if(LunarUtil.ZHI[i].equals(timeZhi)){
|
||||
timeZhiIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
timeZhiIndex--;
|
||||
String timeGan = LunarUtil.GAN[(dayGanIndex*12+timeZhiIndex)%10+1];
|
||||
String timeGan = LunarUtil.GAN[(dayGanIndex%5*12+timeZhiIndex)%10+1];
|
||||
l.add(getYearInGanZhi());
|
||||
l.add(getMonthInGanZhi());
|
||||
l.add(getDayInGanZhi());
|
||||
@@ -884,27 +847,11 @@ public class Lunar{
|
||||
* @return 执星
|
||||
*/
|
||||
public String getZhiXing(){
|
||||
String monthZhi = getMonthZhi();
|
||||
String dayZhi = getDayZhi();
|
||||
int indexMonthZhi = 0;
|
||||
int indexDayZhi = 0;
|
||||
for(int i=0,j=LunarUtil.ZHI.length;i<j;i++){
|
||||
String zhi = LunarUtil.ZHI[i];
|
||||
if(zhi.equals(monthZhi)){
|
||||
indexMonthZhi = i;
|
||||
}
|
||||
if(zhi.equals(dayZhi)){
|
||||
indexDayZhi = i;
|
||||
}
|
||||
if(indexMonthZhi>0&&indexDayZhi>0){
|
||||
break;
|
||||
}
|
||||
int offset = dayZhiIndex-monthZhiIndex;
|
||||
if(offset<0){
|
||||
offset += 12;
|
||||
}
|
||||
int add = indexDayZhi-indexMonthZhi;
|
||||
if(add<0){
|
||||
add = 12+add;
|
||||
}
|
||||
return LunarUtil.ZHI_XING[1+add];
|
||||
return LunarUtil.ZHI_XING[offset+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -913,16 +860,8 @@ public class Lunar{
|
||||
*/
|
||||
public String getDayTianShen(){
|
||||
String monthZhi = getMonthZhi();
|
||||
String dayZhi = getDayZhi();
|
||||
int offset = LunarUtil.MONTH_ZHI_TIAN_SHEN_OFFSET.get(monthZhi);
|
||||
int dayIndex = 0;
|
||||
for(int i=0,j=LunarUtil.ZHI.length;i<j;i++){
|
||||
if(LunarUtil.ZHI[i].equals(dayZhi)){
|
||||
dayIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return LunarUtil.TIAN_SHEN[1+(dayIndex-1+offset)%12];
|
||||
return LunarUtil.TIAN_SHEN[(dayZhiIndex+offset)%12+1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -946,11 +885,11 @@ public class Lunar{
|
||||
* @return 逐日胎神方位
|
||||
*/
|
||||
public String getDayPositionTai(){
|
||||
int ten = dayGanIndex-dayZhiIndex;
|
||||
if(ten<0){
|
||||
ten = ten+12;
|
||||
int offset = dayGanIndex-dayZhiIndex;
|
||||
if(offset<0){
|
||||
offset += 12;
|
||||
}
|
||||
return LunarUtil.POSITION_TAI_DAY[ten*5+dayGanIndex];
|
||||
return LunarUtil.POSITION_TAI_DAY[offset*5+dayGanIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1023,33 +962,27 @@ public class Lunar{
|
||||
s.append(getPengZuGan());
|
||||
s.append(" ");
|
||||
s.append(getPengZuZhi());
|
||||
s.append("]");
|
||||
s.append(" 喜神方位[");
|
||||
s.append("] 喜神方位[");
|
||||
s.append(getPositionXi());
|
||||
s.append("](");
|
||||
s.append(getPositionXiDesc());
|
||||
s.append(")");
|
||||
s.append(" 阳贵神方位[");
|
||||
s.append(") 阳贵神方位[");
|
||||
s.append(getPositionYangGui());
|
||||
s.append("](");
|
||||
s.append(getPositionYangGuiDesc());
|
||||
s.append(")");
|
||||
s.append(" 阴贵神方位[");
|
||||
s.append(") 阴贵神方位[");
|
||||
s.append(getPositionYinGui());
|
||||
s.append("](");
|
||||
s.append(getPositionYinGuiDesc());
|
||||
s.append(")");
|
||||
s.append(" 福神方位[");
|
||||
s.append(") 福神方位[");
|
||||
s.append(getPositionFu());
|
||||
s.append("](");
|
||||
s.append(getPositionFuDesc());
|
||||
s.append(")");
|
||||
s.append(" 财神方位[");
|
||||
s.append(") 财神方位[");
|
||||
s.append(getPositionCai());
|
||||
s.append("](");
|
||||
s.append(getPositionCaiDesc());
|
||||
s.append(")");
|
||||
s.append(" 冲[");
|
||||
s.append(") 冲[");
|
||||
s.append(getChongDesc());
|
||||
s.append("] 煞[");
|
||||
s.append(getSha());
|
||||
|
||||
@@ -402,39 +402,9 @@ public class LunarUtil{
|
||||
}
|
||||
};
|
||||
/** 地支相冲(子午相冲,丑未相冲,寅申相冲,辰戌相冲,卯酉相冲,巳亥相冲),由于地支对应十二生肖,也就对应了生肖相冲 */
|
||||
public static final Map<String,String> CHONG = new HashMap<String,String>(){
|
||||
private static final long serialVersionUID = -1;
|
||||
{
|
||||
put("子","午");
|
||||
put("丑","未");
|
||||
put("寅","申");
|
||||
put("卯","酉");
|
||||
put("辰","戌");
|
||||
put("巳","亥");
|
||||
put("午","子");
|
||||
put("未","丑");
|
||||
put("申","寅");
|
||||
put("酉","卯");
|
||||
put("戌","辰");
|
||||
put("亥","巳");
|
||||
}
|
||||
};
|
||||
public static final String[] CHONG = {"","午","未","申","酉","戌","亥","子","丑","寅","卯","辰","巳"};
|
||||
/** 天干相冲之无情之克(阳克阳,阴克阴) */
|
||||
public static final Map<String,String> CHONG_GAN = new HashMap<String,String>(){
|
||||
private static final long serialVersionUID = -1;
|
||||
{
|
||||
put("甲","戊");
|
||||
put("乙","己");
|
||||
put("丙","庚");
|
||||
put("丁","辛");
|
||||
put("戊","壬");
|
||||
put("己","癸");
|
||||
put("庚","甲");
|
||||
put("辛","乙");
|
||||
put("壬","丙");
|
||||
put("癸","丁");
|
||||
}
|
||||
};
|
||||
public static final String[] CHONG_GAN = {"","戊","己","庚","辛","壬","癸","甲","乙","丙","丁"};
|
||||
/** 天干四冲(无情之克中克得最严重的4个) */
|
||||
public static final Map<String,String> CHONG_GAN_BAD = new HashMap<String,String>(){
|
||||
private static final long serialVersionUID = -1;
|
||||
@@ -446,21 +416,7 @@ public class LunarUtil{
|
||||
}
|
||||
};
|
||||
/** 天干相冲之有情之克(阳克阴,阴克阳) */
|
||||
public static final Map<String,String> CHONG_GAN_TIE = new HashMap<String,String>(){
|
||||
private static final long serialVersionUID = -1;
|
||||
{
|
||||
put("甲","己");
|
||||
put("乙","戊");
|
||||
put("丙","辛");
|
||||
put("丁","庚");
|
||||
put("戊","癸");
|
||||
put("己","壬");
|
||||
put("庚","乙");
|
||||
put("辛","甲");
|
||||
put("壬","丁");
|
||||
put("癸","丙");
|
||||
}
|
||||
};
|
||||
public static final String[] CHONG_GAN_TIE = {"","己","戊","辛","庚","癸","壬","乙","甲","丁","丙"};
|
||||
/** 天干五合(有情之克中最有情的5个) */
|
||||
public static final Map<String,String> CHONG_GAN_TIE_GOOD = new HashMap<String,String>(){
|
||||
private static final long serialVersionUID = -1;
|
||||
@@ -1223,24 +1179,33 @@ public class LunarUtil{
|
||||
}
|
||||
|
||||
/**
|
||||
* 将HH:mm时刻转换为时辰(地支),非法的时刻返回子,null返回null
|
||||
* 获取HH:mm时刻的地支序号,非法的时刻返回0
|
||||
* @param hm HH:mm时刻
|
||||
* @return 时辰(地支),如子
|
||||
* @return 地支序号,0到11
|
||||
*/
|
||||
public static String convertTime(String hm){
|
||||
public static int getTimeZhiIndex(String hm){
|
||||
if(null==hm){
|
||||
return null;
|
||||
return 0;
|
||||
}
|
||||
if(hm.length()>5){
|
||||
hm = hm.substring(0,5);
|
||||
}
|
||||
int x = 2;
|
||||
int x = 1;
|
||||
for(int i=1;i<22;i+=2){
|
||||
if(hm.compareTo((i<10?"0":"")+i+":00")>=0&&hm.compareTo((i+1<10?"0":"")+(i+1)+":59")<=0){
|
||||
return ZHI[x];
|
||||
return x;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
return ZHI[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将HH:mm时刻转换为时辰(地支),非法的时刻返回子
|
||||
* @param hm HH:mm时刻
|
||||
* @return 时辰(地支),如子
|
||||
*/
|
||||
public static String convertTime(String hm){
|
||||
return ZHI[getTimeZhiIndex(hm)+1];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ public class TimeTest {
|
||||
put("22:59","亥");
|
||||
put("21:30","亥");
|
||||
|
||||
put(null,null);
|
||||
put(null,"子");
|
||||
|
||||
put("","子");
|
||||
put("23:01:01","子");
|
||||
|
||||
Reference in New Issue
Block a user