java 有没有什么好办法可以得到下周三的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3463756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a good way to get the date of the coming Wednesday?
提问by user256239
Is there a good way to get the date of the coming Wednesday? That is, if today is Tuesday, I want to get the date of Wednesday in this week; if today is Wednesday, I want to get the date of next Wednesday; if today is Thursday, I want to get the date of Wednesday in the following week.
有没有什么好办法可以得到下周三的日期?也就是说,如果今天是星期二,我想得到这周星期三的日期;如果今天是星期三,我想得到下星期三的日期;如果今天是星期四,我想在下周获得星期三的日期。
Thanks.
谢谢。
回答by polygenelubricants
The basic algorithm is the following:
基本算法如下:
- Get the current date
- Get its day of week
- Find its difference with Wednesday
- If the difference is not positive, add 7 (i.e. insist on next coming/future date)
- Add the difference
- 获取当前日期
- 获取它的星期几
- 找出它与星期三的区别
- 如果差值不是正数,加7(即坚持下一个来/未来日期)
- 添加差异
Here's a snippet to show how to do this with java.util.Calendar:
这是一个片段,用于展示如何使用java.util.Calendar:
import java.util.Calendar;
public class NextWednesday {
public static Calendar nextDayOfWeek(int dow) {
Calendar date = Calendar.getInstance();
int diff = dow - date.get(Calendar.DAY_OF_WEEK);
if (diff <= 0) {
diff += 7;
}
date.add(Calendar.DAY_OF_MONTH, diff);
return date;
}
public static void main(String[] args) {
System.out.printf(
"%ta, %<tb %<te, %<tY",
nextDayOfWeek(Calendar.WEDNESDAY)
);
}
}
Relative to my here and now, the output of the above snippet is "Wed, Aug 18, 2010".
相对于我的此时此地,上面代码片段的输出是"Wed, Aug 18, 2010".
API links
接口链接
java.util.Calendarjava.util.Formatter- for the formatting string syntax
java.util.Calendarjava.util.Formatter- 用于格式化字符串语法
回答by Unknown
tl;dr
tl;博士
LocalDate // Represent a date-only value, without time-of-day and without time zone.
.now() // Capture the current date as seen in the wall-clock time used by the people of a specific region (a time zone). The JVM's current default time zone is used here. Better to specify explicitly your desired/expected time zone by passing a `ZoneId` argument. Returns a `LocalDate` object.
.with( // Generate a new `LocalDate` object based on values of the original but with some adjustment.
TemporalAdjusters // A class that provides some handy pre-defined implementations of `TemporalAdjuster` (note the singular) interface.
.next( // An implementation of `TemporalAdjuster` that jumps to another date on the specified day-of-week.
DayOfWeek.WEDNESDAY // Pass one of the seven predefined enum objects, Monday-Sunday.
) // Returns an object implementing `TemporalAdjuster` interface.
) // Returns a `LocalDate` object.
Details
细节
Using Java8 Date time APIyou can easily find the coming Wednesday.
使用Java8 日期时间 API,您可以轻松找到即将到来的星期三。
LocalDate nextWed = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));
next(DayOfWeek dayOfWeek) - Returns the next day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted.
next(DayOfWeek dayOfWeek) - 返回下一个星期调整器,它将日期调整为调整日期后指定星期几的第一次出现。
Suppose If you want to get previous Wednesday then,
假设如果你想获得上一个星期三,那么
LocalDate prevWed = LocalDate.now().with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
previous(DayOfWeek dayOfWeek) - Returns the previous day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week before the date being adjusted.
previous(DayOfWeek dayOfWeek) - 返回上一个星期调整器,它将日期调整为在调整日期之前指定星期几的第一次出现。
Suppose If you want to get next or current Wednesday then
假设如果你想得到下一个或当前的星期三,那么
LocalDate nextOrSameWed = LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));
nextOrSame(DayOfWeek dayOfWeek) - Returns the next-or-same day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted unless it is already on that day in which case the same object is returned.
nextOrSame(DayOfWeek dayOfWeek) - 返回下一个或同一个星期的调整器,它将日期调整到被调整的日期之后指定星期几的第一次出现,除非它已经在那个日期如果返回相同的对象。
Edit: You can also pass ZoneId to get the current date from the system clock in the specified time-zone.
编辑:您还可以通过 ZoneId 从指定时区的系统时钟获取当前日期。
ZoneId zoneId = ZoneId.of("UTC");
LocalDate nextWed = LocalDate.now(zoneId).with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));
For more information refer TemporalAdjusters
有关更多信息,请参阅TemporalAdjusters
回答by Yishai
Using JodaTime:
使用 JodaTime:
LocalDate date = new LocalDate(System.currentTimeMillis());
Period period = Period.fieldDifference(date, date.withDayOfWeek(DateTimeConstants.WEDNESDAY));
int days = period.getDays();
if (days < 1) {
days = days + 7;
}
System.out.println(date.plusDays(days));
回答by EboMike
Use java.util.Calendar. You get the current date/time like this:
使用 java.util.Calendar。您可以像这样获得当前日期/时间:
Calendar date = Calendar.getInstance();
From there, get date.get(Calendar.DAY_OF_WEEK)to get the current day of week and get the difference to Calendar.WEDNESDAYand add it.
从那里,获取date.get(Calendar.DAY_OF_WEEK)当前星期几并获取差异Calendar.WEDNESDAY并添加它。
回答by Utsav Gupta
Calendar c= Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
c.add(Calendar.DAY_OF_MONTH, 7);
c.getTime();
回答by Soubhab Pathak
public static void nextWednesday() throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
int days = Calendar.WEDNESDAY - weekday;
if (days < 0)
{
days += 7;
}
calendar.add(Calendar.DAY_OF_YEAR, days);
System.out.println(sdf.format(calendar.getTime()));
}

