Get date of first day of week based on LocalDate.now() in Java 8

前端 未结 9 1494
梦谈多话
梦谈多话 2020-12-02 19:35

I would like the get the date of the first day of the week based on LocalDate.now(). The following was possible with JodaTime, but seems to be removed from the new Date API

相关标签:
9条回答
  • 2020-12-02 20:30

    It works for me in case if I want to get Monday as a first day of current week:

    LocalDate mondayDate = LocalDate.now().with(WeekFields.of(Locale.FRANCE).getFirstDayOfWeek());
    
    0 讨论(0)
  • 2020-12-02 20:31

    The LocalDate doesn't seem to have this, but WeekFields (which is from the java-8 API) does (here). So you can do this:

    WeekFields.of(Locale.getDefault()).firstDayOfWeek.value
    

    This returns the value of the first day of week, starting from 1 as Monday, ending with 7 as Sunday.

    Example of usage (in Kotlin), to get a new LocalDate that has the dayOfWeek be set, going backward in time instead of forward:

    /**@param targetDayOfWeek day of week to go to, starting from 1 as Monday (and 7 is Sunday) */
    fun LocalDate.minusDaysToDayOfWeek(targetDayOfWeek: Int = WeekFields.of(Locale.getDefault()).firstDayOfWeek.value): LocalDate {
        //conversion so that Sunday is 0, Monday is 1, etc:
        val diffDays = (dayOfWeek.value % 7) - (targetDayOfWeek % 7)
        val result = when {
            diffDays == 0 -> this
            diffDays < 0 -> minusDays((7 + diffDays).toLong())
            else -> minusDays(diffDays.toLong())
        }
        return result
    }
    

    Example inputs-outputs :

    2017-12-31 -> 2017-12-31
    2018-01-01 -> 2017-12-31
    2018-01-02 -> 2017-12-31
    2018-01-03 -> 2017-12-31
    2018-01-04 -> 2017-12-31
    2018-01-05 -> 2017-12-31
    2018-01-06 -> 2017-12-31
    2018-01-07 -> 2018-01-07
    2018-01-08 -> 2018-01-07
    2018-01-09 -> 2018-01-07
    2018-01-10 -> 2018-01-07
    2018-01-11 -> 2018-01-07
    2018-01-12 -> 2018-01-07
    2018-01-13 -> 2018-01-07
    2018-01-14 -> 2018-01-14
    2018-01-15 -> 2018-01-14
    

    And here's a sample function to go forward in time to the target day of week:

    fun LocalDate.plusDaysToDayOfWeek(targetDayOfWeek: Int = getLastDayOfWeek()): LocalDate {
        val diffDays = (targetDayOfWeek % 7) - (dayOfWeek.value % 7)
        val result = when {
            diffDays == 0 -> this
            diffDays < 0 -> plusDays((7 + diffDays).toLong())
            else -> plusDays(diffDays.toLong())
        }
        return result
    }
    
    /**@return the  last day of week, when 1 is Monday ... 7 is Sunday) */
    @JvmStatic
    fun getLastDayOfWeek(firstDayOfWeek: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek): Int {
        return when (firstDayOfWeek) {
            DayOfWeek.MONDAY -> DayOfWeek.SUNDAY.value
            else -> firstDayOfWeek.value - 1
        }
    }
    

    BTW, weird thing is that I think the code behind the scenes of the new API actually uses Calendar class anyway...

    In case you hate using the dayOfWeek as used for LocalDate (as I do), and you prefer the one used with Calendar, you can use these simple converters:

    fun DayOfWeek.toCalendarDayOfWeek(): Int {
        return when (this) {
            DayOfWeek.SATURDAY -> Calendar.SATURDAY
            else -> (this.value + 1) % 7
        }
    }
    
    @JvmStatic
    fun convertLocalDateDayOfWeekToCalendarDayOfWeek(localDateDayOfWeek: Int): Int {
        return when (localDateDayOfWeek) {
            DayOfWeek.SATURDAY.value -> Calendar.SATURDAY
            else -> (localDateDayOfWeek + 1) % 7
        }
    }
    
    @JvmStatic
    fun convertFromCalendarDayOfWeekToLocalDateDayOfWeek(calendarDayOfWeek: Int): Int {
        return when (calendarDayOfWeek) {
            Calendar.SUNDAY -> DayOfWeek.SUNDAY.value
            else -> calendarDayOfWeek - 1
        }
    }
    
    0 讨论(0)
  • 2020-12-02 20:31
    public void getWeekFromADateOfAMonth(){
        String date = "2019-01-02T18:25:43.511Z";
        TemporalField fieldISO = WeekFields.of(Locale.US).dayOfWeek();
        ZonedDateTime dateTime = ZonedDateTime.parse(date);
    
        int week = dateTime.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );
        int weekYear = dateTime.get ( IsoFields.WEEK_BASED_YEAR );
    
        System.out.println ( "now: " + dateTime + " is week: " + week + " of weekYear: " + weekYear );
    
        int startDate = dateTime.with(fieldISO,1).getDayOfMonth();
        int endDate = dateTime.with(fieldISO,7).getDayOfMonth();
    
        String startMonth = String.valueOf(dateTime.with(fieldISO,1).getMonth());
        String endMonth = String.valueOf(dateTime.with(fieldISO,7).getMonth());
    }
    
    0 讨论(0)
提交回复
热议问题