如何使用Joda-Time API获取下周五。
今天的LocalDate
是today
。在我看来,你必须在本周五之前或之后决定你是谁。请参阅此方法:
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);
}
}
是否可以缩短或使用oneliner?
PS:请不要建议我使用JDK的日期/时间。 Joda-Time是一个更好的API。
答案 0 :(得分:60)
使用{8}内置的java.time框架(Tutorial),您可以使用TemporalAdjusters
获取next或previous day-of-week
private LocalDate calcNextFriday(LocalDate d) {
return d.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
}
答案 1 :(得分:52)
可以以更容易阅读的方式进行:
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);
}
PS。我没有测试实际代码! :)
答案 2 :(得分:5)
您的代码在一行
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);
}
有点测试过; - )
答案 3 :(得分:4)
我只是浪费了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;
}
现在您只需要为星期几制作一个Partial(LocalDate)。
Partial().with(DateTimeFieldType.dayOfWeek(), DateTimeConstants.FRIDAY);
现在,如果当前日期在(现在)之后,那么部分最重要的字段将是+1。
即如果您在2012年3月部分参与,它将创建2013年3月的新日期时间或&lt;。
答案 4 :(得分:4)
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
}
答案 5 :(得分:3)
计算字节@fvu的答案可以进一步缩短为:
private LocalDate calcNextFriday(LocalDate d) {
return d.plusWeeks(d.getDayOfWeek() < DateTimeConstants.FRIDAY ? 0 : 1).withDayOfWeek(DateTimeConstants.FRIDAY);
}
答案 6 :(得分:0)
一个简单的基于模的解决方案,可以与大多数以前的Java版本一起使用,以防您不允许将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()));
}
}
答案 7 :(得分:0)
这是一种替代方法,无需使用 LocalDate 之外的任何其他类/API。
LocalDate nextFriday = (d.plusWeeks(1).withDayOfWeek(5).minusDays(7).isAfter(d))
? d.plusWeeks(1).withDayOfWeek(5)
: d.withDayOfWeek(5);