在Java中的日期范围内获取所有星期五 [英] Get all Fridays in a date Range in Java

查看:138
本文介绍了在Java中的日期范围内获取所有星期五的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个任务,我必须在一个日期范围内得到所有的星期五。我写了一小段代码,惊讶地看到一些奇怪的行为。



以下是我的代码:

  public class Friday {
public static void main(String [] args){
String start =01/01/2009;
String end =12/09/2013​​;
String [] startTokens = start.split(/);
String [] endTokens = end.split(/);
日历startCal = new GregorianCalendar(Integer.parseInt(startTokens [2]),Integer.parseInt(startTokens [1]) - 1,Integer.parseInt(startTokens [0]));
日历endCal = new GregorianCalendar(Integer.parseInt(endTokens [2]),Integer.parseInt(endTokens [1]) - 1,Integer.parseInt(endTokens [0]));

int startYear = Integer.parseInt(startTokens [2]);
int endYear = Integer.parseInt(endTokens [2]);


int startWeek = startCal.get(Calendar.WEEK_OF_YEAR);
int endWeek = endCal.get(Calendar.WEEK_OF_YEAR);

日历cal = new GregorianCalendar();
cal.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
// cal.setMinimalDaysInFirstWeek(7);
ArrayList< String> main = new ArrayList< String>();
while(startYear< = endYear){
cal.set(Calendar.YEAR,startYear);
System.out.println(cal.getMinimalDaysInFirstWeek());
if(startYear == endYear){
main.addAll(getFridays(startWeek,endWeek,cal));
}
else {
main.addAll(getFridays(startWeek,52,cal));
startWeek = 1;
}
startYear = startYear +1;
}

for(String s:main){
System.err.println(s);
}
}
public static ArrayList< String> getFridays(int startWeek,int endWeek,Calendar cal){
ArrayList< String> fridays = new ArrayList< String>();
while(startWeek< = endWeek){
cal.set(Calendar.WEEK_OF_YEAR,startWeek);
fridays.add(cal.getTime()。toString());
startWeek = startWeek +1
}
返回星期五;
}
}

现在,当我运行代码时,我注意到星期五2011年失踪经过一些调试和在线浏览,我认为 Calendar.WEEK_OF_YEAR 是区域设置,我必须使用 setMinimalDaysInFirstWeek(7)修改它。



所以取消注释上面的代码中的相关行。



从我以前了解的第一个每周的一周应从一整个星期开始。



例如2010年1月1日星期五。但是,由于我配置了这个星期从1月3日开始,所以不应该显示在结果中。但现在我仍然看到1月1日星期五



我非常困惑。有些人可以解释为什么会发生这种情况吗?



这些Stackoverflow文章帮助了我一些:



为什么2010年12月31日每年回报1?



了解java.util.Calendar WEEK_OF_YEAR

解决方案

这是一个更简单的方法,使用精彩 http://www.joda.org/joda-time/ 图书馆:

  String start =01/01/2009; 
String end =12/09/2013​​;
DateTimeFormatter模式= DateTimeFormat.forPattern(dd / MM / yyyy);
DateTime startDate = pattern.parseDateTime(start);
DateTime endDate = pattern.parseDateTime(end);

列表< DateTime> fridays = new ArrayList<>();

while(startDate.isBefore(endDate)){
if(startDate.getDayOfWeek()== DateTimeConstants.FRIDAY){
fridays.add(startDate);
}
startDate = startDate.plusDays(1);
}

在这个结尾,你会有星期五的星期五阵列。简单吗?



或者如果你想加快速度,一旦你有一个星期五,你可以从使用天数切换到使用周:

  String start =01/01/2009; 
String end =12/09/2013​​;
DateTimeFormatter pattern = DateTimeFormat.forPattern(dd / MM / yyyy);
DateTime startDate = pattern.parseDateTime(start);
DateTime endDate = pattern.parseDateTime(end);

列表< DateTime> fridays = new ArrayList<>();
boolean reachAFriday = false;
while(startDate.isBefore(endDate)){
if(startDate.getDayOfWeek()== DateTimeConstants.FRIDAY){
fridays.add(startDate);
达到AFriday = true;
}
if(reachedAFriday){
startDate = startDate.plusWeeks(1);
} else {
startDate = startDate.plusDays(1);
}
}


I recently came across a task where i have to get all Fridays in a date range. I wrote a small piece of code and was surprised see some strange behaviour.

Below is my code:

public class Friday {
    public static void main(String[]args){
        String start = "01/01/2009";
        String end = "12/09/2013";
        String[] startTokens = start.split("/");
        String[] endTokens = end.split("/");
        Calendar  startCal = new GregorianCalendar(Integer.parseInt(startTokens[2]),Integer.parseInt(startTokens[1])-1,Integer.parseInt(startTokens[0]));
        Calendar endCal = new GregorianCalendar(Integer.parseInt(endTokens[2]),Integer.parseInt(endTokens[1])-1, Integer.parseInt(endTokens[0]));

        int startYear = Integer.parseInt(startTokens[2]);
        int endYear = Integer.parseInt(endTokens[2]); 


        int startWeek = startCal.get(Calendar.WEEK_OF_YEAR);
        int endWeek = endCal.get(Calendar.WEEK_OF_YEAR);

        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
    //  cal.setMinimalDaysInFirstWeek(7);
        ArrayList<String> main = new ArrayList<String>();
        while(startYear <= endYear ){
               cal.set(Calendar.YEAR, startYear);
               System.out.println(cal.getMinimalDaysInFirstWeek());
                if(startYear == endYear){
                    main.addAll(getFridays(startWeek, endWeek, cal));
                }
                else{
                    main.addAll(getFridays(startWeek, 52, cal));
                    startWeek = 1;
                }
                startYear =startYear +1;
        }

        for(String s: main){
            System.err.println(s);
        }
    }
    public static ArrayList<String> getFridays(int startWeek, int endWeek, Calendar cal){
        ArrayList<String> fridays = new ArrayList<String>();
        while(startWeek <= endWeek){
            cal.set(Calendar.WEEK_OF_YEAR, startWeek);
            fridays.add(cal.getTime().toString());
            startWeek = startWeek+1;
        }
        return fridays;
    }
}

Now when I ran the code i noticed that Fridays of 2011 are missing. After some debugging and online browsing i figured that Calendar.WEEK_OF_YEAR is locale specific and I have to use setMinimalDaysInFirstWeek(7) to fix it.

So uncommented the related line in the above code.

From what I understood now first week of year should start from full week of year.

For example Jan 1 2010 is friday. But it should not show up in results as i configured it to treat that week begins from Jan 3rd. But Now i still see the Jan 1 as friday

I am very much confused. Can some one explain why it is happening?

These Stackoverflow articles helped me a bit:

Why dec 31 2010 returns 1 as week of year?

Understanding java.util.Calendar WEEK_OF_YEAR

解决方案

Here is an easier method, using the wonderful http://www.joda.org/joda-time/ library:

String start = "01/01/2009";
String end = "12/09/2013";
DateTimeFormatter pattern = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime startDate = pattern.parseDateTime(start);
DateTime endDate = pattern.parseDateTime(end);

List<DateTime> fridays = new ArrayList<>();

while (startDate.isBefore(endDate)){
    if ( startDate.getDayOfWeek() == DateTimeConstants.FRIDAY ){
        fridays.add(startDate);
    }
    startDate = startDate.plusDays(1);
}

at the end of this, you'd have the fridays in the fridays array. Simple?

Or if you want to speed things up, once you have gotten a friday, you can switch from using days, to using weeks:

String start = "01/01/2009";
String end = "12/09/2013";
DateTimeFormatter pattern = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime startDate = pattern.parseDateTime(start);
DateTime endDate = pattern.parseDateTime(end);

List<DateTime> fridays = new ArrayList<>();
boolean reachedAFriday = false;
while (startDate.isBefore(endDate)){
    if ( startDate.getDayOfWeek() == DateTimeConstants.FRIDAY ){
        fridays.add(startDate);
        reachedAFriday = true;
    }
    if ( reachedAFriday ){
        startDate = startDate.plusWeeks(1);
    } else {
        startDate = startDate.plusDays(1);
    }
}

这篇关于在Java中的日期范围内获取所有星期五的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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