提交 15acccbd authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add DateTimeUtils.valueToCalendar()

上级 952aaf18
...@@ -317,6 +317,20 @@ public class DateTimeUtils { ...@@ -317,6 +317,20 @@ public class DateTimeUtils {
return ValueTimestamp.fromDateValueAndNanos(dateValue, nanos); return ValueTimestamp.fromDateValueAndNanos(dateValue, nanos);
} }
private static Calendar valueToCalendar(Value value) {
Calendar cal = DateTimeUtils.createGregorianCalendar();
if (value instanceof ValueTimestamp) {
cal.setTime(value.getTimestamp());
} else if (value instanceof ValueDate) {
cal.setTime(value.getDate());
} else if (value instanceof ValueTime) {
cal.setTime(value.getTime());
} else {
cal.setTime(value.getTimestamp());
}
return cal;
}
/** /**
* Parse a date string. The format is: [+|-]year-month-day * Parse a date string. The format is: [+|-]year-month-day
* *
...@@ -499,9 +513,7 @@ public class DateTimeUtils { ...@@ -499,9 +513,7 @@ public class DateTimeUtils {
* @return the value * @return the value
*/ */
public static int getDatePart(Value date, int field) { public static int getDatePart(Value date, int field) {
java.util.Date d = date.getTimestamp(); Calendar c = valueToCalendar(date);
Calendar c = getCalendar();
c.setTime(d);
if (field == Calendar.YEAR) { if (field == Calendar.YEAR) {
return getYear(c); return getYear(c);
} }
...@@ -557,10 +569,7 @@ public class DateTimeUtils { ...@@ -557,10 +569,7 @@ public class DateTimeUtils {
* @return the day of the week, Monday as 1 to Sunday as 7 * @return the day of the week, Monday as 1 to Sunday as 7
*/ */
public static int getIsoDayOfWeek(Value value) { public static int getIsoDayOfWeek(Value value) {
java.util.Date date = value.getDate(); int val = valueToCalendar(value).get(Calendar.DAY_OF_WEEK) - 1;
Calendar cal = DateTimeUtils.createGregorianCalendar();
cal.setTimeInMillis(date.getTime());
int val = cal.get(Calendar.DAY_OF_WEEK) - 1;
return val == 0 ? 7 : val; return val == 0 ? 7 : val;
} }
...@@ -579,9 +588,7 @@ public class DateTimeUtils { ...@@ -579,9 +588,7 @@ public class DateTimeUtils {
* @return the week of the year * @return the week of the year
*/ */
public static int getIsoWeek(Value value) { public static int getIsoWeek(Value value) {
java.util.Date date = value.getDate(); Calendar c = valueToCalendar(value);
Calendar c = DateTimeUtils.createGregorianCalendar();
c.setTimeInMillis(date.getTime());
c.setFirstDayOfWeek(Calendar.MONDAY); c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(4); c.setMinimalDaysInFirstWeek(4);
return c.get(Calendar.WEEK_OF_YEAR); return c.get(Calendar.WEEK_OF_YEAR);
...@@ -595,9 +602,7 @@ public class DateTimeUtils { ...@@ -595,9 +602,7 @@ public class DateTimeUtils {
* @return the year * @return the year
*/ */
public static int getIsoYear(Value value) { public static int getIsoYear(Value value) {
java.util.Date date = value.getDate(); Calendar cal = valueToCalendar(value);
Calendar cal = DateTimeUtils.createGregorianCalendar();
cal.setTimeInMillis(date.getTime());
cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4); cal.setMinimalDaysInFirstWeek(4);
int year = getYear(cal); int year = getYear(cal);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论