Commit acbd965d authored by yanzg's avatar yanzg

身份证识别

parent cb7bc202
...@@ -1093,6 +1093,82 @@ public class DateHelper { ...@@ -1093,6 +1093,82 @@ public class DateHelper {
return ret; return ret;
} }
/**
* 获取结束时间
*
* @param current 当前时间
* @param fieldValueFormat 结束时间呈现格式
* @return 返回值
*/
public static String getToTime(String current, String fieldValueFormat) {
return getToTime(DateHelper.getDateTime(current), fieldValueFormat);
}
/**
* 获取结束时间
*
* @param current 当前时间
* @param fieldValueFormat 结束时间呈现格式
* @return 返回值
*/
public static String getToTime(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("-12-31 23:59:59");
return sbTo.toString();
}
if (dateFormat) {
sbTo.append("-").append(DateHelper.getDateTimeString("dd", current));
} else {
// 获取月份最后一天
Date monthEnd = DateHelper.getMonthEnd(DateHelper.getDateTime(sbTo + "-01"));
sbTo.append("-")
.append(DateHelper.getDateTimeString("dd", monthEnd))
.append(" 23:59:59");
return sbTo.toString();
}
if (hourFormat) {
sbTo.append(" ").append(DateHelper.getDateTimeString("HH", current));
} else {
// 获取一天中最后1小时
sbTo.append(" 23:59:59");
return sbTo.toString();
}
if (minuteFormat) {
sbTo.append(":").append(DateHelper.getDateTimeString("mm", current));
} else {
// 获取一小时中最后一分钟
sbTo.append(":59:59");
return sbTo.toString();
}
if (secondFormat) {
sbTo.append(":").append(DateHelper.getDateTimeString("ss", current));
} else {
// 获取一分钟中最后一秒
sbTo.append(":59");
}
return sbTo.toString();
}
/** /**
* 每日日期拆分 * 每日日期拆分
*/ */
......
...@@ -76,9 +76,116 @@ public class TestDateHelper { ...@@ -76,9 +76,116 @@ public class TestDateHelper {
@Test @Test
public void testDayMonth() { public void testDayMonth() {
Date dt = DateHelper.getDateTime("2023-12-12 15:59:59"); Date dt = DateHelper.getDateTime("2023-12-12 15:59:59");
Assert.assertEquals(2023,DateHelper.getYear(dt)); Assert.assertEquals(2023, DateHelper.getYear(dt));
Assert.assertEquals(12,DateHelper.getMonth(dt)); Assert.assertEquals(12, DateHelper.getMonth(dt));
Assert.assertEquals(12,DateHelper.getDay(dt)); Assert.assertEquals(12, DateHelper.getDay(dt));
Assert.assertEquals(15,DateHelper.getHour(dt)); Assert.assertEquals(15, DateHelper.getHour(dt));
}
@Test
public void getEndDate() {
// 同一分钟的最后时间
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:59", "yyyy-MM-dd HH:mm:ss"),
"2023-12-12 15:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 23:59:01", "yyyy-MM-dd HH:mm:ss"),
"2023-12-12 23:59:01"
);
// 同一分钟的最后时间
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:59", "yyyy-MM-dd HH:mm:00"),
"2023-12-12 15:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 23:59:01", "yyyy-MM-dd HH:mm:00"),
"2023-12-12 23:59:59"
);
// 同一小时的最后时间
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:03:59", "yyyy-MM-dd HH:00:00"),
"2023-12-12 15:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:01", "yyyy-MM-dd HH:00:00"),
"2023-12-12 15:59:59"
);
// 同一天的最后时间
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 12:03:59", "yyyy-MM-dd 00:00:00"),
"2023-12-12 23:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:01", "yyyy-MM-dd 00:00:00"),
"2023-12-12 23:59:59"
);
// 同一月的最后时间
Assert.assertEquals(
DateHelper.getToTime("2023-1-15 12:03:59", "yyyy-MM-01 00:00:00"),
"2023-01-31 23:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-02-12 15:59:01", "yyyy-MM-01 00:00:00"),
"2023-02-28 23:59:59"
);
// 同一年的最后时间
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:59", "yyyy-01-01 00:00:00"),
"2023-12-31 23:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-01-12 15:59:59", "yyyy-01-01 00:00:00"),
"2023-12-31 23:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-01-12 15:59:59", "yyyy-01-01 00:00:00"),
"2023-12-31 23:59:59"
);
// 同一小时的最后秒,不计算分钟
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:03:59", "yyyy-MM-dd HH:00:ss"),
"2023-12-12 15:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:01", "yyyy-MM-dd HH:00:ss"),
"2023-12-12 15:59:59"
);
// 同一月中的最后小时
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:03:59", "yyyy-MM-00 HH:00:00"),
"2023-12-31 23:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:01", "yyyy-MM-00 HH:00:00"),
"2023-12-31 23:59:59"
);
// 同一年中的最后小时
Assert.assertEquals(
DateHelper.getToTime("2023-05-12 15:03:59", "yyyy-01-01 HH:00:00"),
"2023-12-31 23:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:01", "yyyy-01-01 HH:00:00"),
"2023-12-31 23:59:59"
);
// 同一年中的最后日
Assert.assertEquals(
DateHelper.getToTime("2023-05-12 15:03:59", "yyyy-01-dd 00:00:00"),
"2023-12-31 23:59:59"
);
Assert.assertEquals(
DateHelper.getToTime("2023-12-12 15:59:01", "yyyy-01-dd 00:00:00"),
"2023-12-31 23:59:59"
);
} }
} }
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