Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数据处理_Java_Groovy_Calendar - Fatal编程技术网

Java数据处理

Java数据处理,java,groovy,calendar,Java,Groovy,Calendar,给出了java.util.Date的一个实例,它是一个星期天。如果一个月内有4个星期日,我需要确定所选日期是否正确 每月的第一个星期日 每月第二个星期日 月的第二个最后星期日 本月最后一个星期日 如果这个月有5个星期天,那么我需要确定所选日期是否正确 每月的第一个星期日 每月第二个星期日 每月第三个星期日 月的第二个最后星期日 本月最后一个星期日 我已经查看了JodaTime,但是还没有找到相关的功能。该项目实际上是一个Groovy项目,因此我可以使用Java或Groovy来解决这个问题

给出了
java.util.Date
的一个实例,它是一个星期天。如果一个月内有4个星期日,我需要确定所选日期是否正确

  • 每月的第一个星期日
  • 每月第二个星期日
  • 月的第二个最后星期日
  • 本月最后一个星期日
如果这个月有5个星期天,那么我需要确定所选日期是否正确

  • 每月的第一个星期日
  • 每月第二个星期日
  • 每月第三个星期日
  • 月的第二个最后星期日
  • 本月最后一个星期日
我已经查看了JodaTime,但是还没有找到相关的功能。该项目实际上是一个Groovy项目,因此我可以使用Java或Groovy来解决这个问题

谢谢,
多纳尔

国际周日月=周日月/7+1

import java.sql.Date;
导入java.util.Calendar;
公共类道指{
公共静态void main(字符串[]args){
日期=新日期(2010年4月1日);
Calendar cal=Calendar.getInstance();
校准设定时间(日期);
对于(整数周=0;周<5;周++){
System.out.println(cal.getTime()+“week:+cal.get(Calendar.DAY/OF/u week/u IN/u MONTH));
cal.add(日历周/月,1);
}
}
}
>5月1日星期日00:00:00 CEST 3910周:1
>5月8日星期日00:00:00 CEST 3910周:2
>5月15日星期日00:00:00 CEST 3910周:3
>5月22日星期日00:00:00 CEST 3910周:4
>5月29日星期日00:00:00 CEST 3910周:5
更多编辑

好的,考虑到Jay的评论,让我们试试这个:

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";
}
。。。或者以其他方式。

您可以使用来计算日期:

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)

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

目前给出的解决方案将告诉您第1周、第2周、第3周、第4周或第5周。但问题是他想知道“最后”和“倒数第二”。如果提问者真的需要知道“最后一个”等,那么就有一点额外的复杂性。你必须计算出一个月有多少个星期天,才能知道第四个星期天是否是最后一个星期天。我想到的第一个办法是:

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;
GregorianCalendar gc=新的GregorianCalendar();
gc.设置时间(mydate);
gc.set(日历.月日,1);//回滚到月初
int weekday=gc.get(Calendar.DAY,OF,OF,OF,WEEK);//第一天的星期几
//查找月份的第一个星期日
第一个星期日;
如果(工作日==日历.星期日)
第一个星期日=1;
其他的
第一个星期日=工作日日历。星期日+6;
//第五个星期日当月的第几天(如有)
int第五个星期日=第一个星期日+7*4;
//查找月份的最后一天
gc.add(日历月,1);
gc.add(日历日期,-1);
int lastDay=gc.get(每月的第天);
国际星期日;

如果(第五个星期日)编辑:这是我的最终版本:

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]
}
today=Calendar.instance
dayOfWeekInMonth=today.get(Calendar.DAY\u OF\u WeekinMonth\u IN\u MONTH)
//移动到当月发生的每周最后一天
today.set(日历.日/周/月/月,-1)
maxDayOfWeekInMonth=today.get(Calendar.DAY\u OF\u WeekinMonth\u IN\u MONTH)
dayOfWeekCount=[“第一”、“第二”、“第三”、“最后第二”、“最后一个”]
转折点=maxDayOfWeekInMonth-2

如果(dayOfWeekInMonth应该是div而不是mod?3%7给出3,3/7给出0。是的-改变了。感谢Vincent仍然不可能有6个星期日。问题是“如何获得星期日”,而不是“如何获得星期日”,如果我做得对的话。只是一句小评论:这不完全是你打印的星期数。例如,如果星期日和2010-05-0开始2是星期天,不是已经是一个月的第二周了吗?@hudolejev你的权利,这是呼叫顺序号,如链接的javadoc中所描述的。真的没有一个答案对你有用吗?谢谢Jay,看起来你是唯一一个读过整个问题的人啊,+14,+7,非常干净的解决方案。不错。
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)
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;
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]
}