有什么好的方法来得到下周三的日期? [英] Is there a good way to get the date of the coming Wednesday?

查看:207
本文介绍了有什么好的方法来得到下周三的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么好的方法来得到下周三的日期?
也就是说,如果今天是星期二,我想获得本周三的日期;如果今天是星期三,我想得到下周三的日期;如果今天是星期四,我想在下周获得星期三的日期。

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.

谢谢。

推荐答案

基本算法如下:


  • 获取当前日期
  • $ b $ 未来日期)
  • 添加差异

  • 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

这里有一个代码段,这与 java.util.Calendar

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)
        );
    }
}

相对于我这里和现在,以上代码段2010年8月18日星期三

Relative to my here and now, the output of the above snippet is "Wed, Aug 18, 2010".

  • java.util.Calendar
  • java.util.Formatter - for the formatting string syntax

这篇关于有什么好的方法来得到下周三的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆