From e787131c414016e5af0cb418164d8c269a4aae26 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:25:20 +0800 Subject: [PATCH] dt --- rxlib/src/main/java/org/rx/bean/DateTime.java | 124 ++++++++++++------ rxlib/src/test/java/org/rx/bean/TestBean.java | 20 ++- rxlib/src/test/java/org/rx/util/TestUtil.java | 5 +- 3 files changed, 103 insertions(+), 46 deletions(-) diff --git a/rxlib/src/main/java/org/rx/bean/DateTime.java b/rxlib/src/main/java/org/rx/bean/DateTime.java index 8b4bbfde..72ed1739 100644 --- a/rxlib/src/main/java/org/rx/bean/DateTime.java +++ b/rxlib/src/main/java/org/rx/bean/DateTime.java @@ -9,6 +9,9 @@ import java.text.ParseException; import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.Month; +import java.time.temporal.TemporalAdjusters; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; @@ -21,9 +24,9 @@ * GMT: UTC +0 * http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java/ */ +@SuppressWarnings("deprecation") public final class DateTime extends Date { private static final long serialVersionUID = 414744178681347341L; - public static final DateTime MIN = new DateTime(2000, 1, 1, 0, 0, 0), MAX = new DateTime(9999, 12, 31, 0, 0, 0); public static final String DATE_FORMAT = "yyy-MM-dd"; public static final String TIME_FORMAT = "HH:mm:ss"; public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; @@ -92,57 +95,35 @@ private Calendar getCalendar() { return calendar; } - public DateTime getDatePart() { - return DateTime.valueOf(toDateString(), DATE_FORMAT); - } - - public DateTime setDatePart(String date) { - return DateTime.valueOf(toString(date + " HH:mm:ss"), DATE_TIME_FORMAT); - } - - public DateTime getTimePart() { - return DateTime.valueOf(toTimeString(), TIME_FORMAT); - } - - public DateTime setTimePart(String time) { - return DateTime.valueOf(toString("yyy-MM-dd " + time), DATE_TIME_FORMAT); - } - - public boolean isToday() { - return DateTime.now().toString(DATE_FORMAT).equals(toDateString()); - } - - @SuppressWarnings(NON_UNCHECKED) @Override public int getYear() { return getCalendar().get(Calendar.YEAR); } - @SuppressWarnings(NON_UNCHECKED) @Override public int getMonth() { return getCalendar().get(Calendar.MONTH) + 1; } - @SuppressWarnings(NON_UNCHECKED) + public Month getMonthEnum() { + return Month.of(getCalendar().get(Calendar.MONTH) + 1); + } + @Override public int getDay() { return getCalendar().get(Calendar.DAY_OF_MONTH); } - @SuppressWarnings(NON_UNCHECKED) @Override public int getHours() { return getCalendar().get(Calendar.HOUR_OF_DAY); } - @SuppressWarnings(NON_UNCHECKED) @Override public int getMinutes() { return getCalendar().get(Calendar.MINUTE); } - @SuppressWarnings(NON_UNCHECKED) @Override public int getSeconds() { return getCalendar().get(Calendar.SECOND); @@ -157,19 +138,35 @@ public int getDayOfYear() { } public DayOfWeek getDayOfWeek() { - return DayOfWeek.of(getCalendar().get(Calendar.DAY_OF_WEEK)); + switch (getCalendar().get(Calendar.DAY_OF_WEEK)) { + case Calendar.MONDAY: + return DayOfWeek.MONDAY; + case Calendar.TUESDAY: + return DayOfWeek.TUESDAY; + case Calendar.WEDNESDAY: + return DayOfWeek.WEDNESDAY; + case Calendar.THURSDAY: + return DayOfWeek.THURSDAY; + case Calendar.FRIDAY: + return DayOfWeek.FRIDAY; + case Calendar.SATURDAY: + return DayOfWeek.SATURDAY; +// case Calendar.SUNDAY: + default: + return DayOfWeek.SUNDAY; + } } public double getTotalDays() { - return super.getTime() / (24d * 60 * 60 * 1000); + return super.getTime() / (24 * 60 * 60 * 1000d); } public double getTotalHours() { - return super.getTime() / (60d * 60 * 1000); + return super.getTime() / (60 * 60 * 1000d); } public double getTotalMinutes() { - return super.getTime() / (60d * 1000); + return super.getTime() / (60 * 1000d); } public double getTotalSeconds() { @@ -180,17 +177,12 @@ public double getTotalMilliseconds() { return super.getTime(); } - @SuppressWarnings(NON_UNCHECKED) - public DateTime(int year, int month, int day, int hour, int minute, int second) { + public DateTime(int year, Month month, int day, int hour, int minute, int second) { Calendar c = getCalendar(); - c.set(year, month - 1, day, hour, minute, second); + c.set(year, month.getValue() - 1, day, hour, minute, second); super.setTime(c.getTimeInMillis()); } - public DateTime(@NonNull Date date) { - super(date.getTime()); - } - public DateTime(long ticks) { super(ticks); } @@ -203,6 +195,35 @@ public void setTime(long time) { } } + public DateTime getDatePart() { + return new DateTime(getYear(), getMonthEnum(), getDay(), 0, 0, 0); + } + + public DateTime setDatePart(int year, Month month, int day) { + return new DateTime(year, month, day, getHours(), getMinutes(), getSeconds()); + } + + public DateTime setDatePart(String date) { + return DateTime.valueOf(toString(date + " HH:mm:ss"), DATE_TIME_FORMAT); + } + + public DateTime getTimePart() { + return new DateTime(1970, Month.JANUARY, 1, getHours(), getMinutes(), getSeconds()); + } + + public DateTime setTimePart(int hour, int minute, int second) { + return new DateTime(getYear(), getMonthEnum(), getDay(), hour, minute, second); + } + + public DateTime setTimePart(String time) { + return DateTime.valueOf(toString("yyy-MM-dd " + time), DATE_TIME_FORMAT); + } + + public boolean isToday() { + DateTime n = DateTime.now(); + return n.getYear() == getYear() && n.getMonth() == getMonth() && n.getDay() == getDay(); + } + public DateTime addYears(int value) { return add(Calendar.YEAR, value); } @@ -231,6 +252,18 @@ public DateTime addMilliseconds(int value) { return add(Calendar.MILLISECOND, value); } + public DateTime nextDayOfWeek() { +// System.out.println("n:" + LocalDate.parse(toDateString()).with(TemporalAdjusters.next(getDayOfWeek()))); +// int dayOfWeek = getCalendar().get(Calendar.DAY_OF_WEEK); +// return addDays((9 - dayOfWeek) % 7); + return addDays(7); + } + + public DateTime lastDayOfMonth() { +// System.out.println("n:" + LocalDate.parse(toDateString()).with(TemporalAdjusters.lastDayOfMonth())); + return set(Calendar.DAY_OF_MONTH, getCalendar().getActualMaximum(Calendar.DAY_OF_MONTH)); + } + private DateTime add(int field, int value) { Calendar c = getCalendar(); long mark = c.getTimeInMillis(); @@ -244,12 +277,21 @@ private DateTime add(int field, int value) { } } - public DateTime addTicks(long ticks) { - return new DateTime(super.getTime() + ticks); + private DateTime set(int field, int value) { + Calendar c = getCalendar(); + long mark = c.getTimeInMillis(); + c.set(field, value); + try { + DateTime newVal = new DateTime(c.getTimeInMillis()); + newVal.getCalendar().setTimeZone(getCalendar().getTimeZone()); + return newVal; + } finally { + c.setTimeInMillis(mark); + } } public DateTime add(@NonNull Date value) { - return addTicks(value.getTime()); + return new DateTime(super.getTime() + value.getTime()); } public DateTime subtract(@NonNull Date value) { diff --git a/rxlib/src/test/java/org/rx/bean/TestBean.java b/rxlib/src/test/java/org/rx/bean/TestBean.java index a7f8abcd..f7922b5f 100644 --- a/rxlib/src/test/java/org/rx/bean/TestBean.java +++ b/rxlib/src/test/java/org/rx/bean/TestBean.java @@ -11,6 +11,8 @@ import org.rx.test.PersonBean; import org.rx.test.PersonGender; +import java.time.DayOfWeek; +import java.time.Month; import java.util.HashSet; import java.util.Set; import java.util.concurrent.CountDownLatch; @@ -180,8 +182,8 @@ public void decimal() { public void dateTime() { DateTime now = DateTime.now(); DateTime utc = DateTime.utcNow(); - DateTime d = new DateTime(2010, 8, 24, 11, 12, 13); - DateTime d3 = new DateTime(2010, 8, 23, 11, 12, 13); + DateTime d = new DateTime(2010, Month.AUGUST, 24, 11, 12, 13); + DateTime d3 = new DateTime(2010, Month.AUGUST, 23, 11, 12, 13); assert now.getTime() == utc.getTime(); assert d.getYear() == 2010; @@ -197,8 +199,20 @@ public void dateTime() { System.out.println(ts); System.out.println(d); String sts = String.valueOf(ts); - assert d.toString().equals("2010-08-24 11:12:13," + sts.substring(sts.length() - 3) + "+0800"); + System.out.println(d.toString(DateTime.ISO_DATE_TIME_FORMAT)); + assert d.toString(DateTime.ISO_DATE_TIME_FORMAT).equals("2010-08-24T11:12:13." + sts.substring(sts.length() - 3) + "+0800"); + assert d.getTimePart().toDateTimeString().equals("1970-01-01 11:12:13"); assert d.setTimePart("14:30:01").toDateTimeString().equals("2010-08-24 14:30:01"); + assert d.setTimePart(14, 30, 1).toDateTimeString().equals("2010-08-24 14:30:01"); + assert d.getDatePart().toDateTimeString().equals("2010-08-24 00:00:00"); + assert d.setDatePart("2022-02-02").toDateTimeString().equals("2022-02-02 11:12:13"); + assert d.setDatePart(2022, Month.FEBRUARY, 2).toDateTimeString().equals("2022-02-02 11:12:13"); + + DateTime nd = DateTime.valueOf("2024-04-10 00:20:00"); + assert nd.getDayOfWeek() == DayOfWeek.WEDNESDAY; + log.info("{} nextDayOfWeek {}", nd, nd.nextDayOfWeek()); + log.info("{} lastDayOfMonth {}", nd, nd.lastDayOfMonth()); + log.info("{} isToday {}", nd, nd.isToday()); } @Test diff --git a/rxlib/src/test/java/org/rx/util/TestUtil.java b/rxlib/src/test/java/org/rx/util/TestUtil.java index ab8cedb6..bce1fcde 100644 --- a/rxlib/src/test/java/org/rx/util/TestUtil.java +++ b/rxlib/src/test/java/org/rx/util/TestUtil.java @@ -23,6 +23,7 @@ import org.rx.third.guava.CaseFormat; import java.lang.reflect.Method; +import java.time.Month; import java.util.Arrays; import java.util.*; @@ -44,7 +45,7 @@ default FlagsEnum getFlags() { class DateToIntConvert implements BeanMapConverter { @Override public Integer convert(Date sourceValue, Class targetType, String propertyName) { - return (int) (sourceValue.getTime() - DateTime.MIN.getTime()); + return (int) (sourceValue.getTime() - DateTime.now().getTime()); } } @@ -89,7 +90,7 @@ public void normalMapBean() { f.setIndex(2); f.setName(str_name_wyf); f.setAge(6); - f.setBirth(new DateTime(2020, 2, 20, 0, 0, 0)); + f.setBirth(new DateTime(2020, Month.FEBRUARY, 20, 0, 0, 0)); f.setGender(PersonGender.BOY); f.setCashCent(200L); GirlBean t = new GirlBean();