百分api 文档解析
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.8 KiB

  1. package com.bw.ocr.utils;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9. import java.util.Date;
  10. import lombok.extern.slf4j.Slf4j;
  11. import com.alibaba.fastjson.JSON;
  12. import com.alibaba.fastjson.JSONObject;
  13. /**
  14. * 日期工具类
  15. *
  16. * @author jian.mao
  17. * @date 2022年11月15日
  18. * @description
  19. */
  20. @Slf4j
  21. public class DateUtil {
  22. /**
  23. * @return
  24. */
  25. public static String getTimeStrForNow() {
  26. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
  27. return sdf.format(new Date());
  28. }
  29. public static String getTimeStrForDay(long time) {
  30. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  31. return sdf.format(new Date(time * 1000));
  32. }
  33. public static String getTimeStrForDay() {
  34. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  35. return sdf.format(new Date());
  36. }
  37. public static String getDateTime() {
  38. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  39. String time = sdf.format(new Date());
  40. return time;
  41. }
  42. public static String getDateTime(Long timestap) {
  43. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  44. String time = sdf.format(new Date(timestap));
  45. return time;
  46. }
  47. public static String getDate(Long timestap) {
  48. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  49. String time = sdf.format(new Date(timestap));
  50. return time;
  51. }
  52. public static String getDateTimeForMonth() {
  53. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
  54. String time = sdf.format(new Date());
  55. return time;
  56. }
  57. /**
  58. * 休眠
  59. *
  60. * @param millis 毫秒
  61. */
  62. public static void sleep(long millis) {
  63. try {
  64. Thread.sleep(millis);
  65. } catch (InterruptedException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. /**
  70. * 1. @Description:时间戳转时间
  71. * 2. @Author: ying.zhao
  72. * 3. @Date: 2023/3/28
  73. */
  74. public static String timestampToDate(String time) {
  75. int thirteen = 13;
  76. int ten = 10;
  77. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  78. // if (time.length() == thirteen) {
  79. if (time.length() > ten) {
  80. return sdf.format(new Date(Long.parseLong(time)));
  81. } else {
  82. return sdf.format(new Date(Integer.parseInt(time) * 1000L));
  83. }
  84. }
  85. public static String parseCreated(String jsonTime){
  86. String formattedDateTime = getDateTime();
  87. try {
  88. // 使用fastjson解析JSON数据
  89. JSONObject jsonObject = JSON.parseObject(jsonTime);
  90. // 获取日期和时间的值
  91. JSONObject dateObject = jsonObject.getJSONObject("date");
  92. int day = dateObject.getIntValue("day");
  93. int month = dateObject.getIntValue("month");
  94. int year = dateObject.getIntValue("year");
  95. JSONObject timeObject = jsonObject.getJSONObject("time");
  96. int hour = timeObject.getIntValue("hour");
  97. int minute = timeObject.getIntValue("minute");
  98. int second = timeObject.getIntValue("second");
  99. // 创建LocalDateTime对象
  100. LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute, second);
  101. // 定义日期时间格式化器
  102. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  103. // 格式化日期时间
  104. formattedDateTime = dateTime.format(formatter);
  105. } catch (Exception e) {
  106. log.info("日期转换失败:{}",e);
  107. }
  108. return formattedDateTime;
  109. }
  110. /**
  111. * 字符串转换日期
  112. * @param format
  113. * @param date
  114. * @return
  115. */
  116. public static Date strToDate(String format,String date){
  117. SimpleDateFormat sdf = new SimpleDateFormat(format);
  118. if (date == null || date.equals("")){
  119. return new Date();
  120. }else{
  121. Date ru = null;
  122. try {
  123. ru = sdf.parse(date);
  124. } catch (ParseException e) {
  125. e.printStackTrace();
  126. }
  127. return ru;
  128. }
  129. }
  130. /**
  131. * 日期格式话
  132. * @param format 日期格式
  133. * @param dater 要转换的日期,默认当前时间
  134. * @return
  135. */
  136. public static String FormatDate(String format,Date date){
  137. String fromatDate = null;
  138. SimpleDateFormat sdf = new SimpleDateFormat(format);
  139. if (date == null){
  140. fromatDate = sdf.format(new Date());
  141. }else{
  142. fromatDate = sdf.format(date);
  143. }
  144. return fromatDate;
  145. }
  146. public static void main(String[] args) {
  147. String time = timestampToDate("955814400000");
  148. System.out.println(time);
  149. }
  150. }