mysql根据help_topic 获取指定日期

2023-12-14 07:13:44

1.获取最近6年

SELECT YEAR
	(
	DATE_SUB( CURRENT_DATE, INTERVAL help_topic_id YEAR )) date 
FROM
	mysql.help_topic 
WHERE
	help_topic_id <6

2.获取最近6个月

SELECT
	date_format( date_sub( curdate(), INTERVAL ( cast( help_topic_id AS signed ) - 0 ) MONTH ), '%Y-%m' ) date 
FROM
	mysql.help_topic 
WHERE
	help_topic_id<6
ORDER BY
	help_topic_id

3.获取最近7天

SELECT
	DATE_FORMAT( date_add( curdate(), INTERVAL ( CAST( help_topic_id AS signed ) - 6 ) DAY ), '%m-%d' ) AS date 
FROM
	mysql.help_topic 
WHERE
	help_topic_id < 7 
ORDER BY
help_topic_id

?

4.获取指定传入日期

SELECT
	DATE_FORMAT(
		DATE_ADD(
			CONCAT('2023-11-01'), INTERVAL (help_topic_id) DAY),
			'%Y-%m-%d' ) dateStr 
		FROM
			mysql.help_topic 
		WHERE
			help_topic_id <= TIMESTAMPDIFF(
			DAY,
			CONCAT('2023-11-01'),
			CONCAT('2023-11-11')
			)		
			ORDER BY
	dateStr

?

5.获取这个星期的日期

1.第一步获取当前日期的星期一的日期

  /**
     * 获取周一
     */
    public static String getThisWeekMonday(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return dateFormat.format(cal.getTime());
    }

2.传入星期一的日期,获取7天的日期

SELECT DATE_FORMAT(date_add(curdate(), INTERVAL ( CAST( help_topic_id AS signed ) - 6 ) DAY ), '%Y-%m-%d') AS `week`
                          FROM mysql.help_topic
                          WHERE help_topic_id &lt; 7
                          ORDER BY help_topic_id

3.日期转化为星期显示

 /**
     * 日期转星期
     */
    public static String dateToWeek(String time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(dateFormat.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = Math.max(cal.get(Calendar.DAY_OF_WEEK) - 1, 0);
        return weekDays[w];
    }

文章来源:https://blog.csdn.net/qq_23591001/article/details/134928938
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。