java日历 - 获取最后5天的星期(没有周末) [英] java calendar - get last 5 days of week (without weekend)

查看:246
本文介绍了java日历 - 获取最后5天的星期(没有周末)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些问题。

我需要从数据库获取一些数据

I need to get some data from database

1 )从最后5天获得5条记录(每天一条记录),但天必须是一周的天(现在是周末)

1) get 5 records from 5 last days (one record per one day), but the days must be the days of a week (now a weekend)

2)得到20条记录每月的记录(当前或过去,每天一个记录)

2) get 20 records from the month (current or past, one record per one day)

我在与Java日历打斗,但仍然不知道,如何获取日期。

I'm fighting with Java Calendar, but still i have no idea, how to get the dates.

有人能帮助我吗?

推荐答案

应该工作:

private Calendar workingDaysBack(final Calendar from, final int count) {
    for (int daysBack = 0; daysBack < count; ++daysBack) {
        do {
            from.add(Calendar.DAY_OF_YEAR, -1);
        } while(isWeekend(from));
    }
    return from;
}

private boolean isWeekend(Calendar cal) {
    return cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
           cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
}

这篇关于java日历 - 获取最后5天的星期(没有周末)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆