提交自己二次封装的util类

This commit is contained in:
wen 2022-01-15 16:32:52 +08:00
parent 634fe23e7d
commit 32e76be85b
2 changed files with 110 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}