如何获取一个月的Java日历的第一个星期三的日期 [英] How to get date of first Wednesday of a month java calendar

查看:1160
本文介绍了如何获取一个月的Java日历的第一个星期三的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用java日历类获取当月下一个第一个星期三的日期。例如:

How would I get the date of the next first Wednesday of the month using the java calendar class. For example:

Today(24/03/2012) the next first Wednesday will be 04/04/2012
On(05/04/2012) the next first Wednesday will be 02/05/2012

推荐答案

这项工作



  • 移动到下个月

  • 移至第一天

  • 到星期三

  • set start date
  • move to next month
  • move to first day in month
  • add days until we get to a wednesday

代码

import static java.util.Calendar.*;

public static void main(String[] args) {
    System.out.println(getNextMonthFirstWed(new Date(112, 3 - 1, 24)));
    System.out.println(getNextMonthFirstWed(new Date(112, 4 - 1, 05)));
}

private static Date getNextMonthFirstWed(Date date) {
    Calendar c = getInstance();
    c.setTime(date);
    c.add(MONTH, 1);
    c.set(DAY_OF_MONTH, 1);

    // search until wednesday
    while (c.get(DAY_OF_WEEK) != WEDNESDAY) {
        c.add(DAY_OF_MONTH, 1);
    }
    return c.getTime();
}

这篇关于如何获取一个月的Java日历的第一个星期三的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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