Skip to content

Commit

Permalink
fix Date bug
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Aug 9, 2017
1 parent ecda579 commit 000cc70
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* 修复DateUtil.beginOfYear问题(感谢@【北京】iisimpler)
* 修正Email正则,符合RFC 5322规范(感谢@【北京】iisimpler)
* 修正ArrayUtil.isEmpty逻辑(感谢@【北京】仓山有井名为空)
* 修复计算第几周时没有考虑每周第一天的情况(DateTime增加setFirstDayOfWeek方法),并设置默认值为周一(@【北京】仓山有井名为空)

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

Expand Down
40 changes: 38 additions & 2 deletions hutool-core/src/main/java/com/xiaoleilu/hutool/date/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class DateTime extends Date {

/** 是否可变对象 */
private boolean mutable = true;
/** 一周的第一天,默认是周一 */
private Week firstDayOfWeek = Week.MONDAY;

/**
* 转换JDK date为 DateTime
Expand Down Expand Up @@ -142,7 +144,7 @@ public DateTime(String dateStr, DateParser dateParser) {
* @param offsite 偏移量,正数为向后偏移,负数为向前偏移
* @return 如果此对象为可变对象,返回自身,否则返回新对象
*/
public DateTime offsite(DateField datePart, int offsite) {
public DateTime offset(DateField datePart, int offsite) {
final Calendar cal = toCalendar();
cal.add(datePart.getValue(), offsite);

Expand All @@ -159,7 +161,7 @@ public DateTime offsite(DateField datePart, int offsite) {
* @return 如果此对象为可变对象,返回自身,否则返回新对象
* @since 3.0.9
*/
public DateTime offsiteNew(DateField datePart, int offsite) {
public DateTime offsetNew(DateField datePart, int offsite) {
final Calendar cal = toCalendar();
cal.add(datePart.getValue(), offsite);

Expand Down Expand Up @@ -289,17 +291,25 @@ public Month monthEnum() {

/**
* 获得指定日期是所在年份的第几周<br>
* 此方法返回值与一周的第一天有关,比如:<br>
* 2016年1月3日为周日,如果一周的第一天为周日,那这天是第二周(返回2)<br>
* 如果一周的第一天为周一,那这天是第一周(返回1)
*
* @return 周
* @see #setFirstDayOfWeek(Week)
*/
public int weekOfYear() {
return getField(DateField.WEEK_OF_YEAR);
}

/**
* 获得指定日期是所在月份的第几周<br>
* 此方法返回值与一周的第一天有关,比如:<br>
* 2016年1月3日为周日,如果一周的第一天为周日,那这天是第二周(返回2)<br>
* 如果一周的第一天为周一,那这天是第一周(返回1)
*
* @return 周
* @see #setFirstDayOfWeek(Week)
*/
public int weekOfMonth() {
return getField(DateField.WEEK_OF_MONTH);
Expand Down Expand Up @@ -414,6 +424,7 @@ public boolean isLeapYear() {
*/
public Calendar toCalendar() {
final Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(firstDayOfWeek.getValue());
cal.setTime(this);
return cal;
}
Expand All @@ -425,6 +436,7 @@ public Calendar toCalendar() {
*/
public Calendar toCalendar(Locale locale) {
final Calendar cal = Calendar.getInstance(locale);
cal.setFirstDayOfWeek(firstDayOfWeek.getValue());
cal.setTime(this);
return cal;
}
Expand All @@ -446,6 +458,7 @@ public Calendar toCalendar(TimeZone zone) {
*/
public Calendar toCalendar(TimeZone zone, Locale locale) {
final Calendar cal = Calendar.getInstance(zone, locale);
cal.setFirstDayOfWeek(firstDayOfWeek.getValue());
cal.setTime(this);
return cal;
}
Expand Down Expand Up @@ -572,6 +585,29 @@ public DateTime setMutable(boolean mutable) {
this.mutable = mutable;
return this;
}

/**
* 获得一周的第一天,默认为周一
* @return 一周的第一天
*/
public Week getFirstDayOfWeek() {
return firstDayOfWeek;
}

/**
* 设置一周的第一天<br>
* JDK的Calendar中默认一周的第一天是周日,Hutool中将此默认值设置为周一<br>
* 设置一周的第一天主要影响{@link #weekOfMonth()}和{@link #weekOfYear()} 两个方法
*
* @param firstDayOfWeek 一周的第一天
* @return this
* @see #weekOfMonth()
* @see #weekOfYear()
*/
public DateTime setFirstDayOfWeek(Week firstDayOfWeek) {
this.firstDayOfWeek = firstDayOfWeek;
return this;
}

// -------------------------------------------------------------------- toString start
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1339,12 +1339,12 @@ public static Object[] wrap(Object obj) {
* 对象是否为数组对象
*
* @param obj 对象
* @return 是否为数组对象
* @throws NullPointerException 提供被监测的对象为<code>null</code>
* @return 是否为数组对象,如果为{@code null} 返回false
*/
public static boolean isArray(Object obj) {
if (null == obj) {
throw new NullPointerException("Object check for isArray is null");
// throw new NullPointerException("Object check for isArray is null");
return false;
}
return obj.getClass().isArray();
}
Expand Down Expand Up @@ -2487,11 +2487,11 @@ public static float min(float[] numberArray) {
}

/**
* 取最小值
* 取最大值
*
* @param <T> 元素类型
* @param numberArray 数字数组
* @return 最小值
* @return 最大值
* @since 3.0.9
*/
public static <T extends Comparable<? super T>> T max(T[] numberArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,11 @@ public boolean accept(T t) {
*/
public static <K, V> Map<K, V> filter(Map<K, V> map, Editor<Entry<K, V>> editor) {
final Map<K, V> map2 = ObjectUtil.clone(map);
if(isEmpty(map2)) {
return map2;
}

map2.clear();

Entry<K, V> modified;
for (Entry<K, V> entry : map.entrySet()) {
modified = editor.edit(entry);
Expand Down Expand Up @@ -912,8 +915,11 @@ public static <K, V> Map<K, V> filter(Map<K, V> map, Editor<Entry<K, V>> editor)
*/
public static <K, V> Map<K, V> filter(Map<K, V> map, Filter<Entry<K, V>> filter) {
final Map<K, V> map2 = ObjectUtil.clone(map);
map2.clear();
if(isEmpty(map2)) {
return map2;
}

map2.clear();
for (Entry<K, V> entry : map.entrySet()) {
if (filter.accept(entry)) {
map2.put(entry.getKey(), entry.getValue());
Expand All @@ -932,9 +938,11 @@ public static <K, V> Map<K, V> filter(Map<K, V> map, Filter<Entry<K, V>> filter)
*/
public static <T> int count(Iterable<T> iterable, Matcher<T> matcher) {
int count = 0;
for (T t : iterable) {
if (null == matcher || matcher.match(t)) {
count++;
if(null != iterable) {
for (T t : iterable) {
if (null == matcher || matcher.match(t)) {
count++;
}
}
}
return count;
Expand Down
17 changes: 17 additions & 0 deletions hutool-core/src/main/java/com/xiaoleilu/hutool/util/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.imageio.stream.ImageOutputStream;

import com.xiaoleilu.hutool.exceptions.UtilException;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.io.IORuntimeException;
import com.xiaoleilu.hutool.lang.Base64;

Expand Down Expand Up @@ -972,6 +973,22 @@ public static void writePng(Image image, ImageOutputStream destImageStream) thro
}
}

/**
* 写出图像为目标文件扩展名对应的格式
*
* @param image {@link Image}
* @param targetFile 目标文件
* @throws IORuntimeException IO异常
* @since 3.1.0
*/
public static void write(Image image, File targetFile) throws IORuntimeException{
try {
ImageIO.write(toBufferedImage(image), FileUtil.extName(targetFile), targetFile);// 输出到文件
} catch (IOException e) {
throw new IORuntimeException(e);
}
}

/**
* 获得{@link ImageReader}
* @param type 图片文件类型,例如 "jpeg" 或 "tiff"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public void mutableTest(){
DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);

//默认情况下DateTime为可变对象
DateTime offsite = dateTime.offsite(DateField.YEAR, 0);
DateTime offsite = dateTime.offset(DateField.YEAR, 0);
Assert.assertTrue(offsite == dateTime);

//设置为不可变对象后变动将返回新对象
dateTime.setMutable(false);
offsite = dateTime.offsite(DateField.YEAR, 0);
offsite = dateTime.offset(DateField.YEAR, 0);
Assert.assertFalse(offsite == dateTime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,15 @@ public void currentTest(){
String currentNanoStr = String.valueOf(currentNano);
Assert.assertNotNull(currentNanoStr);
}

@Test
public void weekOfYearTest() {
//第一周周日
int weekOfYear1 = DateUtil.weekOfYear(DateUtil.parse("2016-01-03"));
Assert.assertEquals(1, weekOfYear1);

//第二周周四
int weekOfYear2 = DateUtil.weekOfYear(DateUtil.parse("2016-01-07"));
Assert.assertEquals(2, weekOfYear2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public DateTime step(DateTime current, DateTime end, int index) {
if(current.isAfterOrEquals(end)) {
return null;
}
return current.offsiteNew(DateField.DAY_OF_YEAR, 1);
return current.offset(DateField.DAY_OF_YEAR, 1);
}

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ public void getLocalMacAddressTest(){
boolean match = ReUtil.isMatch(PatternPool.MAC_ADDRESS, macAddress);
Assert.assertTrue(match);
}

@Test
public void longToIpTest() {
String ipv4 = NetUtil.longToIpv4(2130706433L);
Assert.assertEquals("127.0.0.1", ipv4);
}

@Test
public void ipToLongTest() {
long ipLong = NetUtil.ipv4ToLong("127.0.0.1");
Assert.assertEquals(2130706433L, ipLong);
}
}
12 changes: 11 additions & 1 deletion hutool-db/src/main/java/com/xiaoleilu/hutool/db/sql/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,24 @@ public void setSufWrapQuote(Character sufWrapQuote) {
this.sufWrapQuote = sufWrapQuote;
}
//--------------------------------------------------------------- Getters and Setters end

/**
* 包装字段名<br>
* 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
* @param field 字段名
* @return 包装后的字段名
*/
public String wrap(String field){
return wrapWord(field);
}

/**
* 包装字段名<br>
* 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
* @param field 字段名
* @return 包装后的字段名
*/
private String wrapWord(String field){
if(preWrapQuote == null || sufWrapQuote == null || StrUtil.isBlank(field)) {
return field;
}
Expand Down
11 changes: 11 additions & 0 deletions hutool-db/src/test/java/com/xiaoleilu/hutool/db/CRUDTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.xiaoleilu.hutool.db;

import java.sql.SQLException;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.xiaoleilu.hutool.db.ds.DSFactory;
import com.xiaoleilu.hutool.db.handler.EntityListHandler;
import com.xiaoleilu.hutool.lang.Console;
import com.xiaoleilu.hutool.util.CollectionUtil;

Expand Down Expand Up @@ -47,6 +49,15 @@ public void crudTest() throws SQLException{
Assert.assertNull(result3);
}

@Test
@Ignore
public void findTest() throws SQLException {
List<Entity> find = runner.find(CollectionUtil.newArrayList("name AS name2"), Entity.create("user"), new EntityListHandler());
for (Entity entity : find) {
Console.log(entity);
}
}

@Test
@Ignore
public void insertBatchTest() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package com.xiaoleilu.hutool.poi.test;
import java.util.List;

import org.junit.Ignore;
Expand Down

0 comments on commit 000cc70

Please sign in to comment.