简体   繁体   English

在 Android 上获取一周的开始和结束

[英]Get start and end of week on Android

I wonder how could I calculate start and end days of the current week?我想知道如何计算本周的开始和结束天数? I've found that this it not implemented in standard android libs or such lib as date4j.我发现它没有在标准 android 库或 date4j 之类的库中实现。

If there some easy and plain way to implement this?如果有一些简单明了的方法来实现这一点? Or I have to implement bicycle again?或者我必须再次实施自行车?

Thanks.谢谢。

It doesn't take much code to do this with date4j.使用 date4j 执行此操作不需要太多代码。 An example of calculating the first day of the week:计算一周第一天的示例:

 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);
  }

The above follows the convention that Sunday is the start of the week.以上遵循周日是一周开始的惯例。 In some jurisdictions, that convention doesn't apply, so you would need to change the code for such cases.在某些司法管辖区,该约定不适用,因此您需要针对此类情况更改代码。

Maybe MonthDisplayHelper could be of help for you.也许MonthDisplayHelper可以为您提供帮助。

Good luck!祝你好运!

This worked for me...这对我有用...

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());

For last day of the week, you could use cal.set(Calendar.DAY_OF_WEEK, cal.SUNDAY);对于一周的最后一天,您可以使用cal.set(Calendar.DAY_OF_WEEK, cal.SUNDAY); but it returns previous sunday on some devices但它在某些设备上返回上周日

This kind of date-time work is often easier using the Joda-Time 2.3 library.使用Joda-Time 2.3 库,这种日期时间工作通常更容易。

Taken from this answer to a similar question.取自this answer to a similar question。

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

You can call isBefore / isAfter and minusWeeks / plusWeeks to get past/future values.您可以调用isBefore / isAfterminusWeeks / plusWeeks来获取过去/未来的值。

This is what I have been using to get the date:这是我一直用来获取日期的:

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

 startOfWeek = c.getTime();

Try java.util.Calendar.getFirstDayOfWeek()... then it's easy to calculate the last day of week: http://developer.android.com/reference/java/util/Calendar.html#getFirstDayOfWeek () Try java.util.Calendar.getFirstDayOfWeek()... then it's easy to calculate the last day of week: http://developer.android.com/reference/java/util/Calendar.html#getFirstDayOfWeek ()

I have written my own logic for this.我为此写了自己的逻辑。

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定当前日期和星期开始时间,获取星期开始和结束日期 - Get the week start and end date given a current date and week start 获取一周的开始日和结束日? [爪哇] - Get Start day and End day of a Week? [Java] 如何获取一个月中特定周的开始和结束日期 - how to get the start and end date of specific week of a month 通过在java中传递开始日期和结束日期来获取给定日期的周数 - Get week number of given date by passing start and end date in java 如何将日历周的开始和结束作为 LocalDateTime (java 8) - How to get the start and end of a calendar week as LocalDateTime (java 8) 在Java中获取当前周开始和结束日期 - (周一至周日) - Get current week start and end date in Java - (MONDAY TO SUNDAY) 从周/月/年获取开始日期和结束日期 - Get start date and end date from week/month/year 使用Java SE API和Joda Time(基于ISO)获取周开始时间戳和周结束时间戳 - Get week start timestamp and week end timestamp using Java SE API and Joda Time (based on ISO) 获取正确的星期开始日期和结束日期,当星期一,星期二在去年的最后一周 - Get correct week Start date and end date when monday , tuesday falls in last year last week 给定一个calendar.week_of_year,我如何获取一周开始和结束的日期? - Given a calendar.week_of_year, How do I get the dates for the start and end of the week?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM