Java - How to calculate the first and last day of each week

前端 未结 11 852
难免孤独
难免孤独 2020-12-05 05:16

I\'m trying to create a weekly calendar that looks like this: http://dhtmlx.com/docs/products/dhtmlxScheduler/sample_basic.html

How can I calculate every week date?

相关标签:
11条回答
  • 2020-12-05 05:58

    Yes. Use Joda Time

    http://joda-time.sourceforge.net/

    0 讨论(0)
  • 2020-12-05 05:58

    The algorithm you're looking for (calculating the day of the week for any given date) is "Zeller's Congruence". Here's a Java implementation:

    http://technojeeves.com/joomla/index.php/free/57-zellers-congruence

    0 讨论(0)
  • 2020-12-05 05:59

    First day of this week.

        Calendar c = Calendar.getInstance();
        while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            c.add(Calendar.DATE, -1);
        }
    
    0 讨论(0)
  • 2020-12-05 05:59

    Simply setting the day of week does not seem to be reliable. Consider the following simple code:

    Calendar calendar = Calendar.getInstance(Locale.GERMANY);
    calendar.set(2011, Calendar.SEPTEMBER, 18);
    System.out.printf("Starting day: %tF%n", calendar);
    
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    System.out.printf("Last monday: %tF%n", calendar);
    System.out.printf("First day of week: %d%n", calendar.getFirstDayOfWeek());
    

    The result of running this program is:

    Starting day: 2011-09-18
    Last monday: 2011-09-19
    First day of week: 2
    

    In other words, it stepped forward in time. For a German locale, this is really not the expected answer. Note that the calendar correctly uses Monday as first day of the week (only for computing the week of the year, perhaps).

    0 讨论(0)
  • 2020-12-05 06:01

    With the new date and time API in Java 8 you would do:

    LocalDate now = LocalDate.now();
    
    // determine country (Locale) specific first day of current week
    DayOfWeek firstDayOfWeek = WeekFields.of(Locale.getDefault()).getFirstDayOfWeek();
    LocalDate startOfCurrentWeek = now.with(TemporalAdjusters.previousOrSame(firstDayOfWeek));
    
    // determine last day of current week
    DayOfWeek lastDayOfWeek = firstDayOfWeek.plus(6); // or minus(1)
    LocalDate endOfWeek = now.with(TemporalAdjusters.nextOrSame(lastDayOfWeek));
    
    // Print the dates of the current week
    LocalDate printDate = startOfCurrentWeek;
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE dd/MM/yyyy");
    for (int i=0; i < 7; i++) {
        System.out.println(printDate.format(formatter));
        printDate = printDate.plusDays(1);
    }
    
    0 讨论(0)
  • 2020-12-05 06:06

    You can build up on this: The following code prints the first and last dates of each week for 15 weeks from now.

    Calendar c = Calendar.getInstance();
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    for(int i=0; i<15; i++)
    {
        System.out.print("Start Date : " + c.getTime() + ", ");
        c.add(Calendar.DAY_OF_WEEK, 6);
        System.out.println("End Date : " + c.getTime());
        c.add(Calendar.DAY_OF_WEEK, 1);
    }
    
    0 讨论(0)
提交回复
热议问题