如何在过去一个月的第一个星期一之前选择星期日

时间:2018-01-24 06:35:40

标签: java date

我想在之前选择Sunday 之前Monday的{​​{1}}。

Sunday可能在同一个月或上个月,但我希望日期为Sunday。我尝试使用以下逻辑获取Sunday,它适用于当月,但如果我尝试再过一个月Nov-2017,那么我必须再次将MONDAY-2更改为MONDAY-3。所以这不是正确的方法。那我该怎么做呢?

Calendar c = Calendar.getInstance();

c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("Date " + c.getTime());
c.set(Calendar.DAY_OF_MONTH, Calendar.MONDAY - 2);
System.out.println("Date " + c.getTime());

我想将日期传递给代码。那我该怎么办呢?就像我将日期保存在变量中然后根据该变量提供的输入它应该计算逻辑并提供输出

7 个答案:

答案 0 :(得分:5)

@Test
public void testDate() throws ParseException {

    SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");

    Date d = fmt.parse("01-Nov-2017");
    System.out.println(d);
    Calendar c = Calendar.getInstance();
    c.setTime(d);
    getSundayBefore1thMondayOfMonth(c);
}

public void getSundayBefore1thMondayOfMonth(Calendar c) {
    c.set(Calendar.DAY_OF_MONTH, 1);
    int wd = c.get(Calendar.DAY_OF_WEEK);
    if (wd > Calendar.MONDAY ) {
        c.add(Calendar.DAY_OF_MONTH, 7);
    }
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    System.out.println(c.getTime());
    c.add(Calendar.DAY_OF_MONTH, -1);
    System.out.println(c.getTime());
}



Wed Nov 01 00:00:00 CST 2017
Mon Nov 06 00:00:00 CST 2017
Sun Nov 05 00:00:00 CST 2017

答案 1 :(得分:3)

如果您使用的是Java 8,那么您可以使用java.time库,您可以使用:

LocalDate firstMondayOfMonth = LocalDate.now().with(
        TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)
);// This return 2018-01-01

LocalDate sunday = firstMondayOfMonth.minusDays(1);//This return 2017-12-31

要在2017年11月进行测试,您可以使用LocalDate.of代替LocalDate.now(),如下所示:

LocalDate firstMondayOfMonth = LocalDate.of(2017, Month.NOVEMBER, 1).with(
        TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)
);// This return 2017-11-06

LocalDate firstSunday = firstMondayOfMonth.minusDays(1);// This return 2017-11-05

答案 2 :(得分:3)

TL;博士

YearMonth
    .now()          // Current year-month. Tip: Better to pass the optional time zone, as shown further down in this Answer.
    .atDay( 1 )     // First of the current month.
    .with( TemporalAdjusters.nextOrSame( DayOfWeek.MONDAY ) )  // Move from first day of month to the following Monday, or stay on the first if it already a Monday. 
    .minusDays( 1 ) // Move back one day from Monday to get a Sunday. May be in current month or in previous month. 

java.time

你正在使用现在遗留下来的麻烦的旧日期日期类,取而代之的是java.time类。

确定当前月份意味着确定当前日期。确定当前日期需要时区。对于任何特定时刻,日期在全球范围内因地区而异。

ZoneId z = ZoneId.of( “Africa/Tunis” ) ;

YearMonth类代表整个月。

YearMonth currentYearMonth = YearMonth.now( z ) ;

由此我们可以获得本月的第一天。

LocalDate firstOfMonth = currentYearMonth.atDay( 1 ) ;

我们可以通过拨打TemporalAdjuster来移动到一周中的某一天。

LocalDate firstMondayOfMonth = firstOfMonth.with( TemporalAdjusters.nextOrSame( DayOfWeek.MONDAY ) ) ;
LocalDate sundayBeforeFirstMondayOfMonth = firstMondayOfMonth.with( TemporalAdjusters.previous( DayOfWeek.SUNDAY ) ) ;

逻辑上,最后一行可以用.minusDays( 1 )替换,因为我们知道上一个星期日紧接着我们星期一的定义。

答案 3 :(得分:1)

在下面的代码中,我们首先得到本月的第一个星期一。然后我们只减去1天。

    // input
    int year = 2017;
    int month = Calendar.NOVEMBER;

    // get first monday of the month
    Calendar c = Calendar.getInstance();
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    c.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.YEAR, year);
    System.out.println("Date " + c.getTime());

    // subtract 1
    c.add(Calendar.DAY_OF_MONTH, -1);
    System.out.println("Date " + c.getTime());

答案 4 :(得分:1)

mydf <- structure(list(amiloride = c(35.28, 0, -2.69, 0, 0, 18.63), BRD.K14844937 = c(63.64, 
-65.7, 0, 0, -95.85, -48.56), flupentixol = c(54.02, -53.37, 
-96.26, 0, 55.2, 0)), .Names = c("amiloride", "BRD.K14844937", 
"flupentixol"), class = "data.frame", row.names = c("PC3", "VCAP", 
"A375", "A549", "HA1E", "HCC515"))

根据月的第一周对您的意义,选择Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek( Calendar.MONDAY); //Monday is first day of a week c.setMinimalDaysInFirstWeek( 1); //first week of month is the week that has at least one day in this month //c.setMinimalDaysInFirstWeek( 7); //first week of month must be a full week c.set(Calendar.WEEK_OF_MONTH, 1); //move to first week of month System.out.println("Date " + c.getTime()); c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //move to Monday System.out.println("Date " + c.getTime()); c.add( Calendar.DAY_OF_MONTH, -1); //go back one day System.out.println("Date " + c.getTime()); 方法之一。

答案 5 :(得分:1)

试试这个:

        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        System.out.println(calendar.getTime());

答案 6 :(得分:1)

在Java 8中,您可以使用以下代码:

 public class Main {

public static int sum = 1;

public static void main(String[] args) {
    System.out.println(factorial(4,8));
}

public static  long factorial(long min , long max ){

    while(max >= min) {
        sum *= max;
        max -= 1;
        factorial(min ,max);
    }
    return sum;
}

并按照要求调用上面的方法:

public static void getSundayBeforeFirstMondayOfMonth(LocalDate date){
    date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)).minusDays(1);
  }