From 32e76be85bfd14a2ce0dfc9d7baeefff92ca5345 Mon Sep 17 00:00:00 2001 From: wen <1455474577@qq.com> Date: Sat, 15 Jan 2022 16:32:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=87=AA=E5=B7=B1=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E5=B0=81=E8=A3=85=E7=9A=84util=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xhpc/common/util/MyDateUtil.java | 76 +++++++++++++++++++ .../com/xhpc/common/util/MyPagingUtil.java | 34 +++++++++ 2 files changed, 110 insertions(+) create mode 100644 xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/util/MyDateUtil.java create mode 100644 xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/util/MyPagingUtil.java diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/util/MyDateUtil.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/util/MyDateUtil.java new file mode 100644 index 00000000..a910ddb0 --- /dev/null +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/util/MyDateUtil.java @@ -0,0 +1,76 @@ +package com.xhpc.common.util; + +import cn.hutool.core.date.DateTime; + +import java.util.Calendar; +import java.util.Date; + +/** + * 进行日期处理的工具类 + * + * @author WH + * @date 2022/1/15 15:59 + * @since version-1.0 + */ +@SuppressWarnings("all") +public class MyDateUtil { + + public static void main(String[] args) { + + System.out.println(getCurrentDateStr()); + } + + /** + * 以xxxx年x月xx日 星期x的字符串格式展示当前时间 + * + * @author WH + * @date 2022/1/15 16:15 + * @since version-1.0 + */ + public static String getCurrentDateStr() { + + Calendar now = Calendar.getInstance(); + int year = now.get(Calendar.YEAR); + int month = now.get(Calendar.MONTH) + 1; + int day = now.get(Calendar.DAY_OF_MONTH); + String dayNames[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; + int dayOfWeek = now.get(Calendar.DAY_OF_WEEK) - 1; + if (dayOfWeek < 0) dayOfWeek = 0; + String week = dayNames[dayOfWeek]; + String nowDateStr = year + "年" + month + "月" + day + "日 " + week; + + return nowDateStr; + + } + + public static final String DATE_FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss"; + + /** + * 获取表示所传入的Date对象的Calendar对象 + * + * @param date 要转换的Date对象 + * @return 表示所传入的Date对象的Calendar对象 + * @author WH + * @date 2022/1/15 16:11 + * @since version-1.0 + */ + public static Calendar getCalendar(Date date) { + + Calendar c = Calendar.getInstance(); + c.setTime(date); + return c; + } + + /** + * 获取一个yyyy-MM-dd HH:mm:ss格式的当前时间字符串 + * + * @author WH + * @date 2022/1/15 16:04 + * @since version-1.0 + */ + public static String getCurrentDateStrInYyyyMmDdHhMmSsFormat() { + + return DateTime.now().toString(DATE_FORMAT_DATE_TIME); + } + +} diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/util/MyPagingUtil.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/util/MyPagingUtil.java new file mode 100644 index 00000000..7ddeb358 --- /dev/null +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/util/MyPagingUtil.java @@ -0,0 +1,34 @@ +package com.xhpc.common.util; + +/** + * 用于进行分页的工具类 + * + * @author WH + * @date 2022/1/15 15:25 + * @since version-1.0 + */ +public class MyPagingUtil { + + /** + * 用于计算MySQL数据库中的limit子句的startIndex值 + * + * @param currentPage 当前要显示的记录的页数 + * @param items 当前页要显示多少条记录 + * @return 返回MySQL数据库中的limit子句的startIndex值 + * @throws IllegalArgumentException 传入的参数不符合要求,发生此异常 + * @author WH + * @date 2022/1/15 15:27 + * @since version-1.0 + */ + public static int calculateStartIndex(int currentPage, int items) throws IllegalArgumentException { + + if ((currentPage - 1) < 0) { + throw new IllegalArgumentException("当前页数最小为1"); + } + if (items < 1) { + throw new IllegalArgumentException("当前页要显示的记录数最小为1"); + } + return (currentPage - 1) * items; + } + +}