18

我正在做一个项目,要求将日期计算为给定月份的最后一个星期五。我想我有一个只使用标准 Java 的解决方案,但我想知道是否有人知道更简洁或更高效的方法。以下是我今年测试的内容:

    for (int month = 0; month < 13; month++) {
        GregorianCalendar d = new GregorianCalendar();
        d.set(d.MONTH, month);
        System.out.println("Last Week of Month in " + d.getDisplayName(d.MONTH, Calendar.LONG, Locale.ENGLISH) + ": " + d.getLeastMaximum(d.WEEK_OF_MONTH));
        d.set(d.DAY_OF_WEEK, d.FRIDAY);
        d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));
        while (d.get(d.MONTH) > month || d.get(d.MONTH) < month) {
            d.add(d.WEEK_OF_MONTH, -1);
        }
        Date dt = d.getTime();
        System.out.println("Last Friday of Last Week in  " + d.getDisplayName(d.MONTH, Calendar.LONG, Locale.ENGLISH) + ": " + dt.toString());
    }
4

16 回答 16

37

让 Calendar.class 为您施展魔法;)

pCal.set(GregorianCalendar.DAY_OF_WEEK,Calendar.FRIDAY);
pCal.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1);
于 2010-03-30T13:49:33.870 回答
27

根据marked23的建议:

public Date getLastFriday( int month, int year ) {
   Calendar cal = Calendar.getInstance();
   cal.set( year, month + 1, 1 );
   cal.add( Calendar.DAY_OF_MONTH, -( cal.get( Calendar.DAY_OF_WEEK ) % 7 + 1 ) );
   return cal.getTime();
}
于 2008-09-16T21:04:14.677 回答
15

java.time

使用Java 8 及更高版本中内置的java.timeTemporalAdjusters.lastInMonth库,您可以使用:

val now = LocalDate.now() 
val lastInMonth = now.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY))

您可以从DayOfWeek枚举中选择任何一天。

如果您需要添加时间信息,您可以使用任何可LocalDate用于LocalDateTime转换的信息,例如

lastFriday.atStartOfDay() // e.g. 2015-11-27T00:00
于 2015-11-23T09:51:28.530 回答
10

你永远不需要循环来找出这个。要确定本月的“最后一个星期五”日期,请从下个月的第一天开始。根据该月的第一天所在的星期几(数字)减去适当的天数。这是你的“上周五”。我很确定它可以归结为一个较长的单行,但我不是 Java 开发人员。所以我会把它留给别人。

于 2008-09-16T20:14:17.137 回答
7

我会使用像Jodatime这样的库。它有一个非常有用的 API,它使用正常的月份数字。最重要的是,它是线程安全的。

我认为你可以有一个解决方案(但可能不是最短的,但肯定更具可读性):

DateTime now = new DateTime();      
DateTime dt = now.dayOfMonth().withMaximumValue().withDayOfWeek(DateTimeConstants.FRIDAY);
if (dt.getMonthOfYear() != now.getMonthOfYear()) {
  dt = dt.minusDays(7);
}       
System.out.println(dt);
于 2008-09-16T20:13:47.513 回答
2

您需要知道两件事 - 该月的天数,以及该月的第一天所在的工作日。

如果该月的第一天是

  • 星期天,那么最后一个星期五总是27 号。
  • 星期一,然后最后一个星期五总是26 号。
  • 星期二,然后最后一个星期五总是25 号。
  • 星期三,那么最后一个星期五就是 24 号,除非这个月有 31 天,那么就是 31 号
  • 星期四,那么最后一个星期五就是 23 号,除非这个月有 30 天或更多天,否则就是 30 号。
  • 星期五,那么最后一个星期五就是 22 号,除非这个月有 29 天或更多,那么就是 29 号。
  • 星期六,那么最后一个星期五总是28 号。

只有三种特殊情况。一个 switch 语句和三个 if 语句(或三元运算符,如果您希望每个案例都有一行......)

把它写在纸上。不需要任何特殊的库、函数、朱利安转换等(好吧,除了获得第一个工作日的工作日,也许是那个月的天数......)

Aaron 用 Ja​​va 实现了它。

-亚当

于 2008-09-16T20:36:58.037 回答
2

亚当戴维斯算法的代码

public static int getLastFriday(int month, int year)
{
Calendar cal = Calendar.getInstance();
cal.set(year, month, 1, 0, 0, 0); // set to first day of the month
cal.set(Calendar.MILLISECOND, 0);

int firstDay = cal.get(Calendar.DAY_OF_WEEK);
int daysOfMonth = cal.getMaximum(Calendar.DAY_OF_MONTH);

switch (firstDay)
{
    case Calendar.SUNDAY :
        return 27;
    case Calendar.MONDAY :
        return 26;
    case Calendar.TUESDAY :
        return 25;
    case Calendar.WEDNESDAY :
        if (daysOfMonth == 31) return 31;
        return 24;
    case Calendar.THURSDAY :
        if (daysOfMonth >= 30) return 30;
        return 23;
    case Calendar.FRIDAY :
        if (daysOfMonth >= 29) return 29;
        return 22;
    case Calendar.SATURDAY :
        return 28;
}
throw new RuntimeException("what day of the month?");
}}
于 2008-09-16T21:27:28.007 回答
2

以下是获取本月最后一个星期五或任何工作日的方法:

Calendar thisMonth = Calendar.getInstance();
dayOfWeek = Calendar.FRIDAY; // or whatever
thisMonth.set(Calendar.WEEK_OF_MONTH, thisMonth.getActualMaximum(Calendar.WEEK_OF_MONTH);;
thisMonth.set(Calendar.DAY_OF_WEEK, dayOfWeek);
int lastDay = thisMonth.get(Calendar.DAY_OF_MONTH); // this should be it.
于 2011-10-18T10:55:03.150 回答
1

更容易阅读的蛮力方法:

public int getLastFriday(int month, int year) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, 1, 0, 0, 0); // set to first day of the month
    cal.set(Calendar.MILLISECOND, 0);

    int friday = -1;
    while (cal.get(Calendar.MONTH) == month) { 
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) { // is it a friday?
            friday = cal.get(Calendar.DAY_OF_MONTH);
            cal.add(Calendar.DAY_OF_MONTH, 7); // skip 7 days
        } else {
            cal.add(Calendar.DAY_OF_MONTH, 1); // skip 1 day
        }
    }
    return friday;
}
于 2008-09-16T20:09:12.190 回答
1

虽然我同意 scubabbl,但这是一个没有内在时间的版本。

int year = 2008;
for (int m = Calendar.JANUARY; m <= Calendar.DECEMBER; m++) {
    Calendar cal = new GregorianCalendar(year, m, 1);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    int diff = Calendar.FRIDAY - cal.get(Calendar.DAY_OF_WEEK);
    if (diff > 0) {
        diff -= 7;
    }
    cal.add(Calendar.DAY_OF_MONTH, diff);
    System.out.println(cal.getTime());
}
于 2008-09-16T20:15:11.723 回答
1

以下节目适用于每个月的最后一个星期五。它可用于获取任何月份中一周中任何一天的最后一天。变量offset=0表示当前月份(系统日期),offset=1 表示下个月,依此类推。该getLastFridayofMonth(int offset)方法将返回上周五。

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class LastFriday {

  public static Calendar getLastFriday(Calendar cal,int offset){
    int dayofweek;//1-Sunday,2-Monday so on....
    cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+offset);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); //set calendar to last day of month
    dayofweek=cal.get(Calendar.DAY_OF_WEEK); //get the day of the week for last day of month set above,1-sunday,2-monday etc
    if(dayofweek<Calendar.FRIDAY)  //Calendar.FRIDAY will return integer value =5 
      cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-7+Calendar.FRIDAY-dayofweek);
    else
      cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)+Calendar.FRIDAY-dayofweek); 

    return cal;
  }

  public static String  getLastFridayofMonth(int offset) { //offset=0 mean current month
    final String DATE_FORMAT_NOW = "dd-MMM-yyyy";
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
    cal=getLastFriday(cal,offset);
    return sdf.format(cal.getTime()); 

  }

  public static void main(String[] args) {
    System.out.println(getLastFridayofMonth(0)); //0 = current month
    System.out.println(getLastFridayofMonth(1));//1=next month
    System.out.println(getLastFridayofMonth(2));//2=month after next month
  }

}
于 2012-06-06T21:54:44.323 回答
1

在 Java 8 中,我们可以简单地这样做:

LocalDate lastFridayOfMonth = LocalDate
                                    .now()
                                    .with(lastDayOfMonth())
                                    .with(previous(DayOfWeek.FRIDAY));
于 2016-12-06T11:11:36.413 回答
0

这看起来是一个完全可以接受的解决方案。如果可行,请使用它。这是最少的代码,除非您必须这样做,否则没有理由对其进行优化。

于 2008-09-16T19:56:49.333 回答
0

希望这可以帮助..

public static void getSundaysInThisMonth(int monthNumber, int yearNumber){
    //int year =2009;
    //int dayOfWeek = Calendar.SUNDAY;
    // instantiate Calender and set to first Sunday of 2009
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.MONTH, monthNumber-1);
    cal.set(Calendar.YEAR, yearNumber);
    cal.set(Calendar.DATE, 1);
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    int dateOfWeek = cal.get(Calendar.DATE);
    while (dayOfWeek  != Calendar.SUNDAY) {
       cal.set(Calendar.DATE, ++dateOfWeek);
       dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
      }
    cal.set(Calendar.DATE, dateOfWeek);

    int i = 1;
    while (cal.get(Calendar.YEAR) == yearNumber && cal.get(Calendar.MONTH)==monthNumber-1)
    {
            System.out.println("Sunday " + " " + i + ": " + cal.get(Calendar.DAY_OF_MONTH));
            cal.add(Calendar.DAY_OF_MONTH, 7);
            i++;
    }

  }
  public static void main(String args[]){
    getSundaysInThisMonth(1,2009);
  }
于 2009-07-01T07:19:00.857 回答
0
public static int lastSundayDate()
{
    Calendar cal = getCalendarInstance();
    cal.setTime(new Date(getUTCTimeMillis()));
    cal.set( Calendar.DAY_OF_MONTH , 25 );
    return (25 + 8 - (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY ? cal.get(Calendar.DAY_OF_WEEK) : 8));
}
于 2014-03-14T06:01:38.780 回答
-1
public static Calendar getNthDow(int month, int year, int dayOfWeek, int n) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, 1);
    cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, n);
    return (cal.get(Calendar.MONTH) == month) && (cal.get(Calendar.YEAR) == year) ? cal : null;
}
于 2012-02-27T17:22:59.023 回答