From eefdd7fc4f5a37d0a81a11ceb8082af3425a4b22 Mon Sep 17 00:00:00 2001 From: zhangrenhua Date: Mon, 11 Jul 2022 16:15:46 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=89=A9=E5=B1=95IoUtil=E3=80=81BooleanUti?= =?UTF-8?q?l=E4=B8=A4=E4=B8=AA=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/io/IoUtil.java | 39 +++++++++++++++++++ .../java/cn/hutool/core/util/BooleanUtil.java | 17 ++++++++ .../java/cn/hutool/core/io/IoUtilTest.java | 28 ++++++++++++- .../cn/hutool/core/util/BooleanUtilTest.java | 38 +++++++++++++++++- 4 files changed, 118 insertions(+), 4 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java index b259bbdde..eced4215e 100755 --- a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java @@ -186,6 +186,45 @@ public class IoUtil extends NioUtil { } } + /** + * 复制InputStream中的内容到byte[]中,并返回复制后的byte[] + * @param in InputStream + * @return 返回从InputStream中复制出来的byte[]内容 + * @throws IOException + * @author ZRH 455741807@qq.com + */ + public static byte[] copyToByteArray(InputStream in) throws IOException { + if (in == null) { + return new byte[0]; + } + ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE); + copy(in, out); + return out.toByteArray(); + } + + /** + * 复制InputStream中的内容为字符串,并返回字符串内容 + * @param in InputStream + * @param charset 编码 + * @return 返回从InputStream中复制出来的字符串内容,如果InputStream为null,返回“” + * @throws IOException + * @author ZRH 455741807@qq.com + */ + public static String copyToString(InputStream in, Charset charset) throws IOException { + if (in == null) { + return ""; + } + + StringBuilder out = new StringBuilder(DEFAULT_BUFFER_SIZE); + InputStreamReader reader = new InputStreamReader(in, charset); + char[] buffer = new char[DEFAULT_BUFFER_SIZE]; + int charsRead; + while ((charsRead = reader.read(buffer)) != -1) { + out.append(buffer, 0, charsRead); + } + return out.toString(); + } + // -------------------------------------------------------------------------------------- Copy end // -------------------------------------------------------------------------------------- getReader and getWriter start diff --git a/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java index 2d215e82b..4a148be69 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java @@ -18,6 +18,23 @@ public class BooleanUtil { /** 表示为假的字符串 */ private static final Set FALSE_SET = CollUtil.newHashSet("false", "no", "n", "f", "0", "off", "否", "错", "假", "錯", "×"); + /** + * 验证前端传过来的字符串,是不是正确的bool值 + * @param boolStr + * @return boolStr是TRUE_SET、FALSE_SET中的字符串时,才返回true;其余内容返回false + * @author ZRH 455741807@qq.com + */ + public static boolean verifyBooleanString(String boolStr){ + if(StrUtil.isBlank(boolStr)){ + return false; + } + boolStr = boolStr.toLowerCase(); + if(TRUE_SET.contains(boolStr) || FALSE_SET.contains(boolStr)){ + return true; + } + return false; + } + /** * 取相反值 * diff --git a/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java index c0caa2500..d29cc9851 100644 --- a/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java @@ -5,8 +5,8 @@ import cn.hutool.core.util.RandomUtil; import org.junit.Assert; import org.junit.Test; -import java.io.BufferedReader; -import java.io.IOException; +import java.io.*; +import java.nio.charset.Charset; public class IoUtilTest { @@ -32,4 +32,28 @@ public class IoUtilTest { throw new IORuntimeException(e); } } + + @Test + public void copyToByteArrayTest() throws Exception { + try(InputStream is1 = ResourceUtil.getStream("hutool.jpg"); + InputStream is2 = ResourceUtil.getStream("hutool.jpg")){ + byte[] copiedBytes = IoUtil.copyToByteArray(is1); + byte[] readBytes = IoUtil.readBytes(is2); + Assert.assertArrayEquals(readBytes, copiedBytes); + } + } + + @Test + public void copyToStringTest() throws Exception { + String str = "abc123"; + try(InputStream is1 = new ByteArrayInputStream(str.getBytes(Charset.defaultCharset())); + InputStream is2 = new ByteArrayInputStream(str.getBytes(Charset.defaultCharset()))){ + String copiedString = IoUtil.copyToString(is1, Charset.defaultCharset()); + String readString = IoUtil.read(is2, Charset.defaultCharset()); + Assert.assertEquals(readString, copiedString); + } + + + } + } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java index c6f458596..7e8860248 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java @@ -4,7 +4,41 @@ import org.junit.Assert; import org.junit.Test; public class BooleanUtilTest { - + + @Test + public void testVerifyBooleanString(){ + /*true的各种形式*/ + Assert.assertTrue(BooleanUtil.verifyBooleanString("true")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("yes")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("t")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("OK")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("1")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("On")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("是")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("对")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("真")); + + /*false的各种形式*/ + Assert.assertTrue(BooleanUtil.verifyBooleanString("false")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("no")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("n")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("f")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("0")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("off")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("否")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("错")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("假")); + Assert.assertTrue(BooleanUtil.verifyBooleanString("錯")); + + /*非正常的bool字符串*/ + Assert.assertFalse(BooleanUtil.verifyBooleanString(null)); + Assert.assertFalse(BooleanUtil.verifyBooleanString("")); + Assert.assertFalse(BooleanUtil.verifyBooleanString("x")); + Assert.assertFalse(BooleanUtil.verifyBooleanString("a")); + Assert.assertFalse(BooleanUtil.verifyBooleanString("99")); + Assert.assertFalse(BooleanUtil.verifyBooleanString("q23")); + } + @Test public void toBooleanTest() { Assert.assertTrue(BooleanUtil.toBoolean("true")); @@ -16,7 +50,7 @@ public class BooleanUtilTest { Assert.assertTrue(BooleanUtil.toBoolean("是")); Assert.assertTrue(BooleanUtil.toBoolean("对")); Assert.assertTrue(BooleanUtil.toBoolean("真")); - + Assert.assertFalse(BooleanUtil.toBoolean("false")); Assert.assertFalse(BooleanUtil.toBoolean("6455434")); Assert.assertFalse(BooleanUtil.toBoolean("")); From 064b163754a0125e6ac9a5958f9abcd9ec299aa6 Mon Sep 17 00:00:00 2001 From: zhangrenhua Date: Thu, 21 Jul 2022 15:33:33 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=8F=82=E8=80=83java.util.concurrent.Time?= =?UTF-8?q?Unit=E5=AE=9E=E7=8E=B0=E4=BA=86=E4=B8=80=E4=B8=AAFileSizeUnit?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=8C=85=EF=BC=8C=E4=B8=8Ehutool=E4=B8=ADDat?= =?UTF-8?q?aSize=E7=9B=B8=E6=AF=94=E6=9B=B4=E5=8A=A0=E4=BE=A7=E9=87=8D?= =?UTF-8?q?=E4=BA=8E=E5=8D=95=E4=BD=8D=E4=B9=8B=E9=97=B4=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/io/unit/FileSizeUnit.java | 233 ++++++++++++++++++ .../hutool/core/io/unit/FileSizeUnitTest.java | 120 +++++++++ 2 files changed, 353 insertions(+) create mode 100644 hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java create mode 100644 hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java diff --git a/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java b/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java new file mode 100644 index 000000000..8d0da3ef3 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java @@ -0,0 +1,233 @@ +package cn.hutool.core.io.unit; + + +import java.text.DecimalFormat; + +/** + * 文件大小单位转换,方便于各种单位之间的转换 + * @author zrh 455741807@qq.com + * @date 2022-07-21 + */ +public enum FileSizeUnit { + /**字节*/ + BYTE { + @Override + public long toByte(long duration) { + return duration; + } + + @Override + public long toKB(long duration) { + return duration/(CKB/CBYTE); + } + + @Override + public long toMB(long duration) { + return duration/(CMB/CBYTE); + } + + @Override + public long toGB(long duration) { + return duration/(CGB/CBYTE); + } + + @Override + public long toTB(long duration) { + return duration/(CTB/CBYTE); + } + }, + + /**KB*/ + KB { + @Override + public long toByte(long duration) { + return x(duration, CKB/CBYTE, MAX/(CKB/CBYTE)); + } + + @Override + public long toKB(long duration) { + return duration; + } + + @Override + public long toMB(long duration) { + return duration/(CMB/CKB); + } + + @Override + public long toGB(long duration) { + return duration/(CGB/CKB); + } + + @Override + public long toTB(long duration) { + return duration/(CTB/CKB); + } + }, + + /**MB*/ + MB { + @Override + public long toByte(long duration) { + return x(duration, CMB/CBYTE, MAX/(CMB/CBYTE)); + } + + @Override + public long toKB(long duration) { + return x(duration, CMB/CKB, MAX/(CMB/CKB)); + } + + @Override + public long toMB(long duration) { + return duration; + } + + @Override + public long toGB(long duration) { + return duration/(CGB/CMB); + } + + @Override + public long toTB(long duration) { + return duration/(CTB/CMB); + } + }, + + /**GB*/ + GB { + @Override + public long toByte(long duration) { + return x(duration, CGB/CBYTE, MAX/(CGB/CBYTE)); + } + + @Override + public long toKB(long duration) { + return x(duration, CGB/CKB, MAX/(CGB/CKB)); + } + + @Override + public long toMB(long duration) { + return x(duration, CGB/CMB, MAX/(CGB/CMB)); + } + + @Override + public long toGB(long duration) { + return duration; + } + + @Override + public long toTB(long duration) { + return duration/(CTB/CGB); + } + }, + + /**TB*/ + TB { + @Override + public long toByte(long duration) { + return x(duration, CTB/CBYTE, MAX/(CTB/CBYTE)); + } + + @Override + public long toKB(long duration) { + return x(duration, CTB/CKB, MAX/(CTB/CKB)); + } + + @Override + public long toMB(long duration) { + return x(duration, CTB/CMB, MAX/(CTB/CMB)); + } + + @Override + public long toGB(long duration) { + return x(duration, CTB/CGB, MAX/(CTB/CTB)); + } + + @Override + public long toTB(long duration) { + return duration; + } + } + ; + + private static final long CBYTE = 1; + private static final long CKB = 1024; + private static final long CMB = (CKB * CKB); + private static final long CGB = (CKB * CMB); + private static final long CTB = (CKB * CGB); + + private static final long MAX = Long.MAX_VALUE; + + /** + * Scale d by m, checking for overflow. + * This has a short name to make above code more readable. + */ + private static long x(long d, long m, long over) { + if (d > over) { return Long.MAX_VALUE; } + if (d < -over) { return Long.MIN_VALUE; } + return d * m; + } + + + public long toByte(long duration) { throw new AbstractMethodError(); } + public long toKB(long duration) { throw new AbstractMethodError(); } + public long toMB(long duration) { throw new AbstractMethodError(); } + public long toGB(long duration) { throw new AbstractMethodError(); } + public long toTB(long duration) { throw new AbstractMethodError(); } + + /** + * 文件大小转换为可读带单位的字符串,如1.2GB、300.0MB等 + * @param length 文件大小,java.io.File.length()返回值,单位为Bytes + * @return + */ + public static String readableSizeStr(long length) { + return readableSizeStr(length, FileSizeUnit.BYTE); + } + + /** + * 文件大小转换为可读带单位的字符串,如1.2GB、300MB等 + * @param size 文件大小 + * @param unit 单位 + * @return + */ + public static String readableSizeStr(long size, FileSizeUnit unit) { + if(size <= 0 || unit == null) { + return ""; + } + long byteSize = unit.toByte(size); + String[] fileSizeArr = getFileSize(byteSize); + return fileSizeArr[0]+fileSizeArr[1]; + } + + /** + * 以合适的单位返回文件大小,返回数组,String[0]为文件大小数字,String[1]为单位 + * @param length The length, in bytes + * @return 返回数组,String[0]为文件大小数字,String[1]为单位 + */ + private static String[] getFileSize(long length) { + if(length <= 0) { + return new String[]{"0","Byte"}; + } + + String[] size = new String[2]; + DecimalFormat df = new DecimalFormat("#.0"); + if (length < CKB) { + size[0] = df.format(length); + size[1] = "Byte"; + } else if (length < CMB) { + size[0] = df.format( length*1.0/ CKB); + size[1] = "KB"; + } else if (length < CGB) { + size[0] = df.format(length*1.0/ CMB); + size[1] = "MB"; + } else if (length < CTB) { + size[0] = df.format(length*1.0/ CGB); + size[1] = "GB"; + } else { + size[0] = df.format(length*1.0/ CTB); + size[1] = "TB"; + } + return size; + } + +} diff --git a/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java b/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java new file mode 100644 index 000000000..3dd4a5bd9 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java @@ -0,0 +1,120 @@ +package cn.hutool.core.io.unit; + +import org.junit.Assert; +import org.junit.Test; + +/** + * 文件大小单位转换,方便于各种单位之间的转换 + * @author zrh 455741807@qq.com + * @date 2022-07-21 + */ +public class FileSizeUnitTest { + private static final long Byte = 1; + private static final long KB = 1024; + private static final long MB = (KB * KB); + private static final long GB = (KB * MB); + private static final long TB = (KB * GB); + + @Test + public void toByte() { + long byteS = 123, kbS = 1, mbS = 1, gbS = 1, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toByte(byteS); + long kbT = FileSizeUnit.KB.toByte(kbS); + long mbT = FileSizeUnit.MB.toByte(mbS); + long gbT = FileSizeUnit.GB.toByte(gbS); + long tbT = FileSizeUnit.TB.toByte(tbS); + + Assert.assertEquals(byteS, byteT); + Assert.assertEquals(kbS*(KB/Byte), kbT); + Assert.assertEquals(mbS*(MB/Byte), mbT); + Assert.assertEquals(gbS*(GB/Byte), gbT); + Assert.assertEquals(tbS*(TB/Byte), tbT); + + + } + + @Test + public void toKB() { + long byteS = 1230, kbS = 456, mbS = 1, gbS = 1, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toKB(byteS); + long kbT = FileSizeUnit.KB.toKB(kbS); + long mbT = FileSizeUnit.MB.toKB(mbS); + long gbT = FileSizeUnit.GB.toKB(gbS); + long tbT = FileSizeUnit.TB.toKB(tbS); + + Assert.assertEquals(byteS/(KB/Byte), byteT); + Assert.assertEquals(kbS, kbT); + Assert.assertEquals(mbS*(MB/KB), mbT); + Assert.assertEquals(gbS*(GB/KB), gbT); + Assert.assertEquals(tbS*(TB/KB), tbT); + } + + @Test + public void toMB() { + long byteS = 1230000, kbS = 456000, mbS = 789, gbS = 1, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toMB(byteS); + long kbT = FileSizeUnit.KB.toMB(kbS); + long mbT = FileSizeUnit.MB.toMB(mbS); + long gbT = FileSizeUnit.GB.toMB(gbS); + long tbT = FileSizeUnit.TB.toMB(tbS); + + Assert.assertEquals(byteS/(MB/Byte), byteT); + Assert.assertEquals(kbS/(MB/KB), kbT); + Assert.assertEquals(mbS, mbT); + Assert.assertEquals(gbS*(GB/MB), gbT); + Assert.assertEquals(tbS*(TB/MB), tbT); + } + + @Test + public void toGB() { + long byteS = 1230000000, kbS = 456000000, mbS = 789000, gbS = 1, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toGB(byteS); + long kbT = FileSizeUnit.KB.toGB(kbS); + long mbT = FileSizeUnit.MB.toGB(mbS); + long gbT = FileSizeUnit.GB.toGB(gbS); + long tbT = FileSizeUnit.TB.toGB(tbS); + + Assert.assertEquals(byteS/(GB/Byte), byteT); + Assert.assertEquals(kbS/(GB/KB), kbT); + Assert.assertEquals(mbS/(GB/MB), mbT); + Assert.assertEquals(gbS, gbT); + Assert.assertEquals(tbS*(TB/GB), tbT); + + } + + @Test + public void toTB() { + long byteS = 1230000000000L, kbS = 111000000000L, mbS = 789000000L, gbS = 1024, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toTB(byteS); + long kbT = FileSizeUnit.KB.toTB(kbS); + long mbT = FileSizeUnit.MB.toTB(mbS); + long gbT = FileSizeUnit.GB.toTB(gbS); + long tbT = FileSizeUnit.TB.toTB(tbS); + + Assert.assertEquals(byteS/(TB/Byte), byteT); + Assert.assertEquals(kbS/(TB/KB), kbT); + Assert.assertEquals(mbS/(TB/MB), mbT); + Assert.assertEquals(gbS/(TB/GB), gbT); + Assert.assertEquals(tbS, tbT); + } + + @Test + public void readableSizeStr1() { + long size = 1024*1024; + String sizeStr = FileSizeUnit.readableSizeStr(size); + Assert.assertEquals("1.0MB", sizeStr); + } + + @Test + public void readableSizeStr2() { + long size = 123*1024; + String sizeStr = FileSizeUnit.readableSizeStr(size, FileSizeUnit.MB); + Assert.assertEquals("123.0GB", sizeStr); + } + +} From 8d2e4c3b4cf5456bfd33656b694a05d795f04269 Mon Sep 17 00:00:00 2001 From: zhangrenhua Date: Thu, 21 Jul 2022 15:36:05 +0800 Subject: [PATCH 3/8] =?UTF-8?q?Revert=20"=E6=89=A9=E5=B1=95IoUtil=E3=80=81?= =?UTF-8?q?BooleanUtil=E4=B8=A4=E4=B8=AA=E5=B7=A5=E5=85=B7=E7=B1=BB"=20Thi?= =?UTF-8?q?s=20reverts=20commit=20eefdd7fc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/io/IoUtil.java | 39 ------------------- .../java/cn/hutool/core/util/BooleanUtil.java | 17 -------- .../java/cn/hutool/core/io/IoUtilTest.java | 28 +------------ .../cn/hutool/core/util/BooleanUtilTest.java | 38 +----------------- 4 files changed, 4 insertions(+), 118 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java index eced4215e..b259bbdde 100755 --- a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java @@ -186,45 +186,6 @@ public class IoUtil extends NioUtil { } } - /** - * 复制InputStream中的内容到byte[]中,并返回复制后的byte[] - * @param in InputStream - * @return 返回从InputStream中复制出来的byte[]内容 - * @throws IOException - * @author ZRH 455741807@qq.com - */ - public static byte[] copyToByteArray(InputStream in) throws IOException { - if (in == null) { - return new byte[0]; - } - ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE); - copy(in, out); - return out.toByteArray(); - } - - /** - * 复制InputStream中的内容为字符串,并返回字符串内容 - * @param in InputStream - * @param charset 编码 - * @return 返回从InputStream中复制出来的字符串内容,如果InputStream为null,返回“” - * @throws IOException - * @author ZRH 455741807@qq.com - */ - public static String copyToString(InputStream in, Charset charset) throws IOException { - if (in == null) { - return ""; - } - - StringBuilder out = new StringBuilder(DEFAULT_BUFFER_SIZE); - InputStreamReader reader = new InputStreamReader(in, charset); - char[] buffer = new char[DEFAULT_BUFFER_SIZE]; - int charsRead; - while ((charsRead = reader.read(buffer)) != -1) { - out.append(buffer, 0, charsRead); - } - return out.toString(); - } - // -------------------------------------------------------------------------------------- Copy end // -------------------------------------------------------------------------------------- getReader and getWriter start diff --git a/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java index 4a148be69..2d215e82b 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java @@ -18,23 +18,6 @@ public class BooleanUtil { /** 表示为假的字符串 */ private static final Set FALSE_SET = CollUtil.newHashSet("false", "no", "n", "f", "0", "off", "否", "错", "假", "錯", "×"); - /** - * 验证前端传过来的字符串,是不是正确的bool值 - * @param boolStr - * @return boolStr是TRUE_SET、FALSE_SET中的字符串时,才返回true;其余内容返回false - * @author ZRH 455741807@qq.com - */ - public static boolean verifyBooleanString(String boolStr){ - if(StrUtil.isBlank(boolStr)){ - return false; - } - boolStr = boolStr.toLowerCase(); - if(TRUE_SET.contains(boolStr) || FALSE_SET.contains(boolStr)){ - return true; - } - return false; - } - /** * 取相反值 * diff --git a/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java index d29cc9851..c0caa2500 100644 --- a/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java @@ -5,8 +5,8 @@ import cn.hutool.core.util.RandomUtil; import org.junit.Assert; import org.junit.Test; -import java.io.*; -import java.nio.charset.Charset; +import java.io.BufferedReader; +import java.io.IOException; public class IoUtilTest { @@ -32,28 +32,4 @@ public class IoUtilTest { throw new IORuntimeException(e); } } - - @Test - public void copyToByteArrayTest() throws Exception { - try(InputStream is1 = ResourceUtil.getStream("hutool.jpg"); - InputStream is2 = ResourceUtil.getStream("hutool.jpg")){ - byte[] copiedBytes = IoUtil.copyToByteArray(is1); - byte[] readBytes = IoUtil.readBytes(is2); - Assert.assertArrayEquals(readBytes, copiedBytes); - } - } - - @Test - public void copyToStringTest() throws Exception { - String str = "abc123"; - try(InputStream is1 = new ByteArrayInputStream(str.getBytes(Charset.defaultCharset())); - InputStream is2 = new ByteArrayInputStream(str.getBytes(Charset.defaultCharset()))){ - String copiedString = IoUtil.copyToString(is1, Charset.defaultCharset()); - String readString = IoUtil.read(is2, Charset.defaultCharset()); - Assert.assertEquals(readString, copiedString); - } - - - } - } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java index 7e8860248..c6f458596 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java @@ -4,41 +4,7 @@ import org.junit.Assert; import org.junit.Test; public class BooleanUtilTest { - - @Test - public void testVerifyBooleanString(){ - /*true的各种形式*/ - Assert.assertTrue(BooleanUtil.verifyBooleanString("true")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("yes")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("t")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("OK")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("1")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("On")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("是")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("对")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("真")); - - /*false的各种形式*/ - Assert.assertTrue(BooleanUtil.verifyBooleanString("false")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("no")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("n")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("f")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("0")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("off")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("否")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("错")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("假")); - Assert.assertTrue(BooleanUtil.verifyBooleanString("錯")); - - /*非正常的bool字符串*/ - Assert.assertFalse(BooleanUtil.verifyBooleanString(null)); - Assert.assertFalse(BooleanUtil.verifyBooleanString("")); - Assert.assertFalse(BooleanUtil.verifyBooleanString("x")); - Assert.assertFalse(BooleanUtil.verifyBooleanString("a")); - Assert.assertFalse(BooleanUtil.verifyBooleanString("99")); - Assert.assertFalse(BooleanUtil.verifyBooleanString("q23")); - } - + @Test public void toBooleanTest() { Assert.assertTrue(BooleanUtil.toBoolean("true")); @@ -50,7 +16,7 @@ public class BooleanUtilTest { Assert.assertTrue(BooleanUtil.toBoolean("是")); Assert.assertTrue(BooleanUtil.toBoolean("对")); Assert.assertTrue(BooleanUtil.toBoolean("真")); - + Assert.assertFalse(BooleanUtil.toBoolean("false")); Assert.assertFalse(BooleanUtil.toBoolean("6455434")); Assert.assertFalse(BooleanUtil.toBoolean("")); From 2a3686631dd510943477736ec8cf8622b36747c8 Mon Sep 17 00:00:00 2001 From: zhangrenhua Date: Thu, 21 Jul 2022 15:39:54 +0800 Subject: [PATCH 4/8] =?UTF-8?q?Revert=20"=E5=8F=82=E8=80=83java.util.concu?= =?UTF-8?q?rrent.TimeUnit=E5=AE=9E=E7=8E=B0=E4=BA=86=E4=B8=80=E4=B8=AAFile?= =?UTF-8?q?SizeUnit=E5=B7=A5=E5=85=B7=E5=8C=85=EF=BC=8C=E4=B8=8Ehutool?= =?UTF-8?q?=E4=B8=ADDataSize=E7=9B=B8=E6=AF=94=E6=9B=B4=E5=8A=A0=E4=BE=A7?= =?UTF-8?q?=E9=87=8D=E4=BA=8E=E5=8D=95=E4=BD=8D=E4=B9=8B=E9=97=B4=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 064b163754a0125e6ac9a5958f9abcd9ec299aa6. --- .../cn/hutool/core/io/unit/FileSizeUnit.java | 233 ------------------ .../hutool/core/io/unit/FileSizeUnitTest.java | 120 --------- 2 files changed, 353 deletions(-) delete mode 100644 hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java delete mode 100644 hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java diff --git a/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java b/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java deleted file mode 100644 index 8d0da3ef3..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java +++ /dev/null @@ -1,233 +0,0 @@ -package cn.hutool.core.io.unit; - - -import java.text.DecimalFormat; - -/** - * 文件大小单位转换,方便于各种单位之间的转换 - * @author zrh 455741807@qq.com - * @date 2022-07-21 - */ -public enum FileSizeUnit { - /**字节*/ - BYTE { - @Override - public long toByte(long duration) { - return duration; - } - - @Override - public long toKB(long duration) { - return duration/(CKB/CBYTE); - } - - @Override - public long toMB(long duration) { - return duration/(CMB/CBYTE); - } - - @Override - public long toGB(long duration) { - return duration/(CGB/CBYTE); - } - - @Override - public long toTB(long duration) { - return duration/(CTB/CBYTE); - } - }, - - /**KB*/ - KB { - @Override - public long toByte(long duration) { - return x(duration, CKB/CBYTE, MAX/(CKB/CBYTE)); - } - - @Override - public long toKB(long duration) { - return duration; - } - - @Override - public long toMB(long duration) { - return duration/(CMB/CKB); - } - - @Override - public long toGB(long duration) { - return duration/(CGB/CKB); - } - - @Override - public long toTB(long duration) { - return duration/(CTB/CKB); - } - }, - - /**MB*/ - MB { - @Override - public long toByte(long duration) { - return x(duration, CMB/CBYTE, MAX/(CMB/CBYTE)); - } - - @Override - public long toKB(long duration) { - return x(duration, CMB/CKB, MAX/(CMB/CKB)); - } - - @Override - public long toMB(long duration) { - return duration; - } - - @Override - public long toGB(long duration) { - return duration/(CGB/CMB); - } - - @Override - public long toTB(long duration) { - return duration/(CTB/CMB); - } - }, - - /**GB*/ - GB { - @Override - public long toByte(long duration) { - return x(duration, CGB/CBYTE, MAX/(CGB/CBYTE)); - } - - @Override - public long toKB(long duration) { - return x(duration, CGB/CKB, MAX/(CGB/CKB)); - } - - @Override - public long toMB(long duration) { - return x(duration, CGB/CMB, MAX/(CGB/CMB)); - } - - @Override - public long toGB(long duration) { - return duration; - } - - @Override - public long toTB(long duration) { - return duration/(CTB/CGB); - } - }, - - /**TB*/ - TB { - @Override - public long toByte(long duration) { - return x(duration, CTB/CBYTE, MAX/(CTB/CBYTE)); - } - - @Override - public long toKB(long duration) { - return x(duration, CTB/CKB, MAX/(CTB/CKB)); - } - - @Override - public long toMB(long duration) { - return x(duration, CTB/CMB, MAX/(CTB/CMB)); - } - - @Override - public long toGB(long duration) { - return x(duration, CTB/CGB, MAX/(CTB/CTB)); - } - - @Override - public long toTB(long duration) { - return duration; - } - } - ; - - private static final long CBYTE = 1; - private static final long CKB = 1024; - private static final long CMB = (CKB * CKB); - private static final long CGB = (CKB * CMB); - private static final long CTB = (CKB * CGB); - - private static final long MAX = Long.MAX_VALUE; - - /** - * Scale d by m, checking for overflow. - * This has a short name to make above code more readable. - */ - private static long x(long d, long m, long over) { - if (d > over) { return Long.MAX_VALUE; } - if (d < -over) { return Long.MIN_VALUE; } - return d * m; - } - - - public long toByte(long duration) { throw new AbstractMethodError(); } - public long toKB(long duration) { throw new AbstractMethodError(); } - public long toMB(long duration) { throw new AbstractMethodError(); } - public long toGB(long duration) { throw new AbstractMethodError(); } - public long toTB(long duration) { throw new AbstractMethodError(); } - - /** - * 文件大小转换为可读带单位的字符串,如1.2GB、300.0MB等 - * @param length 文件大小,java.io.File.length()返回值,单位为Bytes - * @return - */ - public static String readableSizeStr(long length) { - return readableSizeStr(length, FileSizeUnit.BYTE); - } - - /** - * 文件大小转换为可读带单位的字符串,如1.2GB、300MB等 - * @param size 文件大小 - * @param unit 单位 - * @return - */ - public static String readableSizeStr(long size, FileSizeUnit unit) { - if(size <= 0 || unit == null) { - return ""; - } - long byteSize = unit.toByte(size); - String[] fileSizeArr = getFileSize(byteSize); - return fileSizeArr[0]+fileSizeArr[1]; - } - - /** - * 以合适的单位返回文件大小,返回数组,String[0]为文件大小数字,String[1]为单位 - * @param length The length, in bytes - * @return 返回数组,String[0]为文件大小数字,String[1]为单位 - */ - private static String[] getFileSize(long length) { - if(length <= 0) { - return new String[]{"0","Byte"}; - } - - String[] size = new String[2]; - DecimalFormat df = new DecimalFormat("#.0"); - if (length < CKB) { - size[0] = df.format(length); - size[1] = "Byte"; - } else if (length < CMB) { - size[0] = df.format( length*1.0/ CKB); - size[1] = "KB"; - } else if (length < CGB) { - size[0] = df.format(length*1.0/ CMB); - size[1] = "MB"; - } else if (length < CTB) { - size[0] = df.format(length*1.0/ CGB); - size[1] = "GB"; - } else { - size[0] = df.format(length*1.0/ CTB); - size[1] = "TB"; - } - return size; - } - -} diff --git a/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java b/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java deleted file mode 100644 index 3dd4a5bd9..000000000 --- a/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package cn.hutool.core.io.unit; - -import org.junit.Assert; -import org.junit.Test; - -/** - * 文件大小单位转换,方便于各种单位之间的转换 - * @author zrh 455741807@qq.com - * @date 2022-07-21 - */ -public class FileSizeUnitTest { - private static final long Byte = 1; - private static final long KB = 1024; - private static final long MB = (KB * KB); - private static final long GB = (KB * MB); - private static final long TB = (KB * GB); - - @Test - public void toByte() { - long byteS = 123, kbS = 1, mbS = 1, gbS = 1, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toByte(byteS); - long kbT = FileSizeUnit.KB.toByte(kbS); - long mbT = FileSizeUnit.MB.toByte(mbS); - long gbT = FileSizeUnit.GB.toByte(gbS); - long tbT = FileSizeUnit.TB.toByte(tbS); - - Assert.assertEquals(byteS, byteT); - Assert.assertEquals(kbS*(KB/Byte), kbT); - Assert.assertEquals(mbS*(MB/Byte), mbT); - Assert.assertEquals(gbS*(GB/Byte), gbT); - Assert.assertEquals(tbS*(TB/Byte), tbT); - - - } - - @Test - public void toKB() { - long byteS = 1230, kbS = 456, mbS = 1, gbS = 1, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toKB(byteS); - long kbT = FileSizeUnit.KB.toKB(kbS); - long mbT = FileSizeUnit.MB.toKB(mbS); - long gbT = FileSizeUnit.GB.toKB(gbS); - long tbT = FileSizeUnit.TB.toKB(tbS); - - Assert.assertEquals(byteS/(KB/Byte), byteT); - Assert.assertEquals(kbS, kbT); - Assert.assertEquals(mbS*(MB/KB), mbT); - Assert.assertEquals(gbS*(GB/KB), gbT); - Assert.assertEquals(tbS*(TB/KB), tbT); - } - - @Test - public void toMB() { - long byteS = 1230000, kbS = 456000, mbS = 789, gbS = 1, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toMB(byteS); - long kbT = FileSizeUnit.KB.toMB(kbS); - long mbT = FileSizeUnit.MB.toMB(mbS); - long gbT = FileSizeUnit.GB.toMB(gbS); - long tbT = FileSizeUnit.TB.toMB(tbS); - - Assert.assertEquals(byteS/(MB/Byte), byteT); - Assert.assertEquals(kbS/(MB/KB), kbT); - Assert.assertEquals(mbS, mbT); - Assert.assertEquals(gbS*(GB/MB), gbT); - Assert.assertEquals(tbS*(TB/MB), tbT); - } - - @Test - public void toGB() { - long byteS = 1230000000, kbS = 456000000, mbS = 789000, gbS = 1, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toGB(byteS); - long kbT = FileSizeUnit.KB.toGB(kbS); - long mbT = FileSizeUnit.MB.toGB(mbS); - long gbT = FileSizeUnit.GB.toGB(gbS); - long tbT = FileSizeUnit.TB.toGB(tbS); - - Assert.assertEquals(byteS/(GB/Byte), byteT); - Assert.assertEquals(kbS/(GB/KB), kbT); - Assert.assertEquals(mbS/(GB/MB), mbT); - Assert.assertEquals(gbS, gbT); - Assert.assertEquals(tbS*(TB/GB), tbT); - - } - - @Test - public void toTB() { - long byteS = 1230000000000L, kbS = 111000000000L, mbS = 789000000L, gbS = 1024, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toTB(byteS); - long kbT = FileSizeUnit.KB.toTB(kbS); - long mbT = FileSizeUnit.MB.toTB(mbS); - long gbT = FileSizeUnit.GB.toTB(gbS); - long tbT = FileSizeUnit.TB.toTB(tbS); - - Assert.assertEquals(byteS/(TB/Byte), byteT); - Assert.assertEquals(kbS/(TB/KB), kbT); - Assert.assertEquals(mbS/(TB/MB), mbT); - Assert.assertEquals(gbS/(TB/GB), gbT); - Assert.assertEquals(tbS, tbT); - } - - @Test - public void readableSizeStr1() { - long size = 1024*1024; - String sizeStr = FileSizeUnit.readableSizeStr(size); - Assert.assertEquals("1.0MB", sizeStr); - } - - @Test - public void readableSizeStr2() { - long size = 123*1024; - String sizeStr = FileSizeUnit.readableSizeStr(size, FileSizeUnit.MB); - Assert.assertEquals("123.0GB", sizeStr); - } - -} From 0ca9627663efe4c44b27a1037215227b83ee5958 Mon Sep 17 00:00:00 2001 From: zhangrenhua Date: Thu, 21 Jul 2022 15:33:33 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=8F=82=E8=80=83java.util.concurrent.Time?= =?UTF-8?q?Unit=E5=AE=9E=E7=8E=B0=E4=BA=86=E4=B8=80=E4=B8=AAFileSizeUnit?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=8C=85=EF=BC=8C=E4=B8=8Ehutool=E4=B8=ADDat?= =?UTF-8?q?aSize=E7=9B=B8=E6=AF=94=E6=9B=B4=E5=8A=A0=E4=BE=A7=E9=87=8D?= =?UTF-8?q?=E4=BA=8E=E5=8D=95=E4=BD=8D=E4=B9=8B=E9=97=B4=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/io/unit/FileSizeUnit.java | 233 ++++++++++++++++++ .../hutool/core/io/unit/FileSizeUnitTest.java | 120 +++++++++ 2 files changed, 353 insertions(+) create mode 100644 hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java create mode 100644 hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java diff --git a/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java b/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java new file mode 100644 index 000000000..8d0da3ef3 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java @@ -0,0 +1,233 @@ +package cn.hutool.core.io.unit; + + +import java.text.DecimalFormat; + +/** + * 文件大小单位转换,方便于各种单位之间的转换 + * @author zrh 455741807@qq.com + * @date 2022-07-21 + */ +public enum FileSizeUnit { + /**字节*/ + BYTE { + @Override + public long toByte(long duration) { + return duration; + } + + @Override + public long toKB(long duration) { + return duration/(CKB/CBYTE); + } + + @Override + public long toMB(long duration) { + return duration/(CMB/CBYTE); + } + + @Override + public long toGB(long duration) { + return duration/(CGB/CBYTE); + } + + @Override + public long toTB(long duration) { + return duration/(CTB/CBYTE); + } + }, + + /**KB*/ + KB { + @Override + public long toByte(long duration) { + return x(duration, CKB/CBYTE, MAX/(CKB/CBYTE)); + } + + @Override + public long toKB(long duration) { + return duration; + } + + @Override + public long toMB(long duration) { + return duration/(CMB/CKB); + } + + @Override + public long toGB(long duration) { + return duration/(CGB/CKB); + } + + @Override + public long toTB(long duration) { + return duration/(CTB/CKB); + } + }, + + /**MB*/ + MB { + @Override + public long toByte(long duration) { + return x(duration, CMB/CBYTE, MAX/(CMB/CBYTE)); + } + + @Override + public long toKB(long duration) { + return x(duration, CMB/CKB, MAX/(CMB/CKB)); + } + + @Override + public long toMB(long duration) { + return duration; + } + + @Override + public long toGB(long duration) { + return duration/(CGB/CMB); + } + + @Override + public long toTB(long duration) { + return duration/(CTB/CMB); + } + }, + + /**GB*/ + GB { + @Override + public long toByte(long duration) { + return x(duration, CGB/CBYTE, MAX/(CGB/CBYTE)); + } + + @Override + public long toKB(long duration) { + return x(duration, CGB/CKB, MAX/(CGB/CKB)); + } + + @Override + public long toMB(long duration) { + return x(duration, CGB/CMB, MAX/(CGB/CMB)); + } + + @Override + public long toGB(long duration) { + return duration; + } + + @Override + public long toTB(long duration) { + return duration/(CTB/CGB); + } + }, + + /**TB*/ + TB { + @Override + public long toByte(long duration) { + return x(duration, CTB/CBYTE, MAX/(CTB/CBYTE)); + } + + @Override + public long toKB(long duration) { + return x(duration, CTB/CKB, MAX/(CTB/CKB)); + } + + @Override + public long toMB(long duration) { + return x(duration, CTB/CMB, MAX/(CTB/CMB)); + } + + @Override + public long toGB(long duration) { + return x(duration, CTB/CGB, MAX/(CTB/CTB)); + } + + @Override + public long toTB(long duration) { + return duration; + } + } + ; + + private static final long CBYTE = 1; + private static final long CKB = 1024; + private static final long CMB = (CKB * CKB); + private static final long CGB = (CKB * CMB); + private static final long CTB = (CKB * CGB); + + private static final long MAX = Long.MAX_VALUE; + + /** + * Scale d by m, checking for overflow. + * This has a short name to make above code more readable. + */ + private static long x(long d, long m, long over) { + if (d > over) { return Long.MAX_VALUE; } + if (d < -over) { return Long.MIN_VALUE; } + return d * m; + } + + + public long toByte(long duration) { throw new AbstractMethodError(); } + public long toKB(long duration) { throw new AbstractMethodError(); } + public long toMB(long duration) { throw new AbstractMethodError(); } + public long toGB(long duration) { throw new AbstractMethodError(); } + public long toTB(long duration) { throw new AbstractMethodError(); } + + /** + * 文件大小转换为可读带单位的字符串,如1.2GB、300.0MB等 + * @param length 文件大小,java.io.File.length()返回值,单位为Bytes + * @return + */ + public static String readableSizeStr(long length) { + return readableSizeStr(length, FileSizeUnit.BYTE); + } + + /** + * 文件大小转换为可读带单位的字符串,如1.2GB、300MB等 + * @param size 文件大小 + * @param unit 单位 + * @return + */ + public static String readableSizeStr(long size, FileSizeUnit unit) { + if(size <= 0 || unit == null) { + return ""; + } + long byteSize = unit.toByte(size); + String[] fileSizeArr = getFileSize(byteSize); + return fileSizeArr[0]+fileSizeArr[1]; + } + + /** + * 以合适的单位返回文件大小,返回数组,String[0]为文件大小数字,String[1]为单位 + * @param length The length, in bytes + * @return 返回数组,String[0]为文件大小数字,String[1]为单位 + */ + private static String[] getFileSize(long length) { + if(length <= 0) { + return new String[]{"0","Byte"}; + } + + String[] size = new String[2]; + DecimalFormat df = new DecimalFormat("#.0"); + if (length < CKB) { + size[0] = df.format(length); + size[1] = "Byte"; + } else if (length < CMB) { + size[0] = df.format( length*1.0/ CKB); + size[1] = "KB"; + } else if (length < CGB) { + size[0] = df.format(length*1.0/ CMB); + size[1] = "MB"; + } else if (length < CTB) { + size[0] = df.format(length*1.0/ CGB); + size[1] = "GB"; + } else { + size[0] = df.format(length*1.0/ CTB); + size[1] = "TB"; + } + return size; + } + +} diff --git a/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java b/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java new file mode 100644 index 000000000..3dd4a5bd9 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java @@ -0,0 +1,120 @@ +package cn.hutool.core.io.unit; + +import org.junit.Assert; +import org.junit.Test; + +/** + * 文件大小单位转换,方便于各种单位之间的转换 + * @author zrh 455741807@qq.com + * @date 2022-07-21 + */ +public class FileSizeUnitTest { + private static final long Byte = 1; + private static final long KB = 1024; + private static final long MB = (KB * KB); + private static final long GB = (KB * MB); + private static final long TB = (KB * GB); + + @Test + public void toByte() { + long byteS = 123, kbS = 1, mbS = 1, gbS = 1, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toByte(byteS); + long kbT = FileSizeUnit.KB.toByte(kbS); + long mbT = FileSizeUnit.MB.toByte(mbS); + long gbT = FileSizeUnit.GB.toByte(gbS); + long tbT = FileSizeUnit.TB.toByte(tbS); + + Assert.assertEquals(byteS, byteT); + Assert.assertEquals(kbS*(KB/Byte), kbT); + Assert.assertEquals(mbS*(MB/Byte), mbT); + Assert.assertEquals(gbS*(GB/Byte), gbT); + Assert.assertEquals(tbS*(TB/Byte), tbT); + + + } + + @Test + public void toKB() { + long byteS = 1230, kbS = 456, mbS = 1, gbS = 1, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toKB(byteS); + long kbT = FileSizeUnit.KB.toKB(kbS); + long mbT = FileSizeUnit.MB.toKB(mbS); + long gbT = FileSizeUnit.GB.toKB(gbS); + long tbT = FileSizeUnit.TB.toKB(tbS); + + Assert.assertEquals(byteS/(KB/Byte), byteT); + Assert.assertEquals(kbS, kbT); + Assert.assertEquals(mbS*(MB/KB), mbT); + Assert.assertEquals(gbS*(GB/KB), gbT); + Assert.assertEquals(tbS*(TB/KB), tbT); + } + + @Test + public void toMB() { + long byteS = 1230000, kbS = 456000, mbS = 789, gbS = 1, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toMB(byteS); + long kbT = FileSizeUnit.KB.toMB(kbS); + long mbT = FileSizeUnit.MB.toMB(mbS); + long gbT = FileSizeUnit.GB.toMB(gbS); + long tbT = FileSizeUnit.TB.toMB(tbS); + + Assert.assertEquals(byteS/(MB/Byte), byteT); + Assert.assertEquals(kbS/(MB/KB), kbT); + Assert.assertEquals(mbS, mbT); + Assert.assertEquals(gbS*(GB/MB), gbT); + Assert.assertEquals(tbS*(TB/MB), tbT); + } + + @Test + public void toGB() { + long byteS = 1230000000, kbS = 456000000, mbS = 789000, gbS = 1, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toGB(byteS); + long kbT = FileSizeUnit.KB.toGB(kbS); + long mbT = FileSizeUnit.MB.toGB(mbS); + long gbT = FileSizeUnit.GB.toGB(gbS); + long tbT = FileSizeUnit.TB.toGB(tbS); + + Assert.assertEquals(byteS/(GB/Byte), byteT); + Assert.assertEquals(kbS/(GB/KB), kbT); + Assert.assertEquals(mbS/(GB/MB), mbT); + Assert.assertEquals(gbS, gbT); + Assert.assertEquals(tbS*(TB/GB), tbT); + + } + + @Test + public void toTB() { + long byteS = 1230000000000L, kbS = 111000000000L, mbS = 789000000L, gbS = 1024, tbS = 1; + + long byteT = FileSizeUnit.BYTE.toTB(byteS); + long kbT = FileSizeUnit.KB.toTB(kbS); + long mbT = FileSizeUnit.MB.toTB(mbS); + long gbT = FileSizeUnit.GB.toTB(gbS); + long tbT = FileSizeUnit.TB.toTB(tbS); + + Assert.assertEquals(byteS/(TB/Byte), byteT); + Assert.assertEquals(kbS/(TB/KB), kbT); + Assert.assertEquals(mbS/(TB/MB), mbT); + Assert.assertEquals(gbS/(TB/GB), gbT); + Assert.assertEquals(tbS, tbT); + } + + @Test + public void readableSizeStr1() { + long size = 1024*1024; + String sizeStr = FileSizeUnit.readableSizeStr(size); + Assert.assertEquals("1.0MB", sizeStr); + } + + @Test + public void readableSizeStr2() { + long size = 123*1024; + String sizeStr = FileSizeUnit.readableSizeStr(size, FileSizeUnit.MB); + Assert.assertEquals("123.0GB", sizeStr); + } + +} From 4eb720bb31f1bc435bc09954441a77f4082810c4 Mon Sep 17 00:00:00 2001 From: zhangrenhua Date: Fri, 22 Jul 2022 09:04:33 +0800 Subject: [PATCH 6/8] =?UTF-8?q?json=E5=BA=8F=E5=88=97=E5=8C=96=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=85=81=E8=AE=B8=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E7=9A=84key=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/json/JSONConfig.java | 23 +++++++++++++++++++ .../main/java/cn/hutool/json/JSONObject.java | 4 +++- .../java/cn/hutool/json/JSONUtilTest.java | 10 ++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONConfig.java b/hutool-json/src/main/java/cn/hutool/json/JSONConfig.java index 0b437a8f3..95b1039bf 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSONConfig.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONConfig.java @@ -44,6 +44,11 @@ public class JSONConfig implements Serializable { */ private boolean stripTrailingZeros = true; + /** + * 是否忽略多个相同的key + */ + private boolean ignoreDuplicateKey = false; + /** * 创建默认的配置项 * @@ -235,4 +240,22 @@ public class JSONConfig implements Serializable { this.stripTrailingZeros = stripTrailingZeros; return this; } + + /** + * 是否忽略多个相同的key + * @return + */ + public boolean isIgnoreDuplicateKey() { + return ignoreDuplicateKey; + } + + /** + * 是否忽略多个相同的key + * @param ignoreDuplicateKey + * @return + */ + public JSONConfig setIgnoreDuplicateKey(boolean ignoreDuplicateKey) { + this.ignoreDuplicateKey = ignoreDuplicateKey; + return this; + } } diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONObject.java b/hutool-json/src/main/java/cn/hutool/json/JSONObject.java index 921d19df8..1704c8daa 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSONObject.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONObject.java @@ -392,7 +392,9 @@ public class JSONObject extends MapWrapper implements JSON, JSON // 忽略值模式下如果值为空清除key this.remove(key); } else { - if (checkDuplicate && containsKey(key)) { + /*如果允许多个key,就不抛出异常,使用后面的值覆盖前面的值*/ + boolean ignoreDuplicateKey = this.config.isIgnoreDuplicateKey(); + if (checkDuplicate && containsKey(key) && false == ignoreDuplicateKey) { throw new JSONException("Duplicate key \"{}\"", key); } diff --git a/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java b/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java index 3be9a3fbb..5f2e8bb41 100644 --- a/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java +++ b/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java @@ -234,4 +234,14 @@ public class JSONUtilTest { final String xmlStr = JSONUtil.toXmlStr(obj); Assert.assertEquals("v1abc", xmlStr); } + + @Test + public void testDuplicateKey(){ + String str = "{id:123, name:\"张三\", name:\"李四\"}"; + + JSONObject jsonObject = JSONUtil.parseObj(str, JSONConfig.create().setIgnoreDuplicateKey(true)); + System.out.println(jsonObject.toString()); + + Assert.assertNotNull(jsonObject); + } } From fe332dc2dc1b14a3909fc12a639f2437456e4258 Mon Sep 17 00:00:00 2001 From: zhangrenhua Date: Fri, 22 Jul 2022 09:09:21 +0800 Subject: [PATCH 7/8] =?UTF-8?q?json=E5=BA=8F=E5=88=97=E5=8C=96=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=85=81=E8=AE=B8=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E7=9A=84key=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/io/unit/FileSizeUnit.java | 233 ------------------ .../hutool/core/io/unit/FileSizeUnitTest.java | 120 --------- 2 files changed, 353 deletions(-) delete mode 100644 hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java delete mode 100644 hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java diff --git a/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java b/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java deleted file mode 100644 index 8d0da3ef3..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/io/unit/FileSizeUnit.java +++ /dev/null @@ -1,233 +0,0 @@ -package cn.hutool.core.io.unit; - - -import java.text.DecimalFormat; - -/** - * 文件大小单位转换,方便于各种单位之间的转换 - * @author zrh 455741807@qq.com - * @date 2022-07-21 - */ -public enum FileSizeUnit { - /**字节*/ - BYTE { - @Override - public long toByte(long duration) { - return duration; - } - - @Override - public long toKB(long duration) { - return duration/(CKB/CBYTE); - } - - @Override - public long toMB(long duration) { - return duration/(CMB/CBYTE); - } - - @Override - public long toGB(long duration) { - return duration/(CGB/CBYTE); - } - - @Override - public long toTB(long duration) { - return duration/(CTB/CBYTE); - } - }, - - /**KB*/ - KB { - @Override - public long toByte(long duration) { - return x(duration, CKB/CBYTE, MAX/(CKB/CBYTE)); - } - - @Override - public long toKB(long duration) { - return duration; - } - - @Override - public long toMB(long duration) { - return duration/(CMB/CKB); - } - - @Override - public long toGB(long duration) { - return duration/(CGB/CKB); - } - - @Override - public long toTB(long duration) { - return duration/(CTB/CKB); - } - }, - - /**MB*/ - MB { - @Override - public long toByte(long duration) { - return x(duration, CMB/CBYTE, MAX/(CMB/CBYTE)); - } - - @Override - public long toKB(long duration) { - return x(duration, CMB/CKB, MAX/(CMB/CKB)); - } - - @Override - public long toMB(long duration) { - return duration; - } - - @Override - public long toGB(long duration) { - return duration/(CGB/CMB); - } - - @Override - public long toTB(long duration) { - return duration/(CTB/CMB); - } - }, - - /**GB*/ - GB { - @Override - public long toByte(long duration) { - return x(duration, CGB/CBYTE, MAX/(CGB/CBYTE)); - } - - @Override - public long toKB(long duration) { - return x(duration, CGB/CKB, MAX/(CGB/CKB)); - } - - @Override - public long toMB(long duration) { - return x(duration, CGB/CMB, MAX/(CGB/CMB)); - } - - @Override - public long toGB(long duration) { - return duration; - } - - @Override - public long toTB(long duration) { - return duration/(CTB/CGB); - } - }, - - /**TB*/ - TB { - @Override - public long toByte(long duration) { - return x(duration, CTB/CBYTE, MAX/(CTB/CBYTE)); - } - - @Override - public long toKB(long duration) { - return x(duration, CTB/CKB, MAX/(CTB/CKB)); - } - - @Override - public long toMB(long duration) { - return x(duration, CTB/CMB, MAX/(CTB/CMB)); - } - - @Override - public long toGB(long duration) { - return x(duration, CTB/CGB, MAX/(CTB/CTB)); - } - - @Override - public long toTB(long duration) { - return duration; - } - } - ; - - private static final long CBYTE = 1; - private static final long CKB = 1024; - private static final long CMB = (CKB * CKB); - private static final long CGB = (CKB * CMB); - private static final long CTB = (CKB * CGB); - - private static final long MAX = Long.MAX_VALUE; - - /** - * Scale d by m, checking for overflow. - * This has a short name to make above code more readable. - */ - private static long x(long d, long m, long over) { - if (d > over) { return Long.MAX_VALUE; } - if (d < -over) { return Long.MIN_VALUE; } - return d * m; - } - - - public long toByte(long duration) { throw new AbstractMethodError(); } - public long toKB(long duration) { throw new AbstractMethodError(); } - public long toMB(long duration) { throw new AbstractMethodError(); } - public long toGB(long duration) { throw new AbstractMethodError(); } - public long toTB(long duration) { throw new AbstractMethodError(); } - - /** - * 文件大小转换为可读带单位的字符串,如1.2GB、300.0MB等 - * @param length 文件大小,java.io.File.length()返回值,单位为Bytes - * @return - */ - public static String readableSizeStr(long length) { - return readableSizeStr(length, FileSizeUnit.BYTE); - } - - /** - * 文件大小转换为可读带单位的字符串,如1.2GB、300MB等 - * @param size 文件大小 - * @param unit 单位 - * @return - */ - public static String readableSizeStr(long size, FileSizeUnit unit) { - if(size <= 0 || unit == null) { - return ""; - } - long byteSize = unit.toByte(size); - String[] fileSizeArr = getFileSize(byteSize); - return fileSizeArr[0]+fileSizeArr[1]; - } - - /** - * 以合适的单位返回文件大小,返回数组,String[0]为文件大小数字,String[1]为单位 - * @param length The length, in bytes - * @return 返回数组,String[0]为文件大小数字,String[1]为单位 - */ - private static String[] getFileSize(long length) { - if(length <= 0) { - return new String[]{"0","Byte"}; - } - - String[] size = new String[2]; - DecimalFormat df = new DecimalFormat("#.0"); - if (length < CKB) { - size[0] = df.format(length); - size[1] = "Byte"; - } else if (length < CMB) { - size[0] = df.format( length*1.0/ CKB); - size[1] = "KB"; - } else if (length < CGB) { - size[0] = df.format(length*1.0/ CMB); - size[1] = "MB"; - } else if (length < CTB) { - size[0] = df.format(length*1.0/ CGB); - size[1] = "GB"; - } else { - size[0] = df.format(length*1.0/ CTB); - size[1] = "TB"; - } - return size; - } - -} diff --git a/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java b/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java deleted file mode 100644 index 3dd4a5bd9..000000000 --- a/hutool-core/src/test/java/cn/hutool/core/io/unit/FileSizeUnitTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package cn.hutool.core.io.unit; - -import org.junit.Assert; -import org.junit.Test; - -/** - * 文件大小单位转换,方便于各种单位之间的转换 - * @author zrh 455741807@qq.com - * @date 2022-07-21 - */ -public class FileSizeUnitTest { - private static final long Byte = 1; - private static final long KB = 1024; - private static final long MB = (KB * KB); - private static final long GB = (KB * MB); - private static final long TB = (KB * GB); - - @Test - public void toByte() { - long byteS = 123, kbS = 1, mbS = 1, gbS = 1, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toByte(byteS); - long kbT = FileSizeUnit.KB.toByte(kbS); - long mbT = FileSizeUnit.MB.toByte(mbS); - long gbT = FileSizeUnit.GB.toByte(gbS); - long tbT = FileSizeUnit.TB.toByte(tbS); - - Assert.assertEquals(byteS, byteT); - Assert.assertEquals(kbS*(KB/Byte), kbT); - Assert.assertEquals(mbS*(MB/Byte), mbT); - Assert.assertEquals(gbS*(GB/Byte), gbT); - Assert.assertEquals(tbS*(TB/Byte), tbT); - - - } - - @Test - public void toKB() { - long byteS = 1230, kbS = 456, mbS = 1, gbS = 1, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toKB(byteS); - long kbT = FileSizeUnit.KB.toKB(kbS); - long mbT = FileSizeUnit.MB.toKB(mbS); - long gbT = FileSizeUnit.GB.toKB(gbS); - long tbT = FileSizeUnit.TB.toKB(tbS); - - Assert.assertEquals(byteS/(KB/Byte), byteT); - Assert.assertEquals(kbS, kbT); - Assert.assertEquals(mbS*(MB/KB), mbT); - Assert.assertEquals(gbS*(GB/KB), gbT); - Assert.assertEquals(tbS*(TB/KB), tbT); - } - - @Test - public void toMB() { - long byteS = 1230000, kbS = 456000, mbS = 789, gbS = 1, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toMB(byteS); - long kbT = FileSizeUnit.KB.toMB(kbS); - long mbT = FileSizeUnit.MB.toMB(mbS); - long gbT = FileSizeUnit.GB.toMB(gbS); - long tbT = FileSizeUnit.TB.toMB(tbS); - - Assert.assertEquals(byteS/(MB/Byte), byteT); - Assert.assertEquals(kbS/(MB/KB), kbT); - Assert.assertEquals(mbS, mbT); - Assert.assertEquals(gbS*(GB/MB), gbT); - Assert.assertEquals(tbS*(TB/MB), tbT); - } - - @Test - public void toGB() { - long byteS = 1230000000, kbS = 456000000, mbS = 789000, gbS = 1, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toGB(byteS); - long kbT = FileSizeUnit.KB.toGB(kbS); - long mbT = FileSizeUnit.MB.toGB(mbS); - long gbT = FileSizeUnit.GB.toGB(gbS); - long tbT = FileSizeUnit.TB.toGB(tbS); - - Assert.assertEquals(byteS/(GB/Byte), byteT); - Assert.assertEquals(kbS/(GB/KB), kbT); - Assert.assertEquals(mbS/(GB/MB), mbT); - Assert.assertEquals(gbS, gbT); - Assert.assertEquals(tbS*(TB/GB), tbT); - - } - - @Test - public void toTB() { - long byteS = 1230000000000L, kbS = 111000000000L, mbS = 789000000L, gbS = 1024, tbS = 1; - - long byteT = FileSizeUnit.BYTE.toTB(byteS); - long kbT = FileSizeUnit.KB.toTB(kbS); - long mbT = FileSizeUnit.MB.toTB(mbS); - long gbT = FileSizeUnit.GB.toTB(gbS); - long tbT = FileSizeUnit.TB.toTB(tbS); - - Assert.assertEquals(byteS/(TB/Byte), byteT); - Assert.assertEquals(kbS/(TB/KB), kbT); - Assert.assertEquals(mbS/(TB/MB), mbT); - Assert.assertEquals(gbS/(TB/GB), gbT); - Assert.assertEquals(tbS, tbT); - } - - @Test - public void readableSizeStr1() { - long size = 1024*1024; - String sizeStr = FileSizeUnit.readableSizeStr(size); - Assert.assertEquals("1.0MB", sizeStr); - } - - @Test - public void readableSizeStr2() { - long size = 123*1024; - String sizeStr = FileSizeUnit.readableSizeStr(size, FileSizeUnit.MB); - Assert.assertEquals("123.0GB", sizeStr); - } - -} From cf84d06ccef3b55fb12334eb5b6a356c8721353c Mon Sep 17 00:00:00 2001 From: zhangrenhua Date: Fri, 22 Jul 2022 15:01:40 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E9=97=B4=E9=9A=94=E6=97=B6=E9=97=B4=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/date/DateUtil.java | 51 +++++++++++++++++++ .../cn/hutool/core/date/DateUtilTest.java | 23 +++++++++ 2 files changed, 74 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java b/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java index 66a04dd25..8721291f7 100755 --- a/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java @@ -635,6 +635,57 @@ public class DateUtil extends CalendarUtil { return CalendarUtil.formatChineseDate(CalendarUtil.calendar(date), withTime); } + + + /** + * 返回可读的时间间隔,如1天20小时3分48秒 + * @param milliSecond 毫秒 + * @author zrh 455741807@qq.com + * @return + */ + public static String formatDuring(long milliSecond) { + final int i1000=1000; + if (milliSecond <= 0) { + return "小于1毫秒"; + } + if (milliSecond < i1000) { + return milliSecond + "毫秒"; + } + String result = ""; + long days = milliSecond / (1000 * 60 * 60 * 24); + long hours = (milliSecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); + long minutes = (milliSecond % (1000 * 60 * 60)) / (1000 * 60); + long seconds = (milliSecond % (1000 * 60)) / 1000; + if (days > 0) { + result += days + "天"; + } + if (hours > 0) { + result += hours + "小时"; + } + if (minutes > 0) { + result += minutes + "分"; + } + if (seconds >= 0) { + result += seconds + "秒"; + } + return result; + } + + /** + * 返回两个日期之间间隔时间 + * @param begin 开始日期 + * @param end 结束日期 + * @author zrh 455741807@qq.com + * @return + */ + public static String formatDuring(Date begin, Date end) { + if(begin == null || end == null) { + return ""; + } + return formatDuring(end.getTime()-begin.getTime()); + } + + // ------------------------------------ Format end ---------------------------------------------- // ------------------------------------ Parse start ---------------------------------------------- diff --git a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java index 3984bf599..c0f1f72c8 100755 --- a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java @@ -97,6 +97,29 @@ public class DateUtilTest { Assert.assertEquals(date, parse); } + + @Test + public void formatDuring() { + long d = 56123456; + String s = DateUtil.formatDuring(d); + Assert.assertEquals("15小时35分23秒", s); + } + + @Test + public void testFormatDuring() { + Calendar c = Calendar.getInstance(); + Date s = c.getTime(); + + c.add(Calendar.DAY_OF_MONTH, 1); + c.add(Calendar.MINUTE, 2); + c.add(Calendar.SECOND, 3); + Date e = c.getTime(); + + String str = DateUtil.formatDuring(s, e); + + Assert.assertEquals("1天2分3秒", str); + } + @Test public void beginAndEndTest() { String dateStr = "2017-03-01 00:33:23";