DateTimeUtils.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.izouma.nineth.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.github.kevinsawicki.http.HttpRequest;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. import java.time.Instant;
  8. import java.time.LocalDate;
  9. import java.time.LocalDateTime;
  10. import java.time.format.DateTimeFormatter;
  11. import java.time.temporal.TemporalAccessor;
  12. public class DateTimeUtils {
  13. public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
  14. public static final String T_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
  15. public static final String DATE_FORMAT = "yyyy-MM-dd";
  16. public static LocalDate toLocalDate(long ts) {
  17. if (String.valueOf(ts).length() > 10) {
  18. return LocalDate.from(Instant.ofEpochMilli(ts));
  19. } else {
  20. return LocalDate.from(Instant.ofEpochSecond(ts));
  21. }
  22. }
  23. public static LocalDateTime toLocalDateTime(long ts) {
  24. if (String.valueOf(ts).length() > 10) {
  25. return LocalDateTime.from(Instant.ofEpochMilli(ts));
  26. } else {
  27. return LocalDateTime.from(Instant.ofEpochSecond(ts));
  28. }
  29. }
  30. public static LocalDate toLocalDate(String str, String fmt) {
  31. return LocalDate.from(DateTimeFormatter.ofPattern(fmt).parse(str));
  32. }
  33. public static LocalDateTime toLocalDateTime(String str, String fmt) {
  34. return LocalDateTime.from(DateTimeFormatter.ofPattern(fmt).parse(str));
  35. }
  36. public static String format(TemporalAccessor temporal, String fmt) {
  37. return DateTimeFormatter.ofPattern(fmt).format(temporal);
  38. }
  39. public static long toTimestamp(TemporalAccessor temporal) {
  40. return Instant.from(temporal).toEpochMilli();
  41. }
  42. public static boolean isWorkDay(LocalDate date) {
  43. try {
  44. HttpRequest request = HttpRequest.get("https://jiejiari.market.alicloudapi.com/queryHoliday?day="
  45. + date.format(DateTimeFormatter.ofPattern("yyyyMMdd")))
  46. .header("Authorization", "APPCODE b48bc8f6759345a79ae20a951f03dabe");
  47. int code = request.code();
  48. if (code == 200) {
  49. String body = request.body();
  50. JSONObject res = JSON.parseObject(body);
  51. System.out.println(JSON.toJSONString(res, true));
  52. if (res.getInteger("showapi_res_code") == 0) {
  53. JSONObject resBody = res.getJSONObject("showapi_res_body");
  54. return StringUtils.equals(resBody.getString("type"), "1");
  55. }
  56. } else {
  57. System.out.println(code);
  58. }
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. return true;
  63. }
  64. public static void main(String[] args) {
  65. System.out.println(isWorkDay(LocalDate.now().plusDays(2)));
  66. }
  67. }