Commit 665c8b18 authored by yanzg's avatar yanzg

身份证识别

parent acbd965d
...@@ -1169,6 +1169,79 @@ public class DateHelper { ...@@ -1169,6 +1169,79 @@ public class DateHelper {
return sbTo.toString(); return sbTo.toString();
} }
/**
* 获取开始时间
*
* @param current 当前时间
* @param fieldValueFormat 结束时间呈现格式
* @return 返回值
*/
public static String getFromTime(String current, String fieldValueFormat) {
return getFromTime(DateHelper.getDateTime(current), fieldValueFormat);
}
/**
* 获取开始时间
*
* @param current 当前时间
* @param fieldValueFormat 结束时间呈现格式
* @return 返回值
*/
public static String getFromTime(Date current, String fieldValueFormat) {
boolean yearFormat = fieldValueFormat.contains("yyyy");
boolean monthFormat = fieldValueFormat.contains("MM");
boolean dateFormat = fieldValueFormat.contains("dd");
boolean hourFormat = fieldValueFormat.contains("HH");
boolean minuteFormat = fieldValueFormat.contains("mm");
boolean secondFormat = fieldValueFormat.contains("ss");
StringBuilder sbTo = new StringBuilder();
if (yearFormat) {
sbTo.append(DateHelper.getDateTimeString("yyyy", current));
} else {
return StringHelper.EMPTY;
}
if (monthFormat) {
sbTo.append("-").append(DateHelper.getDateTimeString("MM", current));
} else {
// 获取年份最后一月
sbTo.append("-01-01 00:00:00");
return sbTo.toString();
}
if (dateFormat) {
sbTo.append("-").append(DateHelper.getDateTimeString("dd", current));
} else {
// 获取月份最后一天
sbTo.append("-01 00:00:00");
return sbTo.toString();
}
if (hourFormat) {
sbTo.append(" ").append(DateHelper.getDateTimeString("HH", current));
} else {
// 获取一天中最后1小时
sbTo.append(" 00:00:00");
return sbTo.toString();
}
if (minuteFormat) {
sbTo.append(":").append(DateHelper.getDateTimeString("mm", current));
} else {
// 获取一小时中最后一分钟
sbTo.append(":00:00");
return sbTo.toString();
}
if (secondFormat) {
sbTo.append(":").append(DateHelper.getDateTimeString("ss", current));
} else {
// 获取一分钟中最后一秒
sbTo.append(":00");
}
return sbTo.toString();
}
/** /**
* 每日日期拆分 * 每日日期拆分
*/ */
......
...@@ -160,11 +160,11 @@ public class TestDateHelper { ...@@ -160,11 +160,11 @@ public class TestDateHelper {
// 同一月中的最后小时 // 同一月中的最后小时
Assert.assertEquals( Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:03:59", "yyyy-MM-00 HH:00:00"), DateHelper.getToTime("2023-12-12 15:03:59", "yyyy-MM-01 HH:00:00"),
"2023-12-31 23:59:59" "2023-12-31 23:59:59"
); );
Assert.assertEquals( Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:01", "yyyy-MM-00 HH:00:00"), DateHelper.getToTime("2023-12-12 15:59:01", "yyyy-MM-01 HH:00:00"),
"2023-12-31 23:59:59" "2023-12-31 23:59:59"
); );
...@@ -188,4 +188,112 @@ public class TestDateHelper { ...@@ -188,4 +188,112 @@ public class TestDateHelper {
"2023-12-31 23:59:59" "2023-12-31 23:59:59"
); );
} }
@Test
public void getFromDate() {
// 同一分钟的最后时间
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:59", "yyyy-MM-dd HH:mm:ss"),
"2023-12-12 15:59:59"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 23:59:01", "yyyy-MM-dd HH:mm:ss"),
"2023-12-12 23:59:01"
);
// 同一分钟的最后时间
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:59", "yyyy-MM-dd HH:mm:00"),
"2023-12-12 15:59:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 23:59:01", "yyyy-MM-dd HH:mm:00"),
"2023-12-12 23:59:00"
);
// 同一小时的最后时间
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:03:59", "yyyy-MM-dd HH:00:00"),
"2023-12-12 15:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:01", "yyyy-MM-dd HH:00:00"),
"2023-12-12 15:00:00"
);
// 同一天的最后时间
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 12:03:59", "yyyy-MM-dd 00:00:00"),
"2023-12-12 00:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:01", "yyyy-MM-dd 00:00:00"),
"2023-12-12 00:00:00"
);
// 同一月的最后时间
Assert.assertEquals(
DateHelper.getFromTime("2023-1-15 12:03:59", "yyyy-MM-01 00:00:00"),
"2023-01-01 00:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-02-12 15:59:01", "yyyy-MM-01 00:00:00"),
"2023-02-01 00:00:00"
);
// 同一年的最后时间
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:59", "yyyy-01-01 00:00:00"),
"2023-01-01 00:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-01-12 15:59:59", "yyyy-01-01 00:00:00"),
"2023-01-01 00:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-01-12 15:59:59", "yyyy-01-01 00:00:00"),
"2023-01-01 00:00:00"
);
// 同一小时的最后秒,不计算分钟
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:03:59", "yyyy-MM-dd HH:00:ss"),
"2023-12-12 15:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:01", "yyyy-MM-dd HH:00:ss"),
"2023-12-12 15:00:00"
);
// 同一月中的最后小时
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:03:59", "yyyy-MM-01 HH:00:00"),
"2023-12-01 00:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:01", "yyyy-MM-01 HH:00:00"),
"2023-12-01 00:00:00"
);
// 同一年中的最后小时
Assert.assertEquals(
DateHelper.getFromTime("2023-05-12 15:03:59", "yyyy-01-01 HH:00:00"),
"2023-01-01 00:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:01", "yyyy-01-01 HH:00:00"),
"2023-12-31 00:00:00"
);
// 同一年中的最后日
Assert.assertEquals(
DateHelper.getFromTime("2023-05-12 15:03:59", "yyyy-01-dd 00:00:00"),
"2023-01-01 00:00:00"
);
Assert.assertEquals(
DateHelper.getFromTime("2023-12-12 15:59:01", "yyyy-01-dd 00:00:00"),
"2023-01-01 00:00:00"
);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment