forked from plusone/plusone-commons
Compare commits
No commits in common. "33e1c147558577e03ea2ea80deaad4f231ad6721" and "275a156184be84e3068f348db5785d2b50eedebc" have entirely different histories.
33e1c14755
...
275a156184
@ -18,8 +18,6 @@ package xyz.zhouxy.plusone.commons.sql;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
|
||||||
|
|
||||||
public class JdbcSql extends SQL<JdbcSql> {
|
public class JdbcSql extends SQL<JdbcSql> {
|
||||||
|
|
||||||
JdbcSql() {
|
JdbcSql() {
|
||||||
@ -35,38 +33,40 @@ public class JdbcSql extends SQL<JdbcSql> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String IN(String col, Collection<?> c) { // NOSONAR
|
public static String IN(String col, Collection<?> c) {
|
||||||
return IN(col, c.size());
|
return IN(col, c.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> String IN(String col, T[] c) { // NOSONAR
|
public static <T> String IN(String col, T[] c) {
|
||||||
return IN(col, c.length);
|
return IN(col, c.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String IN(String col, int length) { // NOSONAR
|
private static String IN(String col, int length) {
|
||||||
if (length == 0) {
|
return col + " IN (" + String.valueOf(buildQuestionsList(length)) + ')';
|
||||||
return "false";
|
|
||||||
}
|
|
||||||
return col + " IN (" + buildQuestionsList(length) + ')';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String NOT_IN(String col, Collection<?> c) { // NOSONAR
|
public static String NOT_IN(String col, Collection<?> c) {
|
||||||
return NOT_IN(col, c.size());
|
return NOT_IN(col, c.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> String NOT_IN(String col, T[] c) { // NOSONAR
|
public static <T> String NOT_IN(String col, T[] c) {
|
||||||
return NOT_IN(col, c.length);
|
return NOT_IN(col, c.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String NOT_IN(String col, int length) { // NOSONAR
|
private static String NOT_IN(String col, int length) {
|
||||||
if (length == 0) {
|
return col + " NOT IN (" + String.valueOf(buildQuestionsList(length)) + ')';
|
||||||
return "true";
|
|
||||||
}
|
|
||||||
return col + " NOT IN (" + buildQuestionsList(length) + ')';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String buildQuestionsList(int times) {
|
private static char[] buildQuestionsList(int times) {
|
||||||
final int length = times <= 0 ? 0 : (times * 3 - 2);
|
char[] arr = new char[times * 3 - 2];
|
||||||
return StringTools.repeat("?, ", times, length);
|
int i = 0;
|
||||||
|
for (int t = 1; t <= times; t++) {
|
||||||
|
arr[i++] = '?';
|
||||||
|
if (t < times) {
|
||||||
|
arr[i++] = ',';
|
||||||
|
arr[i++] = ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,11 +41,11 @@ public class MyBatisSql extends SQL<MyBatisSql> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String IN(String col, String paramName) { // NOSONAR
|
public static String IN(String col, String paramName) {
|
||||||
return " " + col + " IN" + buildForeach(col, paramName);
|
return " " + col + " IN" + buildForeach(col, paramName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String NOT_IN(String col, String paramName) { // NOSONAR
|
public static String NOT_IN(String col, String paramName) {
|
||||||
return col + " NOT IN" + buildForeach(col, paramName);
|
return col + " NOT IN" + buildForeach(col, paramName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,14 +34,14 @@ public abstract class SQL<T> extends AbstractSQL<T> {
|
|||||||
return new MyBatisSql(withScript);
|
return new MyBatisSql(withScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T WHERE(boolean condition, String sqlCondition) { // NOSONAR
|
public T WHERE(boolean condition, String sqlCondition) {
|
||||||
if (condition) {
|
if (condition) {
|
||||||
return WHERE(sqlCondition);
|
return WHERE(sqlCondition);
|
||||||
}
|
}
|
||||||
return getSelf();
|
return getSelf();
|
||||||
}
|
}
|
||||||
|
|
||||||
public T WHERE(boolean condition, String ifSqlCondition, String elseSqlCondition) { // NOSONAR
|
public T WHERE(boolean condition, String ifSqlCondition, String elseSqlCondition) {
|
||||||
return WHERE(condition ? ifSqlCondition : elseSqlCondition);
|
return WHERE(condition ? ifSqlCondition : elseSqlCondition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,17 +21,19 @@ import java.time.LocalDate;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
import java.time.Year;
|
|
||||||
import java.time.YearMonth;
|
import java.time.YearMonth;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.chrono.IsoChronology;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
import javax.annotation.Nonnull;
|
||||||
import com.google.common.collect.Range;
|
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
import com.google.common.cache.CacheLoader;
|
||||||
|
import com.google.common.cache.LoadingCache;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||||
import xyz.zhouxy.plusone.commons.time.YearQuarter;
|
import xyz.zhouxy.plusone.commons.time.YearQuarter;
|
||||||
@ -43,32 +45,89 @@ import xyz.zhouxy.plusone.commons.time.YearQuarter;
|
|||||||
*/
|
*/
|
||||||
public class DateTimeTools {
|
public class DateTimeTools {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存时间格式化器
|
||||||
|
*/
|
||||||
|
private static final LoadingCache<String, DateTimeFormatter> DATE_TIME_FORMATTER_CACHE = CacheBuilder.newBuilder()
|
||||||
|
.maximumSize(20)
|
||||||
|
.build(new CacheLoader<String, DateTimeFormatter>() {
|
||||||
|
@Override
|
||||||
|
public DateTimeFormatter load(@Nonnull String pattern) throws Exception {
|
||||||
|
return DateTimeFormatter.ofPattern(pattern);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取时间格式化器
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @return 时间格式化器
|
||||||
|
*/
|
||||||
|
public static DateTimeFormatter getDateTimeFormatter(String pattern) {
|
||||||
|
return DATE_TIME_FORMATTER_CACHE.getUnchecked(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
// #region - toString
|
// #region - toString
|
||||||
|
|
||||||
public static String toYearString(int year) {
|
/**
|
||||||
return Integer.toString(year);
|
* 将日期时间转换为指定格式的字符串
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @param dateTime 日期时间
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
|
public static String toString(String pattern, ZonedDateTime dateTime) {
|
||||||
|
return getDateTimeFormatter(pattern).format(dateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toYearString(Year year) {
|
/**
|
||||||
return year.toString();
|
* 将时间戳转换为指定格式的字符串,使用系统默认时区
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @param instant 时间戳
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
|
public static String toString(String pattern, Instant instant) {
|
||||||
|
ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
|
||||||
|
return toString(pattern, dateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toMonthString(int monthValue) {
|
/**
|
||||||
return Strings.padStart(Integer.toString(monthValue), 2, '0');
|
* 将时间戳转换为指定格式的字符串,使用指定时区
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @param instant 时间戳
|
||||||
|
* @param zone 时区
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
|
public static String toString(String pattern, Instant instant, ZoneId zone) {
|
||||||
|
ZonedDateTime dateTime = instant.atZone(zone);
|
||||||
|
return toString(pattern, dateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toMonthString(int monthValue, boolean padStart) {
|
// #endregion
|
||||||
return padStart ? toMonthString(monthValue) : Integer.toString(monthValue);
|
|
||||||
|
// #region - nowStr
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定格式,返回当前时间戳对应的字符串
|
||||||
|
*
|
||||||
|
* @param pattern 时间格式
|
||||||
|
* @return 格式化的字符串
|
||||||
|
*/
|
||||||
|
public static String nowStr(String pattern) {
|
||||||
|
return toString(pattern, ZonedDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toMonthString(Month month) {
|
/**
|
||||||
final int monthValue = month.getValue();
|
* 指定格式,返回当前时间戳对应的字符串,使用指定时区
|
||||||
return Strings.padStart(Integer.toString(monthValue), 2, '0');
|
*
|
||||||
}
|
* @param pattern 时间格式
|
||||||
|
* @param zone 时区
|
||||||
public static String toMonthString(Month month, boolean padStart) {
|
* @return 格式化的字符串
|
||||||
final int monthValue = month.getValue();
|
*/
|
||||||
return padStart ? toMonthString(month) : Integer.toString(monthValue);
|
public static String nowStr(String pattern, ZoneId zone) {
|
||||||
|
return toString(pattern, Instant.now().atZone(zone));
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
@ -649,42 +708,6 @@ public class DateTimeTools {
|
|||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// ================================
|
|
||||||
// #region - others
|
|
||||||
// ================================
|
|
||||||
|
|
||||||
public static LocalDate startDateOfYear(int year) {
|
|
||||||
return LocalDate.ofYearDay(year, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static LocalDate endDateOfYear(int year) {
|
|
||||||
return LocalDate.of(year, 12, 31);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static LocalDateTime startOfNextDate(LocalDate date) {
|
|
||||||
return date.plusDays(1L).atStartOfDay();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ZonedDateTime startOfNextDate(LocalDate date, ZoneId zone) {
|
|
||||||
return date.plusDays(1L).atStartOfDay(zone);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Range<LocalDateTime> toDateTimeRange(LocalDate date) {
|
|
||||||
return Range.closedOpen(date.atStartOfDay(), startOfNextDate(date));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Range<ZonedDateTime> toDateTimeRange(LocalDate date, ZoneId zone) {
|
|
||||||
return Range.closedOpen(date.atStartOfDay(zone), startOfNextDate(date, zone));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isLeapYear(int year) {
|
|
||||||
return IsoChronology.INSTANCE.isLeapYear(year);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ================================
|
|
||||||
// #endregion - others
|
|
||||||
// ================================
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 私有构造方法,明确标识该常量类的作用。
|
* 私有构造方法,明确标识该常量类的作用。
|
||||||
*/
|
*/
|
||||||
|
@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.Month;
|
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
@ -33,6 +32,13 @@ class DateTimeToolsTests {
|
|||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(DateTimeToolsTests.class);
|
private static final Logger log = LoggerFactory.getLogger(DateTimeToolsTests.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLocalNowStr() {
|
||||||
|
String nowStr = DateTimeTools.nowStr("yyyy/MM/dd HH:mm:ss.SSS");
|
||||||
|
log.info(nowStr);
|
||||||
|
assertEquals(23, nowStr.length());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testToJoda() {
|
void testToJoda() {
|
||||||
LocalDateTime dt = LocalDateTime.of(2008, 8, 8, 20, 18, 59, 108000000);
|
LocalDateTime dt = LocalDateTime.of(2008, 8, 8, 20, 18, 59, 108000000);
|
||||||
@ -49,8 +55,7 @@ class DateTimeToolsTests {
|
|||||||
void testToInstant() {
|
void testToInstant() {
|
||||||
ZonedDateTime dt = ZonedDateTime.of(2008, 1, 8, 10, 23, 50, 108000000, ZoneId.systemDefault());
|
ZonedDateTime dt = ZonedDateTime.of(2008, 1, 8, 10, 23, 50, 108000000, ZoneId.systemDefault());
|
||||||
Instant instant = DateTimeTools.toInstant(dt.toInstant().toEpochMilli());
|
Instant instant = DateTimeTools.toInstant(dt.toInstant().toEpochMilli());
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-M-d HH:mm:ss.SSS");
|
String str = DateTimeTools.toString("yy-M-d HH:mm:ss.SSS", instant);
|
||||||
String str = formatter.format(instant.atZone(ZoneId.systemDefault()));
|
|
||||||
log.info(str);
|
log.info(str);
|
||||||
assertEquals("08-1-8 10:23:50.108", str);
|
assertEquals("08-1-8 10:23:50.108", str);
|
||||||
}
|
}
|
||||||
@ -102,11 +107,4 @@ class DateTimeToolsTests {
|
|||||||
log.info("{}", formatter);
|
log.info("{}", formatter);
|
||||||
log.info("{}", formatter2);
|
log.info("{}", formatter2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void testToString() {
|
|
||||||
assertEquals("04", DateTimeTools.toMonthString(Month.APRIL));
|
|
||||||
assertEquals("04", DateTimeTools.toMonthString(Month.APRIL, true));
|
|
||||||
assertEquals("4", DateTimeTools.toMonthString(Month.APRIL, false));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user