Skip to content

Commit

Permalink
add methods
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Jan 27, 2019
1 parent 4972b5a commit 4ce3231
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 106 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
## 4.4.3

### 新特性
* 【crypto】 MD5以及Digester增加加盐支持(issue#256@Github)
* 【crypto】 整理KeyUtil,减少冗余代码
* 【core】 增加Zodiac类,DateUtil增加getZodiac、getChineseZodiac用于获取星座和生肖(issue#260@Github)

### Bug修复
* 【core】 修复ExceptionUtil.stacktraceToString中limit参数无效问题(issue#IR7UE@Gitee)
* 【core】 修复StrUtil.repeatByLength中数组越界问题(issue#IRB2C@Gitee)
* 【core】 修复FileUtil.remove移动后删除失败问题(issue#IRF8R@Gitee)
* 【extra】 修复Ftp中delDir逻辑导致的问题(issue#IRCQ8@Gitee)
* 【core】 修复XmlUtil.mapToXml中map值为空导致的空指针问题。(issue#IRD7X@Gitee)

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

Expand Down
23 changes: 23 additions & 0 deletions hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,29 @@ public static DateRange range(Date start, Date end, final DateField unit) {
public static List<DateTime> rangeToList(Date start, Date end, final DateField unit) {
return CollUtil.newArrayList((Iterable<DateTime>) range(start, end, unit));
}

/**
* 通过生日计算星座
*
* @param month 月,从0开始计数
* @param day 天
* @return 星座名
* @since 4.4.3
*/
public static String getZodiac(int month, int day) {
return Zodiac.getZodiac(month, day);
}

/**
* 计算生肖,只计算1900年后出生的人
*
* @param year 农历年
* @return 生肖名
* @since 4.4.3
*/
public static String getChineseZodiac(int year) {
return Zodiac.getChineseZodiac(year);
}

// ------------------------------------------------------------------------ Private method start
/**
Expand Down
91 changes: 91 additions & 0 deletions hutool-core/src/main/java/cn/hutool/core/date/Zodiac.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cn.hutool.core.date;

import java.util.Calendar;
import java.util.Date;

/**
* 星座 来自:https://blog.csdn.net/u010758605/article/details/48317881
*
* @author looly
* @since 4.4.3
*/
public class Zodiac {

/** 星座分隔时间日 */
private static final int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22 };
/** 星座 */
private static final String[] ZODIACS = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座" };
private static final String[] CHINESE_ZODIACS = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

/**
* 通过生日计算星座
*
* @param date 出生日期
* @return 星座名
*/
public static String getZodiac(Date date) {
return getZodiac(DateUtil.calendar(date));
}

/**
* 通过生日计算星座
*
* @param calendar 出生日期
* @return 星座名
*/
public static String getZodiac(Calendar calendar) {
if (null == calendar) {
return null;
}
return getZodiac(calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
}

/**
* 通过生日计算星座
*
* @param month 月,从0开始计数
* @param day 天
* @return 星座名
*/
public static String getZodiac(int month, int day) {
// 在分隔日前为前一个星座,否则为后一个星座
return day < dayArr[month] ? ZODIACS[month] : ZODIACS[month + 1];
}

// ----------------------------------------------------------------------------------------------------------- 生肖
/**
* 通过生日计算生肖,只计算1900年后出生的人
*
* @param date 出生日期(年需农历)
* @return 星座名
*/
public static String getChineseZodiac(Date date) {
return getChineseZodiac(DateUtil.calendar(date));
}

/**
* 通过生日计算生肖,只计算1900年后出生的人
*
* @param calendar 出生日期(年需农历)
* @return 星座名
*/
public static String getChineseZodiac(Calendar calendar) {
if (null == calendar) {
return null;
}
return getChineseZodiac(calendar.get(Calendar.YEAR));
}

/**
* 计算生肖,只计算1900年后出生的人
*
* @param year 农历年
* @return 生肖名
*/
public static String getChineseZodiac(int year) {
if (year < 1900) {
return null;
}
return CHINESE_ZODIACS[(year - 1900) % CHINESE_ZODIACS.length];
}
}
4 changes: 2 additions & 2 deletions hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1048,11 +1048,11 @@ public static void move(File src, File dest, boolean isOverride) throws IORuntim
// 在文件系统不同的情况下,renameTo会失败,此时使用copy,然后删除原文件
try {
copy(src, dest, isOverride);
src.delete();
} catch (Exception e) {
throw new IORuntimeException(StrUtil.format("Move [{}] to [{}] failed!", src, dest), e);
}

//复制后删除源
del(src);
}
}

Expand Down
30 changes: 20 additions & 10 deletions hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,10 @@ public static void transform(Source source, Result result, String charset, boole
public static Document createXml() {
return createDocumentBuilder().newDocument();
}

/**
* 创建 DocumentBuilder
*
* @return DocumentBuilder
* @since 4.1.2
*/
Expand Down Expand Up @@ -768,23 +769,32 @@ public static Element appendChild(Node node, String tagName) {
*/
private static void mapToXml(Document doc, Element element, Map<?, ?> data) {
Element filedEle;
Object value;
Object key;
for (Entry<?, ?> entry : data.entrySet()) {
filedEle = doc.createElement(entry.getKey().toString());
element.appendChild(filedEle);
value = entry.getValue();
if (value instanceof Map) {
mapToXml(doc, filedEle, (Map<?, ?>) value);
key = entry.getKey();
if (null != key) {
// key作为标签名
filedEle = doc.createElement(key.toString());
element.appendChild(filedEle);
} else {
filedEle.appendChild(doc.createTextNode(value.toString()));
final Object value = entry.getValue();
// value作为标签内的值。
if (null != value) {
if (value instanceof Map) {
//如果值依旧为map,递归继续
mapToXml(doc, filedEle, (Map<?, ?>) value);
element.appendChild(filedEle);
} else {
filedEle.appendChild(doc.createTextNode(value.toString()));
}
}
}
}
}

/**
* 关闭XXE,避免漏洞攻击<br>
* see: https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#JAXP_DocumentBuilderFactory.2C_SAXParserFactory_and_DOM4J
*
* @param dbf DocumentBuilderFactory
* @return DocumentBuilderFactory
*/
Expand Down
19 changes: 19 additions & 0 deletions hutool-core/src/test/java/cn/hutool/core/date/ZodiacTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.hutool.core.date;

import org.junit.Assert;
import org.junit.Test;

public class ZodiacTest {

@Test
public void getZodiacTest() {
Assert.assertEquals("处女座", Zodiac.getZodiac(7, 28));
}

@Test
public void getChineseZodiacTest() {
Assert.assertEquals("狗", Zodiac.getChineseZodiac(1994));
Assert.assertEquals("狗", Zodiac.getChineseZodiac(2018));
Assert.assertEquals("猪", Zodiac.getChineseZodiac(2019));
}
}
Loading

0 comments on commit 4ce3231

Please sign in to comment.