Java Week Day nextDayAndHour(int dayOfTheWeek, int hour)

Here you can find the source of nextDayAndHour(int dayOfTheWeek, int hour)

Description

next Day And Hour

License

Open Source License

Declaration

public static Date nextDayAndHour(int dayOfTheWeek, int hour) 

Method Source Code


//package com.java2s;
/*-/*from   w w  w  .  ja va 2s. co m*/
 * #%L
 * Duniter4j :: Core Shared
 * %%
 * Copyright (C) 2014 - 2017 EIS
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static Date nextDayAndHour(int dayOfTheWeek, int hour) {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(System.currentTimeMillis());
        if (cal.get(Calendar.DAY_OF_WEEK) > dayOfTheWeek
                || (cal.get(Calendar.DAY_OF_WEEK) == dayOfTheWeek && cal.get(Calendar.HOUR_OF_DAY) >= hour)) {
            // Too late for this week: will wait for next week
            cal.add(Calendar.WEEK_OF_YEAR, 1);
        }
        cal.set(Calendar.DAY_OF_WEEK, dayOfTheWeek);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}

Related

  1. isoDayOfWeek(int javaDayOfWeek)
  2. isWeekday(String dateStr, String format)
  3. isWeekendDay(String strDay, DateFormat dateFormatter)
  4. isWeekendToday()
  5. lastDayOfWeek(int year, int month)
  6. nextTwoWeeksFromToday()
  7. nextWeekday(int weekday, int hour, int minute, int second)
  8. parseToEntireWeekCaption(String startDay, String pattern)
  9. plusDay(Date date, int plusWeek)