diff --git a/src/main/java/io/github/ehlxr/date/Main.java b/src/main/java/io/github/ehlxr/date/Main.java new file mode 100644 index 0000000..0b3bbfc --- /dev/null +++ b/src/main/java/io/github/ehlxr/date/Main.java @@ -0,0 +1,93 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.date; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +/** + * @author ehlxr + * @since 2021-01-28 16:03. + */ +public class Main { + public static void main(String[] args) { + // 旧 API 转新 API + // Date -> Instant: + Instant ins1 = new Date().toInstant(); + System.out.println(ins1); + + ZonedDateTime zonedDateTime = ins1.atZone(ZoneId.systemDefault()); + System.out.println(zonedDateTime); + + LocalDateTime localDateTime = zonedDateTime.toLocalDateTime(); + System.out.println(localDateTime); + + // Calendar -> Instant -> ZonedDateTime: + Calendar calendar = Calendar.getInstance(); + Instant ins2 = calendar.toInstant(); + ZonedDateTime zdt = ins2.atZone(calendar.getTimeZone().toZoneId()); + System.out.println(zdt); + + // 新 API 转旧 API + // ZonedDateTime -> long: + ZonedDateTime zdt1 = ZonedDateTime.now(); + long ts = zdt1.toEpochSecond() * 1000; + + // long -> Date: + Date date = new Date(ts); + + // long -> Calendar: + Calendar calendar1 = Calendar.getInstance(); + calendar1.clear(); + calendar1.setTimeZone(TimeZone.getTimeZone(zdt.getZone().getId())); + calendar1.setTimeInMillis(zdt.toEpochSecond() * 1000); + + + // 在数据库中存储时间戳时,尽量使用 long 型时间戳,它具有省空间,效率高,不依赖数据库的优点 + ts = 1574208900000L; + System.out.println(timestampToString(ts, Locale.CHINA, "Asia/Shanghai")); + System.out.println(timestampToString(ts, Locale.US, "America/New_York")); + } + + /** + * 不同用户以不同的偏好来显示不同的本地时间 + */ + static String timestampToString(long epochMilli, Locale lo, String zoneId) { + Instant ins = Instant.ofEpochMilli(epochMilli); + DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT) + // 按照 Locale 默认习惯格式化 + .withLocale(lo); + + return f.withLocale(lo).format(ZonedDateTime.ofInstant(ins, ZoneId.of(zoneId))); + } +} diff --git a/src/main/java/io/github/ehlxr/utils/Builder.java b/src/main/java/io/github/ehlxr/utils/Builder.java new file mode 100644 index 0000000..41d37ad --- /dev/null +++ b/src/main/java/io/github/ehlxr/utils/Builder.java @@ -0,0 +1,96 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + * @author ehlxr + * @since 2021-02-04 14:40. + */ +public class Builder { + private final Supplier instantiator; + private final List> modifiers = new ArrayList<>(); + + public Builder(Supplier instantiator) { + this.instantiator = instantiator; + } + + public static Builder of(Supplier instantiator) { + return new Builder<>(instantiator); + } + + public Builder with(Consumer1 consumer, P1 p1) { + Consumer c = instance -> consumer.accept(instance, p1); + modifiers.add(c); + return this; + } + + public Builder with(Consumer2 consumer, P1 p1, P2 p2) { + Consumer c = instance -> consumer.accept(instance, p1, p2); + modifiers.add(c); + return this; + } + + public Builder with(Consumer3 consumer, P1 p1, P2 p2, P3 p3) { + Consumer c = instance -> consumer.accept(instance, p1, p2, p3); + modifiers.add(c); + return this; + } + + public T build() { + T value = instantiator.get(); + modifiers.forEach(modifier -> modifier.accept(value)); + modifiers.clear(); + return value; + } + + /** + * 1 参数 Consumer + */ + @FunctionalInterface + public interface Consumer1 { + void accept(T t, P1 p1); + } + + /** + * 2 参数 Consumer + */ + @FunctionalInterface + public interface Consumer2 { + void accept(T t, P1 p1, P2 p2); + } + + /** + * 3 参数 Consumer + */ + @FunctionalInterface + public interface Consumer3 { + void accept(T t, P1 p1, P2 p2, P3 p3); + } +} diff --git a/src/main/java/io/github/ehlxr/utils/Try.java b/src/main/java/io/github/ehlxr/utils/Try.java index 6cf2310..16ab91e 100644 --- a/src/main/java/io/github/ehlxr/utils/Try.java +++ b/src/main/java/io/github/ehlxr/utils/Try.java @@ -40,10 +40,10 @@ public interface Try { * 构建消费型(有入参,无返回)Tryable 对象 * * @param consumer {@link ThrowableConsumer} 类型函数式接口 - * @param 入参类型 + * @param

入参类型 * @return {@link TryConsumer} */ - static TryConsumer of(ThrowableConsumer consumer) { + static

TryConsumer

of(ThrowableConsumer consumer) { return new TryConsumer<>(consumer); } @@ -62,11 +62,11 @@ public interface Try { * 构建功能型(有入参,有返回)Tryable 对象 * * @param function {@link ThrowableFunction} 类型函数式接口 - * @param 入参类型 + * @param

入参类型 * @param 返回值类型 * @return {@link TryFunction} */ - static TryFunction of(ThrowableFunction function) { + static TryFunction of(ThrowableFunction function) { return new TryFunction<>(function); } @@ -160,32 +160,15 @@ public interface Try { } } - class TryConsumer extends Tryable> { - private final ThrowableConsumer consumer; - - protected TryConsumer(ThrowableConsumer consumer) { - Objects.requireNonNull(consumer, "No consumer present"); - this.consumer = consumer; - - super.c = this; - } - + @FunctionalInterface + interface ThrowableConsumer

{ /** - * 计算结果 + * Performs this operation on the given argument. * - * @param t 要计算的入参 + * @param p the input argument + * @throws Throwable throwable */ - public void accept(T t) { - try { - Objects.requireNonNull(t, "No accept t present"); - - consumer.accept(t); - } catch (final Throwable e) { - Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e)); - } finally { - doFinally(); - } - } + void accept(P p) throws Throwable; } class TrySupplier extends Tryable> { @@ -232,11 +215,72 @@ public interface Try { } } - class TryFunction extends Tryable> { - private final ThrowableFunction function; - private T t; + @FunctionalInterface + interface ThrowableSupplier

{ + /** + * Gets a result. + * + * @return a result + * @throws Throwable throwable + */ + P get() throws Throwable; + } - protected TryFunction(ThrowableFunction function) { + @FunctionalInterface + interface ThrowableRunnable { + /** + * Performs this operation + * + * @throws Throwable throwable + */ + void run() throws Throwable; + } + + @FunctionalInterface + interface ThrowableFunction { + /** + * Applies this function to the given argument. + * + * @param p the function argument + * @return the function result + * @throws Throwable throwable + */ + R apply(P p) throws Throwable; + } + + class TryConsumer

extends Tryable> { + private final ThrowableConsumer consumer; + + protected TryConsumer(ThrowableConsumer consumer) { + Objects.requireNonNull(consumer, "No consumer present"); + this.consumer = consumer; + + super.c = this; + } + + /** + * 计算结果 + * + * @param p 要计算的入参 + */ + public void accept(P p) { + try { + Objects.requireNonNull(p, "No accept p present"); + + consumer.accept(p); + } catch (final Throwable e) { + Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e)); + } finally { + doFinally(); + } + } + } + + class TryFunction extends Tryable> { + private final ThrowableFunction function; + private P p; + + protected TryFunction(ThrowableFunction function) { Objects.requireNonNull(function, "No function present"); this.function = function; @@ -246,13 +290,13 @@ public interface Try { /** * 传入要计算的入参 * - * @param t 要计算的入参 + * @param p 要计算的入参 * @return {@link TryFunction} */ - public TryFunction apply(T t) { - Objects.requireNonNull(t, "Apply t should not null"); + public TryFunction apply(P p) { + Objects.requireNonNull(p, "Apply p should not null"); - this.t = t; + this.p = p; return this; } @@ -264,9 +308,9 @@ public interface Try { */ public R get(R r) { try { - Objects.requireNonNull(function, "No apply t present"); + Objects.requireNonNull(function, "No apply p present"); - return function.apply(t); + return function.apply(p); } catch (final Throwable e) { Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e)); return r; @@ -282,9 +326,9 @@ public interface Try { */ public R get() { try { - Objects.requireNonNull(t, "No apply t present"); + Objects.requireNonNull(p, "No apply p present"); - return function.apply(t); + return function.apply(p); } catch (final Throwable e) { Optional.ofNullable(throwConsumer).ifPresent(c -> c.accept(e)); return null; @@ -294,50 +338,6 @@ public interface Try { } } - @FunctionalInterface - interface ThrowableRunnable { - /** - * Performs this operation - * - * @throws Throwable throwable - */ - void run() throws Throwable; - } - - @FunctionalInterface - interface ThrowableConsumer { - /** - * Performs this operation on the given argument. - * - * @param t the input argument - * @throws Throwable throwable - */ - void accept(T t) throws Throwable; - } - - @FunctionalInterface - interface ThrowableSupplier { - /** - * Gets a result. - * - * @return a result - * @throws Throwable throwable - */ - T get() throws Throwable; - } - - @FunctionalInterface - interface ThrowableFunction { - /** - * Applies this function to the given argument. - * - * @param t the function argument - * @return the function result - * @throws Throwable throwable - */ - R apply(T t) throws Throwable; - } - @SuppressWarnings({"ConstantConditions", "Convert2MethodRef"}) static void main(String[] args) { // 有返回值,无入参