日历周日至周日

时间:2016-12-05 16:40:48

标签: java

我试图从周日到下周日开始8天的航班。

我现在实现它的方法是显示从我的表单开始所选日期的7天。

// set up calendar for sunday
Calendar sunday = Calendar.getInstance();
sunday.setTime(form.getDate());
sunday.add(Calendar.DAY_OF_WEEK, -1 * (sunday.get(Calendar.DAY_OF_WEEK) - 1));

//set up calendar for next saturday
Calendar saturday = Calendar.getInstance();
saturday.setTime(sunday.getTime());
saturday.add(Calendar.DAY_OF_WEEK, 7);

由于DAY_OF_WEEK的最大值为7,我需要使用什么? 我尝试改变这一行:

saturday.add(Calendar.DAY_OF_WEEK, 7);

以下一个:

saturday.add(Calendar.DATE, 8);

我已经尝试了几次改变,但没有运气。  有什么建议吗?

2 个答案:

答案 0 :(得分:2)

尝试使用Calendar.DAY_OF_YEAR

import java.text.*;
import java.util.*;

DateFormat dateFormat = new SimpleDateFormat("EEE, MMM dd, yyyy hh:mm:ss a z");
Calendar sunday = new GregorianCalendar();

sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // Set day of week to Sunday.

System.out.println(dateFormat.format(sunday.getTime()));

sunday.add(Calendar.DAY_OF_YEAR, 7); // Add seven days.

System.out.println(dateFormat.format(sunday.getTime()));

输出

Sun, Dec 04, 2016 11:47:32 PM EST
Sun, Dec 11, 2016 11:47:32 PM EST

您可以通过制作副本来创建新的日历对象而无需修改现有日历对象。

import java.text.*;
import java.util.*;

public class CalendarUtils {
    public static void main(String[] args) {
        Calendar sunday = CalendarUtils.getThisSundaysDate();
        Calendar saturday = CalendarUtils.daysFrom(sunday, 6);

        CalendarUtils.printDates(sunday, saturday);
    }

    public static Calendar getThisSundaysDate() {
        Calendar sunday = new GregorianCalendar();
        sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sunday;
    }

    public static Calendar daysFrom(Calendar cal, int days) {
        Calendar newCal = copyCalendar(cal);
        newCal.add(Calendar.DAY_OF_YEAR, days);
        return newCal;
    }

    public static Calendar copyCalendar(Calendar cal) {
        Calendar copy = new GregorianCalendar();
        copy.setTime(cal.getTime());
        return copy;
    }

    public static void printDates(Calendar from, Calendar to) {
        DateFormat dateFormat = new SimpleDateFormat("EEE, MMMM dd, yyyy hh:mm:ss a z");

        System.out.println(dateFormat.format(from.getTime()));
        System.out.println(dateFormat.format(to.getTime()));
    }
}

答案 1 :(得分:2)

如果您正在使用Java 8,则应使用新的java.time类。在这种情况下,您希望使用LocalDate课程和TemporalAdjusters课程,并使用previous(DayOfWeek)方法。

或者,使用previousOrSame(DayOfWeek),具体取决于参考日期是星期日会发生什么。

示例,使用今天作为参考日期:

LocalDate refDate = LocalDate.now();
LocalDate prevSunday = refDate.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY));
LocalDate nextSunday = prevSunday.plusDays(7);

DateTimeFormatter fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println("Ref. Date:    " + refDate.format(fmt));
System.out.println("Prev. Sunday: " + prevSunday.format(fmt));
System.out.println("Next. Sunday: " + nextSunday.format(fmt));

输出

Ref. Date:    Monday, December 5, 2016
Prev. Sunday: Sunday, December 4, 2016
Next. Sunday: Sunday, December 11, 2016