这篇个人常用Java函数,不是技术类文章

一、sql

1.1、数据库常用sql

DECLARE startDate date DEFAULT DATE_FORMAT(now_day,'%Y-%m-%d');	-- 更新数据的时间
DECLARE startStamp int(11) DEFAULT UNIX_TIMESTAMP(startDate);	-- 当天0点时间戳
DECLARE endStamp int(11) DEFAULT UNIX_TIMESTAMP(startDate)+86400-1;	-- 当天23点59分59秒
DECLARE theHour int(11) DEFAULT DATE_FORMAT(SYSDATE(),'%H'); -- 当前小时

1.2、数据库查询格式问题

  • 错误写法
'%Y-%m-%d %H:%m:%s'

image

  • 正确写法
%Y-%m-%d %H:%i:%s

image-1654845956524

二、JAVA基础

2.1、字符串截取与替换

  • 字符串切割成数组

  • 数组转字符串

三、获取系统当前操作人

前提前台传了cookie

public final static String getCurrentUser() {
        String userName = null;
        try {
            SecurityContext sc = SecurityContextHolder.getContext();
            Object principal = sc.getAuthentication().getPrincipal();
            if (principal instanceof UserDetails) {
                UserDetails userDetails = (UserDetails) principal;
                userName = userDetails.getUsername();

            }
        } catch (Exception e) {
            return null;
        }
        return userName;
    }

四、map转bean

    private static <T> T toBean(JSONObject jsonObject, T t, boolean isClearNullField) {
        JsonConfig config = new JsonConfig();
        config.setPropertySetStrategy(new PropertySetStrategy() {
            @Override
            public void setProperty(Object o, String s, Object o1) throws JSONException {
                try {
                    PropertySetStrategy.DEFAULT.setProperty(o, s, o1);
                } catch (Exception e) {

                }
            }
        });
        JSONObject.toBean(jsonObject, t, config);
        if (isClearNullField) {
            clearNullField(t);
        }
        return t;
    }

五、mybatis

5.1、分页

<choose>
            <when test="orderSort != null and orderSort != '' and order != null and order != ''">
                ORDER BY
                ${orderSort}
                ${order}
            </when>
            <otherwise>
                ORDER BY CONVERT(project USING GBK) ASC, `gmt_create` DESC
            </otherwise>
        </choose>

时间转换

projectBoard.setStartDate(new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse("2022-07-01"))

其他

获取日期函数

更多函数可以使用hootool依赖

/**
     * 获取今天
     * @return String
     * */
    public static String getToday(){
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    /**
     * 获取昨天
     * @return String
     * */
    public static String getYestoday(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }


    /**
     * 获取本周的第一天
     * @return String
     * **/
    public static String getWeekStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }

    /**
     * 获取本周的最后一天
     * @return String
     * **/
    public static String getWeekEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }

    /**
     * 获取本月开始日期
     * @return String
     * **/
    public static String getMonthStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }

    /**
     * 获取本月最后一天
     * @return String
     * **/
    public static String getMonthEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }

    /**
     * 获取本年的第一天
     * @return String
     * **/
    public static String getYearStart(){
        return new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
    }

    /**
     * 获取本年的最后一天
     * @return String
     * **/
    public static String getYearEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast)+" 23:59:59";
    }
下一篇