在Android上开始和结束一周

时间:2011-07-07 22:24:20

标签: java android date

我想知道如何计算本周的开始和结束日期? 我发现它没有在标准的android库中实现,也没有像date4j那样实现。

如果有一些简单明了的方法来实现这个?或者我必须再次实施自行车?

感谢。

7 个答案:

答案 0 :(得分:6)

使用date4j执行此操作不需要太多代码。 计算一周的第一天的示例:

 private void firstDayOfThisWeek(){
    DateTime today = DateTime.today(TimeZone.getDefault()); 
    DateTime firstDayThisWeek = today; //start value 
    int todaysWeekday = today.getWeekDay();
    int SUNDAY = 1;
    if(todaysWeekday > SUNDAY){
      int numDaysFromSunday = todaysWeekday - SUNDAY;
      firstDayThisWeek = today.minusDays(numDaysFromSunday);
    }
    System.out.println("The first day of this week is : " + firstDayThisWeek);
  }

以上是周日是本周开始的惯例。在某些司法管辖区,该惯例不适用,因此您需要更改此类案例的代码。

答案 1 :(得分:5)

也许MonthDisplayHelper对您有所帮助。

祝你好运!

答案 2 :(得分:1)

尝试java.util.Calendar.getFirstDayOfWeek()...然后很容易计算一周的最后一天:http://developer.android.com/reference/java/util/Calendar.html#getFirstDayOfWeek()

答案 3 :(得分:1)

使用Joda-Time 2.3库,这种日期时间工作通常更容易。

this answer转到类似的问题。

LocalDate now = new LocalDate();
System.out.println( now.withDayOfWeek(DateTimeConstants.MONDAY) );
System.out.println( now.withDayOfWeek(DateTimeConstants.SUNDAY) ); 

您可以致电isBefore / isAfterminusWeeks / plusWeeks来获取过去/未来的价值。

答案 4 :(得分:1)

这对我有用......

Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
cal.set(Calendar.DAY_OF_WEEK, cal.MONDAY);
String firstWkDay = String.valueOf(cal.getTime());
//cal.set(Calendar.DAY_OF_WEEK, cal.SUNDAY);
cal.add(Calendar.DAY_OF_WEEK, 6);
String lastWkDay =  String.valueOf(cal.getTime());

对于一周中的最后一天,您可以使用cal.set(Calendar.DAY_OF_WEEK, cal.SUNDAY);,但它会在某些设备上返回上一个星期日

答案 5 :(得分:1)

这是我用来获取日期的原因:

 Calendar c = Calendar.getInstance();
 c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
 c.setFirstDayOfWeek(Calendar.MONDAY);

 startOfWeek = c.getTime();

答案 6 :(得分:0)

我为此写了自己的逻辑。

fun getStartDateAndEndDateOfWeek(myDate :String) : ArrayList<String>{
        var dates:ArrayList<String> = arrayListOf()
        var cal = Calendar.getInstance()
        cal.time = SimpleDateFormat("yyy/MM/dd").parse(myDate)
        Log.e("==== ","== My Date  : ${SimpleDateFormat("EEE,dd MMM yyyy").format(SimpleDateFormat("yyy/MM/dd").parse(myDate))} ")
        Log.e("== ","Day of Week : ${cal.get(Calendar.DAY_OF_WEEK)}")

        var startDate =""
        var endDate =""

        when(cal.get(Calendar.DAY_OF_WEEK)){
            1 ->{
                var date = getOldDate(6,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(date)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(cal.time)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Sunday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            2 ->{
                var sdate = getOldDate(0,cal.time)
                var edate = getOldDate(-6,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Monday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            3 ->{
                var sdate = getOldDate(1,cal.time)
                var edate = getOldDate(-5,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Tuesday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            4 ->{
                var sdate = getOldDate(2,cal.time)
                var edate = getOldDate(-4,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Wednesday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            5 ->{
                var sdate = getOldDate(3,cal.time)
                var edate = getOldDate(-3,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Thursday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            6 ->{
                var sdate = getOldDate(4,cal.time)
                var edate = getOldDate(-2,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"
                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Friday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            7 ->{
                var sdate = getOldDate(5,cal.time)
                var edate = getOldDate(-1,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Saturday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
        }

        return dates
        //getStartEndOFWeek(cal.get(Calendar.WEEK_OF_YEAR),cal.get(Calendar.YEAR),myDate)
    }


    fun getOldDate(daysAgo: Int,myDate:Date): Date {
        val calendar = Calendar.getInstance()
        /*calendar.set(Calendar.MONTH, APRIL)
        calendar.set(Calendar.DAY_OF_MONTH,6)
        calendar.set(Calendar.YEAR,2020)
        Log.e("==== ","=====  Date  : ${calendar.time}")*/
        calendar.add(Calendar.DAY_OF_YEAR, -daysAgo)
        //Log.e("==== ","=====  Date $daysAgo ago : ${calendar.time}")
        return calendar.time
    }