有没有好的方法来获取下周三的日期?

发布于 2024-09-13 13:58:25 字数 103 浏览 5 评论 0原文

有没有好的方法来获取下周三的日期? 即如果今天是星期二,我想获取本周星期三的日期;如果今天是星期三,我想获取下星期三的日期;如果今天是星期四,我想获取下周星期三的日期。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦旅人picnic 2024-09-20 13:58:25

基本算法如下:

  • 获取当前日期
  • 获取星期几
  • 查找与星期三的差异
  • 如果差异不是正数,则加 7(即坚持下一个到来/未来的日期)
  • 添加差异

这是一个片段,展示如何使用 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)
        );
    }
}

相对于我此时此地,上述代码片段的输出是“Wed, Aug 18, 2010”。

API 链接

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

Here's a snippet to show how to do this with 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".

API links

蛮可爱 2024-09-20 13:58:25

tl;dr

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.

使用

Java8 日期时间 API 您可以轻松找到即将到来的星期三。

LocalDate nextWed = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));

next(DayOfWeek dayOfWeek) - 返回下一个星期调整器,用于调整日期
到该日期之后指定星期几的第一次出现
正在调整中。

假设如果你想得到上周三,

LocalDate prevWed = LocalDate.now().with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));

previous(DayOfWeek dayOfWeek) - 返回上一个星期调整器,该调整器会进行调整
指定星期几之前第一次出现的日期
正在调整的日期。

假设如果你想得到下一个或当前的星期三然后

LocalDate nextOrSameWed = LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));

nextOrSame(DayOfWeek dayOfWeek) - 返回一周中的下一天或同一天
调整器,将日期调整为第一次出现的日期
调整日期后指定的星期几,除非
已经在那天,在这种情况下,将返回相同的对象。

编辑:
您还可以通过 ZoneId 来获取指定时区的系统时钟的当前日期。

ZoneId zoneId = ZoneId.of("UTC");
LocalDate nextWed = LocalDate.now(zoneId).with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));

有关更多信息,请参阅 TemporalAdjusters

tl;dr

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 API you can easily find the coming Wednesday.

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.

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.

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.

Edit:
You can also pass ZoneId to get the current date from the system clock in the specified time-zone.

ZoneId zoneId = ZoneId.of("UTC");
LocalDate nextWed = LocalDate.now(zoneId).with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));

For more information refer TemporalAdjusters

蒗幽 2024-09-20 13:58:25
Calendar c= Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
c.add(Calendar.DAY_OF_MONTH, 7);
c.getTime();
Calendar c= Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
c.add(Calendar.DAY_OF_MONTH, 7);
c.getTime();
冬天的雪花 2024-09-20 13:58:25

使用java.util.Calendar。您可以这样获取当前日期/时间:

Calendar date = Calendar.getInstance();

从那里,获取 date.get(Calendar.DAY_OF_WEEK) 来获取当前星期几并获取与 Calendar.WEDNESDAY 的差异> 并添加它。

Use java.util.Calendar. You get the current date/time like this:

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.WEDNESDAY and add it.

吾性傲以野 2024-09-20 13:58:25

使用 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));

Using 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));
‘画卷フ 2024-09-20 13:58:25
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()));
    }
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()));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文