Skip to content

Commit dbb544e

Browse files
committed
add date and time parser into Util
1 parent add5dbe commit dbb544e

2 files changed

Lines changed: 131 additions & 9 deletions

File tree

modules/library/src/main/java/top/mrxiaom/pluginbase/utils/CollectionUtils.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
import java.util.regex.Pattern;
1414

1515
public class CollectionUtils {
16-
public static List<String> split(String string, String spliter) {
17-
return split(string, spliter, 0);
16+
public static List<String> split(String string, String splitter) {
17+
return split(string, splitter, 0);
1818
}
1919

20-
public static List<String> split(String string, String spliter, int limit) {
20+
public static List<String> split(String string, String splitter, int limit) {
2121
int oldIndex = 0;
22-
int length = spliter.length();
22+
int length = splitter.length();
2323
if (length == 0) throw new IllegalArgumentException("spliter can't be empty!");
2424
int count = limit - 1;
2525
boolean unlimited = limit <= 0;
2626
List<String> list = new ArrayList<>();
2727
while (unlimited || list.size() < count) {
28-
int i = string.indexOf(spliter, oldIndex);
28+
int i = string.indexOf(splitter, oldIndex);
2929
if (i >= 0) {
3030
if (oldIndex == i) {
3131
list.add("");
@@ -41,17 +41,17 @@ public static List<String> split(String string, String spliter, int limit) {
4141
return list;
4242
}
4343

44-
public static List<String> split(String string, char spliter) {
45-
return split(string, spliter, 0);
44+
public static List<String> split(String string, char splitter) {
45+
return split(string, splitter, 0);
4646
}
4747

48-
public static List<String> split(String string, char spliter, int limit) {
48+
public static List<String> split(String string, char splitter, int limit) {
4949
int oldIndex = 0;
5050
int count = limit - 1;
5151
boolean unlimited = limit <= 0;
5252
List<String> list = new ArrayList<>();
5353
while (unlimited || list.size() < count) {
54-
int i = string.indexOf(spliter, oldIndex);
54+
int i = string.indexOf(splitter, oldIndex);
5555
if (i >= 0) {
5656
if (oldIndex == i) {
5757
list.add("");

modules/library/src/main/java/top/mrxiaom/pluginbase/utils/Util.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
import java.net.URL;
2525
import java.nio.file.Path;
2626
import java.nio.file.Paths;
27+
import java.time.LocalDate;
28+
import java.time.LocalDateTime;
29+
import java.time.LocalTime;
30+
import java.time.chrono.IsoChronology;
31+
import java.time.temporal.ChronoField;
2732
import java.util.*;
2833
import java.util.function.BiConsumer;
2934
import java.util.jar.JarEntry;
@@ -338,6 +343,123 @@ public static Optional<Long> parseLong(String s) {
338343
}
339344
}
340345

346+
/**
347+
* 解析字符串为时间
348+
*/
349+
public static Optional<LocalTime> parseTime(String s) {
350+
return parseTime(s, ':');
351+
}
352+
353+
/**
354+
* 解析字符串为时间
355+
* @param splitter 各单位的分隔符,建议使用 <code>:</code>
356+
*/
357+
public static Optional<LocalTime> parseTime(String s, char splitter) {
358+
List<String> split = CollectionUtils.split(s, splitter, 3);
359+
if (split.size() == 2) {
360+
int hour = Util.parseInt(split.get(0)).orElse(-1);
361+
int minute = Util.parseInt(split.get(1)).orElse(-1);
362+
if (ChronoField.HOUR_OF_DAY.range().isValidValue(hour)
363+
&& ChronoField.MINUTE_OF_HOUR.range().isValidValue(minute)
364+
) {
365+
return Optional.of(LocalTime.of(hour, minute));
366+
}
367+
}
368+
if (split.size() == 3) {
369+
int hour = Util.parseInt(split.get(0)).orElse(-1);
370+
int minute = Util.parseInt(split.get(1)).orElse(-1);
371+
int second = Util.parseInt(split.get(2)).orElse(-1);
372+
if (ChronoField.HOUR_OF_DAY.range().isValidValue(hour)
373+
&& ChronoField.MINUTE_OF_HOUR.range().isValidValue(minute)
374+
&& ChronoField.SECOND_OF_MINUTE.range().isValidValue(second)
375+
) {
376+
return Optional.of(LocalTime.of(hour, minute));
377+
}
378+
}
379+
return Optional.empty();
380+
}
381+
382+
/**
383+
* 解析字符串为日期
384+
*/
385+
public static Optional<LocalDate> parseDate(String s) {
386+
return parseDate(s, '-');
387+
}
388+
389+
/**
390+
* 解析字符串为日期
391+
* @param splitter 各单位的分隔符,建议使用 <code>-</code>
392+
*/
393+
public static Optional<LocalDate> parseDate(String s, char splitter) {
394+
List<String> split = CollectionUtils.split(s, splitter, 3);
395+
if (split.size() == 3) {
396+
int year = Util.parseInt(split.get(0)).orElse(-1);
397+
int month = Util.parseInt(split.get(1)).orElse(-1);
398+
int dayOfMonth = Util.parseInt(split.get(2)).orElse(-1);
399+
if (ChronoField.YEAR_OF_ERA.range().isValidValue(year)
400+
&& ChronoField.MONTH_OF_YEAR.range().isValidValue(month)
401+
&& ChronoField.DAY_OF_MONTH.range().isValidValue(dayOfMonth)
402+
) {
403+
int dom;
404+
switch (month) {
405+
case 2:
406+
dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
407+
break;
408+
case 4:
409+
case 6:
410+
case 9:
411+
case 11:
412+
dom = 30;
413+
break;
414+
default:
415+
dom = 31;
416+
break;
417+
}
418+
if (dom <= dayOfMonth) {
419+
return Optional.of(LocalDate.of(year, month, dayOfMonth));
420+
}
421+
}
422+
}
423+
return Optional.empty();
424+
}
425+
426+
/**
427+
* 解析字符串为日期与时间
428+
*/
429+
public static Optional<LocalDateTime> parseDateTime(String s) {
430+
return parseDateTime(s, ' ');
431+
}
432+
433+
/**
434+
* 解析字符串为日期与时间
435+
* @param splitter 日期与时间之间的分隔符,建议使用 <code> </code> (空格)
436+
*/
437+
public static Optional<LocalDateTime> parseDateTime(String s, char splitter) {
438+
return parseDateTime(s, splitter, '-', ':');
439+
}
440+
441+
/**
442+
* 解析字符串为日期与时间
443+
* @param splitter 日期与时间之间的分隔符,建议使用 <code> </code> (空格)
444+
* @param dateSplitter 日期各单位的分隔符
445+
* @param timeSplitter 时间各单位的分隔符
446+
* @see Util#parseDate(String, char)
447+
* @see Util#parseTime(String, char)
448+
*/
449+
public static Optional<LocalDateTime> parseDateTime(String s, char splitter, char dateSplitter, char timeSplitter) {
450+
int index = s.indexOf(splitter);
451+
if (index > 5) {
452+
String dateStr = s.substring(0, index);
453+
String timeStr = s.substring(index + 1);
454+
LocalDate date = parseDate(dateStr, dateSplitter).orElse(null);
455+
LocalTime time = parseTime(timeStr, timeSplitter).orElse(null);
456+
if (date != null && time != null) {
457+
return Optional.of(date.atTime(time));
458+
}
459+
}
460+
return Optional.empty();
461+
}
462+
341463
/**
342464
* 通过 enum values 或者 Bukkit Registry 读取值
343465
* @param type 类型

0 commit comments

Comments
 (0)