Java 日期处理

发布于 2024-09-02 14:38:14 字数 409 浏览 4 评论 0原文

给定一个 java.util.Date 实例,它是星期日。如果该月有 4 个星期日,我需要弄清楚所选日期是否为该

  • 月的第 1
  • 个星期日、该月的第 2 个星期日、该
  • 月的最后一个星期日、
  • 该月的最后一个星期日

如果该月有 5 个星期日,则我需要弄清楚所选日期是否是

  • 该月的第一个星期日
  • 该月的第二个
  • 星期日 该月的第三
  • 个星期日 该月的第二个最后一个星期日
  • 该月的最后一个星期日

我在JodaTime中查看过,但还没有找到相关功能。该项目实际上是一个 Groovy 项目,所以我可以使用 Java 或 Groovy 来解决这个问题。

谢谢, 多纳尔

Given an instance of java.util.Date which is a Sunday. If there are 4 Sundays in the month in question, I need to figure out whether the chosen date is

  • 1st Sunday of month
  • 2nd Sunday of month
  • 2nd last Sunday of month
  • Last Sunday of month

If there are 5 Sundays in the month in question, then I need to figure out whether the chosen date is

  • 1st Sunday of month
  • 2nd Sunday of month
  • 3rd Sunday of month
  • 2nd last Sunday of month
  • Last Sunday of month

I've had a look in JodaTime, but haven't found the relevant functionality there yet. The project is actually a Groovy project, so I could use either Java or Groovy to solve this.

Thanks,
Donal

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

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

发布评论

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

评论(6

清晨说晚安 2024-09-09 14:38:14

到目前为止给出的解决方案将告诉您第一、第二、第三、第四或第五。但问题说他想知道“最后”和“倒数第二”。如果提问者确实需要知道“最后”等,那么就会有一点额外的复杂性。您必须计算出该月中有多少个星期日才能知道第四个星期日是否是最后一个。我想到的第一个方法是:

GregorianCalendar gc=new GregorianCalendar();
gc.setTime(mydate);
gc.set(Calendar.DAY_OF_MONTH,1); // Roll back to first of month
int weekday=gc.get(Calendar.DAY_OF_WEEK); // day of week of first day
// Find day of month of first Sunday
int firstSunday;
if (weekday==Calendar.SUNDAY)
  firstSunday=1;
else
  firstSunday=weekday-Calendar.SUNDAY+6;
// Day of month of fifth Sunday, if it exists
int fifthSunday=firstSunday+7*4;
// Find last day of month
gc.add(Calendar.MONTH,1);
gc.add(Calendar.DATE,-1);
int lastDay=gc.get(DAY_OF_MONTH);

int sundays;
if (fifthSunday<lastDay)
  sundays=4;
else
  sundays=5;

这似乎是一项相当多的工作。有人看到更简单的方法吗?

Solutions given so far will tell you 1st, 2nd, 3rd, 4th, or 5th. But the question said he wanted to know "last" and "second to last". If the questioner really needs to know "last" etc, there's a little extra complexity. You have to figure out how many Sundays are in the month to know whether or not the 4th Sunday is the last. The first way to figure that out that occurs to me is:

GregorianCalendar gc=new GregorianCalendar();
gc.setTime(mydate);
gc.set(Calendar.DAY_OF_MONTH,1); // Roll back to first of month
int weekday=gc.get(Calendar.DAY_OF_WEEK); // day of week of first day
// Find day of month of first Sunday
int firstSunday;
if (weekday==Calendar.SUNDAY)
  firstSunday=1;
else
  firstSunday=weekday-Calendar.SUNDAY+6;
// Day of month of fifth Sunday, if it exists
int fifthSunday=firstSunday+7*4;
// Find last day of month
gc.add(Calendar.MONTH,1);
gc.add(Calendar.DATE,-1);
int lastDay=gc.get(DAY_OF_MONTH);

int sundays;
if (fifthSunday<lastDay)
  sundays=4;
else
  sundays=5;

That seems like rather a lot of work. Anybody see an easier way?

偏爱你一生 2024-09-09 14:38:14

DAY_OF_WEEK_IN_MONTH

import java.sql.Date;
import java.util.Calendar;

public class Dow {
    public static void main(String[] args) {
        Date date = new Date( 2010, 4, 1 );
        Calendar cal = Calendar.getInstance();
        cal.setTime( date );
        for ( int week = 0 ; week < 5 ; week++ ) {
            System.out.println( cal.getTime() + " week:" + cal.get( Calendar.DAY_OF_WEEK_IN_MONTH ) );
            cal.add( Calendar.WEEK_OF_MONTH, 1 );
        }
    }
}


> Sun May 01 00:00:00 CEST 3910 week:1
> Sun May 08 00:00:00 CEST 3910 week:2
> Sun May 15 00:00:00 CEST 3910 week:3
> Sun May 22 00:00:00 CEST 3910 week:4
> Sun May 29 00:00:00 CEST 3910 week:5

DAY_OF_WEEK_IN_MONTH

import java.sql.Date;
import java.util.Calendar;

public class Dow {
    public static void main(String[] args) {
        Date date = new Date( 2010, 4, 1 );
        Calendar cal = Calendar.getInstance();
        cal.setTime( date );
        for ( int week = 0 ; week < 5 ; week++ ) {
            System.out.println( cal.getTime() + " week:" + cal.get( Calendar.DAY_OF_WEEK_IN_MONTH ) );
            cal.add( Calendar.WEEK_OF_MONTH, 1 );
        }
    }
}


> Sun May 01 00:00:00 CEST 3910 week:1
> Sun May 08 00:00:00 CEST 3910 week:2
> Sun May 15 00:00:00 CEST 3910 week:3
> Sun May 22 00:00:00 CEST 3910 week:4
> Sun May 29 00:00:00 CEST 3910 week:5
|煩躁 2024-09-09 14:38:14
return dayOfMonth / 7 + 1;

更多编辑

好的,考虑到杰伊的评论,让我们尝试一下这个:

int sundayId = dayOfMonth / 7 + 1;
if (sundayId == 1) {
    return "1st";
} else if (sundayId == 2) {
    return "2nd";
} else if (sundayId == 3) {
    return ((dayOfMonth + 14 <= daysInMonth) ? "3rd" : "2nd Last");
} else if (sundayId == 4) {
    return ((dayOfMonth + 7 <= daysInMonth) ? "2nd Last" : "Last");
} else {
    return "Last";
}

daysInMonth 的计算如下:

if (month == 2) {
    if (year % 100) {
        return ((year % 400 == 0) ? 29 : 28);
    } else {
        return ((year % 4 == 0) ? 29 : 28);
    }
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
    return 30;
} else {
    return 31;
}

...或以其他方式计算。

return dayOfMonth / 7 + 1;

MORE EDIT

Okay, taking Jay's comment in consideration, let's try this one:

int sundayId = dayOfMonth / 7 + 1;
if (sundayId == 1) {
    return "1st";
} else if (sundayId == 2) {
    return "2nd";
} else if (sundayId == 3) {
    return ((dayOfMonth + 14 <= daysInMonth) ? "3rd" : "2nd Last");
} else if (sundayId == 4) {
    return ((dayOfMonth + 7 <= daysInMonth) ? "2nd Last" : "Last");
} else {
    return "Last";
}

whereas daysInMonth is calculated as follows:

if (month == 2) {
    if (year % 100) {
        return ((year % 400 == 0) ? 29 : 28);
    } else {
        return ((year % 4 == 0) ? 29 : 28);
    }
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
    return 30;
} else {
    return 31;
}

... or in some other way.

孤云独去闲 2024-09-09 14:38:14

int 月份星期日 = 月份日 / 7 + 1;

int sundayOfMonth = dayOfMonth / 7 + 1;

z祗昰~ 2024-09-09 14:38:14

您可以使用 java.util.Calendar< /a> 进行日期计算:

import java.util.Calendar as CAL

def cal = CAL.getInstance(date)
def isFirst= cal.get(CAL.WEEK_OF_MONTH) == 1
def isSecond = cal.get(CAL.WEEK_OF_MONTH) == cal.getMaximum(CAL.WEEK_OF_MONTH)

另外,不要假设 Calendar 对象一个月中只有五周。例如,2010 年 5 月 1 日的最大周数为 6。

You can just use java.util.Calendar to do your date calculations:

import java.util.Calendar as CAL

def cal = CAL.getInstance(date)
def isFirst= cal.get(CAL.WEEK_OF_MONTH) == 1
def isSecond = cal.get(CAL.WEEK_OF_MONTH) == cal.getMaximum(CAL.WEEK_OF_MONTH)

Also, do not assume that there are only five weeks in a month with the Calendar object. For instance, the maximum weeks of 05/01/2010 would be 6.

三人与歌 2024-09-09 14:38:14

编辑:这是我的最终版本:

today = Calendar.instance
dayOfWeekInMonth = today.get(Calendar.DAY_OF_WEEK_IN_MONTH)

// move to last day of week occurrence in current month
today.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1)
maxDayOfWeekInMonth = today.get(Calendar.DAY_OF_WEEK_IN_MONTH)

dayOfWeekCount = ["1st", "2nd", "3rd", "2nd last", "Last"]

turningPoint = maxDayOfWeekInMonth - 2
if (dayOfWeekInMonth <= turningPoint) {
  println dayOfWeekCount[dayOfWeekInMonth - 1]
}
else {
  // use negative index to access list from last to first
  println dayOfWeekCount[dayOfWeekInMonth - maxDayOfWeekInMonth - 1]
}

EDIT: Here's my final version:

today = Calendar.instance
dayOfWeekInMonth = today.get(Calendar.DAY_OF_WEEK_IN_MONTH)

// move to last day of week occurrence in current month
today.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1)
maxDayOfWeekInMonth = today.get(Calendar.DAY_OF_WEEK_IN_MONTH)

dayOfWeekCount = ["1st", "2nd", "3rd", "2nd last", "Last"]

turningPoint = maxDayOfWeekInMonth - 2
if (dayOfWeekInMonth <= turningPoint) {
  println dayOfWeekCount[dayOfWeekInMonth - 1]
}
else {
  // use negative index to access list from last to first
  println dayOfWeekCount[dayOfWeekInMonth - maxDayOfWeekInMonth - 1]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文