时间: 下个星期五怎么办?

我怎样才能得到下个星期五与 Joda 时间 API。

今天的 LocalDatetoday。在我看来,你必须在本周五之前或之后决定自己的身份。看看这个方法:

private LocalDate calcNextFriday(LocalDate d) {
LocalDate friday = d.dayOfWeek().setCopy(5);
if (d.isBefore(friday)) {
return d.dayOfWeek().setCopy(5);
} else {
return d.plusWeeks(1).dayOfWeek().setCopy(5);
}
}

有没有可能画得更短一些,或者画个线条?

PS: 请不要建议我使用 JDK 的日期/时间的东西。 Joda-Time 是一个更好的 API。

Java8引入了更好的 爪哇时间包(教程)。

43825 次浏览

可以用一种更容易理解的方式来做:

if (d.getDayOfWeek() < DateTimeConstants.FRIDAY) {
return d.withDayOfWeek(DateTimeConstants.FRIDAY));
} else if (d.getDayOfWeek() == DateTimeConstants.FRIDAY) {
// almost useless branch, could be merged with the one above
return d;
} else {
return d.plusWeeks(1).withDayOfWeek(DateTimeConstants.FRIDAY));
}

或者短一点的形式

private LocalDate calcNextFriday(LocalDate d) {
if (d.getDayOfWeek() < DateTimeConstants.FRIDAY) {
d = d.withDayOfWeek(DateTimeConstants.FRIDAY));
} else {
d = d.plusWeeks(1).withDayOfWeek(DateTimeConstants.FRIDAY));
}
return d; // note that there's a possibility original object is returned
}

甚至更短

private LocalDate calcNextFriday(LocalDate d) {
if (d.getDayOfWeek() >= DateTimeConstants.FRIDAY) {
d = d.plusWeeks(1);
}
return d.withDayOfWeek(DateTimeConstants.FRIDAY);
}

另外,我没有测试实际的代码! :)

你的代码在一行

private LocalDate calcNextFriday3(LocalDate d) {
return d.isBefore(d.dayOfWeek().setCopy(5))?d.dayOfWeek().setCopy(5):d.plusWeeks(1).dayOfWeek().setCopy(5);
}

另一种方法

private LocalDate calcNextDay(LocalDate d, int weekday) {
return (d.getDayOfWeek() < weekday)?d.withDayOfWeek(weekday):d.plusWeeks(1).withDayOfWeek(weekday);
}




private LocalDate calcNextFriday2(LocalDate d) {
return calcNextDay(d,DateTimeConstants.FRIDAY);
}

有些经受过考验; -)

我只是浪费了大约30分钟试图自己弄清楚这一点,但我需要通常向前推进。

不管怎样,我的解决办法是:

public static DateTime rollForwardWith(ReadableInstant now, AbstractPartial lp) {
DateTime dt = lp.toDateTime(now);
while (dt.isBefore(now)) {
dt = dt.withFieldAdded(lp.getFieldTypes()[0].getRangeDurationType(), 1);
}
return dt;
}

现在您只需要为一周中的某一天做一个部分(LocalDate)。

Partial().with(DateTimeFieldType.dayOfWeek(), DateTimeConstants.FRIDAY);

现在,如果当前日期在这个部分之后(现在) ,那么无论这个部分最重要的字段是什么,它都将是 + 1。

也就是说,如果你使用2012年3月的部分时间,它将创建一个新的日期时间为2013年3月或 < 。

计数字节@fvu 答案可进一步缩短为:

private LocalDate calcNextFriday(LocalDate d) {
return d.plusWeeks(d.getDayOfWeek() < DateTimeConstants.FRIDAY ? 0 : 1).withDayOfWeek(DateTimeConstants.FRIDAY);
}

爪哇时间

使用内置在 Java8及更高版本(教程)中的 爪哇时间框架,您可以使用 TemporalAdjusters获得 下一个上一页每周一天

private LocalDate calcNextFriday(LocalDate d) {
return d.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
}
import java.util.Calendar;


private Calendar getNextweekOfDay(int weekOfDay) {
Calendar today = Calendar.getInstance();
int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
int daysUntilNextWeekOfDay = weekOfDay - dayOfWeek;
if (daysUntilNextWeekOfDay == 0) daysUntilNextWeekOfDay = 7;
Calendar nextWeekOfDay = (Calendar)today.clone();
nextWeekOfDay.add(Calendar.DAY_OF_WEEK, daysUntilNextWeekOfDay);
return nextWeekOfDay;
}


// set alarm for next Friday 9am
public void setAlarm() {
Calendar calAlarm = getNextweekOfDay(Calendar.FRIDAY);
calAlarm.set(Calendar.HOUR_OF_DAY, 9);//9am
calAlarm.set(Calendar.MINUTE, 0);
calAlarm.set(Calendar.SECOND, 0);
scheduleAlarm(calAlarm);// this is my own method to schedule a pendingIntent
}

一个简单的基于模块的解决方案,应该与大多数前 Java 版本的情况下,你不允许升级到 java8或更高版本,或使用标准的 java 日期库作为 jodatime

以下公式给出了你的日期增加的天数:

(7 + Calendar.Friday-yourDateAsCalendar.get (Calendar.DAY _ OF _ Week))% 7

还要注意的是,这可以通过将静态字段 Calendar.FRIDAY 更改为给定的工作日来将其推广到任何工作日。下面是一些代码片段

public static void main(String[] args) {
for (int i = 0; i < 15; i++) {


Calendar cur = Calendar.getInstance();
cur.add(Calendar.DAY_OF_MONTH, i);


Calendar friday = Calendar.getInstance();
friday.setTime(cur.getTime());
friday.add(Calendar.DAY_OF_MONTH, (7 + Calendar.FRIDAY - cur.get(Calendar.DAY_OF_WEEK)) % 7);


System.out.println(MessageFormat.format("Date {0} -> {1} ", cur.getTime(),  friday.getTime()));
}
}

这里有一种替代方法,不使用 LocalDate 之外的任何其他类/API。

     LocalDate nextFriday = (d.plusWeeks(1).withDayOfWeek(5).minusDays(7).isAfter(d))
? d.plusWeeks(1).withDayOfWeek(5)
: d.withDayOfWeek(5);