我想从一个月的一周中获取开始日期和结束日期(android)

好人

我想获取一个月中特定周的开始日期和结束日期,例如,如果我从给定的日历图片中选择第一周,那么开始日期将为 1/FEB/2017,结束日期将为 4/FEB/2017。谢谢。

好人

日历日历 = Calendar.getInstance();

    Date date1 = calendar.getTime();
    //current date to check that our week lies in same month or not
    SimpleDateFormat checkformate = new SimpleDateFormat("MM/yyyy");
    String currentCheckdate= checkformate.format(date1);

    int weekn = calendar.get(Calendar.WEEK_OF_MONTH);
    int month = calendar.get(Calendar.MONTH);
    int year  = calendar.get(Calendar.YEAR);
    //resat calender without date
    calendar.clear();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.set(Calendar.WEEK_OF_MONTH,weekn);
    calendar.set(Calendar.MONTH,month);
    calendar.set(Calendar.YEAR,year);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");

    Date datef = calendar.getTime();
    //move date to 6 days + to get last date of week
    Long timeSixDayPlus = calendar.getTimeInMillis()+518400000L;
    Date dateL = new Date(timeSixDayPlus);
    String firtdate = simpleDateFormat.format(datef);
    String lastdate = simpleDateFormat.format(dateL);
    String firtdateCheck = checkformate.format(datef);
    String lastdateCheck = checkformate.format(dateL);

    //if our week lies in two different months then we show only current month week part only
    if (!firtdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
        firtdate = "1" + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
    }
    if (!lastdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
        int ma = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        lastdate = String.valueOf(ma) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
    }
    Log.e("current","=>>"+firtdate+" to "+lastdate);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我如何使用 Carbon 从一个月的一周和一周的哪一天获得日期?

SQL Server:获取一个月的第一周和第二周的日期范围

使用powershell获取一个月最后一周的日期

如何在oracle中查找一个月中所有周的开始日期和结束日期

我怎么知道某个特定日期是父日期是一个月还是一周?

确定一周中的哪一天是一个月的每个日期

在 Python 中获取一个月开始和结束的 Unix 时间戳

获取一周的开始和结束日期。用户输入了月,周和年

如何编码一个动态的NetSuite日期范围,该日期范围从一个月的第一天(4个月前)开始,到上个月的最后一天结束?

从 PHP 中的日期范围获取一周的开始和结束日期

从一年的开始到一个月前的最后一天的结束之间的日期?

在另一列中基于一个月的开始和日期对行值求和

PostgreSQL - 如何获得前一个月和一周的价值?

同一个月的一周中的几天,moment.js

在Python日期时间为什么是第一个月,“月”,= 1,但“星期一”,一周= 0的第一天?

如何检索日期从一个月的第一天开始的所有行

获取一周中的几天,一个月中的一天

从周号中查找一周的开始结束日期

我有一个月的周数,并想在 PHP 中获得该周内的第一个日期

在sqlite数据库中仅获取日期以来的一个月

使用Java Calendar类获取一周的开始日期和结束日期

为什么如果所选的开始日期与所选的结束日期相差一个月,为什么总是出现错误,说明日期格式无效?

如何从日期开始到下一个星期日结束的一周

我想在当前日期的下拉菜单中显示一个月的日期和日期-月份

给定的天数超过一周,一个月或一年

计算一天,一周,一个月的用户注册

Power BI如何查找一个月的结束日期Q

如何从一个日期中查找一个月的剩余日期?

如何获取日期:距离特定日期一个月?