Skip to content

Commit

Permalink
dt
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Apr 10, 2024
1 parent a5faaf4 commit e787131
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 46 deletions.
124 changes: 83 additions & 41 deletions rxlib/src/main/java/org/rx/bean/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
20 changes: 17 additions & 3 deletions rxlib/src/test/java/org/rx/bean/TestBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions rxlib/src/test/java/org/rx/util/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -44,7 +45,7 @@ default FlagsEnum<BeanMapFlag> getFlags() {
class DateToIntConvert implements BeanMapConverter<Date, Integer> {
@Override
public Integer convert(Date sourceValue, Class<Integer> targetType, String propertyName) {
return (int) (sourceValue.getTime() - DateTime.MIN.getTime());
return (int) (sourceValue.getTime() - DateTime.now().getTime());
}
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e787131

Please sign in to comment.