Example usage for org.joda.time DateTime getDayOfWeek

List of usage examples for org.joda.time DateTime getDayOfWeek

Introduction

In this page you can find the example usage for org.joda.time DateTime getDayOfWeek.

Prototype

public int getDayOfWeek() 

Source Link

Document

Get the day of week field value.

Usage

From source file:com.clevercloud.bianca.lib.date.DateModule.java

License:Open Source License

/**
 * Returns the parsed date.//from  ww  w .j ava 2  s . c om
 */
public Value strptime(Env env, String date, String format) {
    ArrayValueImpl array = new ArrayValueImpl();
    DateTimeFormatterBuilder fb = new DateTimeFormatterBuilder();

    int length = format.length();

    for (int i = 0; i < length; i++) {
        char ch = format.charAt(i);
        if (ch != '%') {
            fb.appendLiteral(ch);
            continue;
        }

        switch (format.charAt(++i)) {
        case 'a':
            fb.appendDayOfWeekShortText();
            break;

        case 'A':
            fb.appendDayOfWeekText();
            break;

        case 'h':
        case 'b':
            fb.appendMonthOfYearShortText();
            ;
            break;

        case 'B':
            fb.appendMonthOfYearText();
            break;

        // TODO: case 'c'

        case 'C':
            fb.appendCenturyOfEra(2, 2);
            break;

        case 'd':
            fb.appendDayOfMonth(2);
            break;

        case 'D':
            fb.appendMonthOfYear(2);
            fb.appendLiteral('/');
            fb.appendDayOfMonth(2);
            fb.appendLiteral('/');
            fb.appendYear(2, 2);
            break;

        // TODO: case 'e'

        case 'F':
            fb.appendYear(4, 4);
            fb.appendLiteral('-');
            fb.appendMonthOfYear(2);
            fb.appendLiteral('-');
            fb.appendDayOfMonth(2);
            break;

        // TODO: case 'g'
        // TODO: case 'G'

        case 'H':
            fb.appendHourOfDay(2);
            break;

        case 'I':
            fb.appendHourOfHalfday(2);
            break;

        case 'j':
            fb.appendDayOfYear(3);
            break;

        // TODO: case 'l'

        case 'm':
            fb.appendMonthOfYear(2);
            break;

        case 'M':
            fb.appendMinuteOfHour(2);
            break;

        case 'n':
            fb.appendLiteral("\n");
            break;

        case 'p':
        case 'P':
            fb.appendHalfdayOfDayText();
            break;

        case 'r':
            fb.appendHourOfHalfday(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            fb.appendLiteral(':');
            fb.appendSecondOfMinute(2);
            fb.appendLiteral(' ');
            fb.appendHalfdayOfDayText();
            break;

        case 'R':
            fb.appendHourOfDay(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            break;

        // TODO: case 's'

        case 'S':
            fb.appendSecondOfMinute(2);
            break;

        case 't':
            fb.appendLiteral("\t");
            break;

        case 'T':
            fb.appendHourOfDay(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            fb.appendLiteral(':');
            fb.appendSecondOfMinute(2);
            break;

        // TODO: case 'u'
        // TODO: case 'U'
        // TODO: case 'V'
        // TODO: case 'w'
        // TODO: case 'W'
        // TODO: case 'x'
        // TODO: case 'X'

        case 'y':
            fb.appendYear(2, 2);
            break;

        case 'Y':
            fb.appendYear(4, 4);
            break;

        case 'z':
            fb.appendTimeZoneOffset(null, true, 2, 2);
            break;

        case 'Z':
            fb.appendTimeZoneName();
            break;

        case '%':
            fb.appendLiteral('%');
            break;

        default:
            fb.appendLiteral(ch);
        }
    }

    DateTimeFormatter dtf = fb.toFormatter().withLocale(Locale.getDefault()).withOffsetParsed();

    org.joda.time.DateTime dt = new org.joda.time.DateTime();

    String unparsed = "";

    try {
        dt = dtf.parseDateTime(date);
    } catch (IllegalArgumentException e) {
        String delims = "[\"]+";

        String[] splits = e.getMessage().split(delims);

        unparsed = unparsed.concat(splits[3]);
    }

    // According to manual strptime(3)
    if (dt.getCenturyOfEra() == 0) {
        if (dt.getYear() > 68) {
            dt = dt.withCenturyOfEra(19);
        } else {
            dt = dt.withCenturyOfEra(20);
        }
    }

    array.put("tm_sec", dt.getSecondOfMinute());
    array.put("tm_min", dt.getMinuteOfHour());
    array.put("tm_hour", dt.getHourOfDay());
    array.put("tm_mday", dt.getDayOfMonth());
    array.put("tm_mon", dt.getMonthOfYear() - 1);
    array.put("tm_year", dt.getYearOfCentury() + ((dt.getCenturyOfEra() - 19) * 100)); // Years since 1900
    array.put("tm_wday", dt.getDayOfWeek() % 7);
    array.put("tm_yday", dt.getDayOfYear() - 1);
    array.put("unparsed", unparsed);

    return array;
}

From source file:com.core.meka.Util.java

public static String dia(Date d) {
    String result = "Desconocido";
    if (d != null) {
        DateTime date = new DateTime(d);
        switch (date.getDayOfWeek()) {
        case 7:// w  w  w  . j a va 2s.c  o m
            result = "Domingo";
            break;
        case 1:
            result = "Lunes";
            break;
        case 2:
            result = "Martes";
            break;
        case 3:
            result = "Miercoles";
            break;
        case 4:
            result = "Jueves";
            break;
        case 5:
            result = "Viernes";
            break;
        case 6:
            result = "Sabado";
            break;
        }
    }
    return result;
}

From source file:com.cronutils.model.time.generator.OnDayOfMonthValueGenerator.java

License:Apache License

private int generateValue(On on, int year, int month) throws NoSuchValueException {
    int time = on.getTime().getValue();
    switch (on.getSpecialChar().getValue()) {
    case L://from w ww .jav a  2s.  c om
        return new DateTime(year, month, 1, 1, 1).dayOfMonth().getMaximumValue();
    case W:
        DateTime doM = new DateTime(year, month, time, 1, 1);
        if (doM.getDayOfWeek() == 6) {//dayOfWeek is Saturday!
            if (time == 1) {//first day in month is Saturday! We execute on Monday
                return 3;
            }
            return time - 1;
        }
        if (doM.getDayOfWeek() == 7) {
            if ((time + 1) <= doM.dayOfMonth().getMaximumValue()) {
                return time + 1;
            }
        }
        break;
    case LW:
        DateTime lastDayOfMonth = new DateTime(year, month,
                new DateTime(year, month, 1, 1, 1).dayOfMonth().getMaximumValue(), 1, 1);
        int dow = lastDayOfMonth.getDayOfWeek();
        int diff = dow - 5;
        if (diff > 0) {
            return lastDayOfMonth.minusDays(diff).dayOfMonth().get();
        }
        return lastDayOfMonth.dayOfMonth().get();
    }
    throw new NoSuchValueException();
}

From source file:com.cronutils.model.time.generator.OnDayOfWeekValueGenerator.java

License:Apache License

private int generateLValues(On on, int year, int month) throws NoSuchValueException {
    int lastDoM = new DateTime(year, month, 1, 1, 1).dayOfMonth().getMaximumValue();
    DateTime lastDoMDateTime = new DateTime(year, month, lastDoM, 1, 1);
    int dowForLastDoM = lastDoMDateTime.getDayOfWeek();//1-7
    int requiredDoW = ConstantsMapper.weekDayMapping(mondayDoWValue, ConstantsMapper.JODATIME_WEEK_DAY,
            on.getTime().getValue());//to normalize to joda-time value
    int dowDiff = dowForLastDoM - requiredDoW;

    if (dowDiff == 0) {
        return lastDoMDateTime.dayOfMonth().get();
    }// ww  w.  jav a2 s . c o m
    if (dowDiff < 0) {
        return lastDoMDateTime.minusDays(dowForLastDoM + (7 - requiredDoW)).dayOfMonth().get();
    }
    if (dowDiff > 0) {
        return lastDoMDateTime.minusDays(dowDiff).dayOfMonth().get();
    }
    throw new NoSuchValueException();
}

From source file:com.ecofactor.qa.automation.util.DateUtil.java

License:Open Source License

/**
 * Gets the uTC day of week./*from  w w  w  .j  a  va 2  s  .c o  m*/
 * @return the uTC day of week
 */
public static int getUTCDayOfWeek() {

    DateTime dt = new DateTime(DateTimeZone.UTC);
    int dayOfWeek = dt.getDayOfWeek();
    if (dayOfWeek < 7) {
        dayOfWeek += 1;
    } else if (dayOfWeek == 7) {
        dayOfWeek = 1;
    }
    return dayOfWeek;
}

From source file:com.enitalk.configs.DateCache.java

public NavigableSet<DateTime> days(JsonNode tree, String tz, JsonNode teacherNode) {
    ConcurrentSkipListSet<DateTime> dates = new ConcurrentSkipListSet<>();

    Iterator<JsonNode> els = tree.elements();
    DateTimeZone dz = DateTimeZone.forID(tz);
    DateTimeFormatter hour = DateTimeFormat.forPattern("HH:mm").withZone(dz);

    DateTime today = DateTime.now().millisOfDay().setCopy(0);
    while (els.hasNext()) {

        JsonNode el = els.next();/*from   w  w w . java2 s .  co  m*/
        String day = el.path("day").asText();

        boolean plus = today.getDayOfWeek() > days.get(day);
        if (el.has("start") && el.has("end")) {
            DateTime start = hour.parseDateTime(el.path("start").asText()).dayOfMonth()
                    .setCopy(today.getDayOfMonth()).monthOfYear().setCopy(today.getMonthOfYear()).year()
                    .setCopy(today.getYear()).withDayOfWeek(days.get(day)).plusWeeks(plus ? 1 : 0);
            DateTime end = hour.parseDateTime(el.path("end").asText()).dayOfMonth()
                    .setCopy(today.getDayOfMonth()).monthOfYear().setCopy(today.getMonthOfYear()).year()
                    .setCopy(today.getYear()).withDayOfWeek(days.get(day)).plusWeeks(plus ? 1 : 0);

            Hours hours = Hours.hoursBetween(start, end);
            int hh = hours.getHours() + 1;

            while (hh-- > 0) {
                dates.add(start.plusHours(hh).toDateTime(DateTimeZone.UTC));
            }
        } else {
            List<String> datesAv = jackson.convertValue(el.path("times"), List.class);
            logger.info("Array of dates {} {}", datesAv, day);

            datesAv.forEach((String dd) -> {
                DateTime date = hour.parseDateTime(dd).dayOfMonth().setCopy(today.getDayOfMonth()).monthOfYear()
                        .setCopy(today.getMonthOfYear()).year().setCopy(today.getYear())
                        .withDayOfWeek(days.get(day)).plusWeeks(plus ? 1 : 0);
                dates.add(date.toDateTime(DateTimeZone.UTC));
            });

        }

    }

    final TreeSet<DateTime> addWeek = new TreeSet<>();
    for (int i = 1; i < 2; i++) {
        for (DateTime e : dates) {
            addWeek.add(e.plusWeeks(i));

        }
    }

    dates.addAll(addWeek);

    DateTime nowUtc = DateTime.now().toDateTime(DateTimeZone.UTC);
    nowUtc = nowUtc.plusHours(teacherNode.path("notice").asInt(2));

    NavigableSet<DateTime> ss = dates.tailSet(nowUtc, true);

    return ss;

}

From source file:com.francelabs.datafari.alerts.AlertsManager.java

License:Apache License

/**
 * Calculate the difference in minutes between the current date time and the
 * provided scheduled one, according to the frequency
 *
 * @param frequency//from w w w. j a va 2  s  .  c om
 *          the frequency of the scheduled date time
 * @param scheduledDate
 *          the initial scheduled date time that the user has typed in the
 *          admin UI
 * @return the difference in minutes between the current date time and the
 *         next scheduled one
 */
private long calculateDelays(final String frequency, final DateTime scheduledDate) {
    long diff = 0L;
    final Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    final DateTime currentDateTime = new DateTime(cal.getTime());
    DateTime scheduledDateTimeUpdate;

    switch (frequency.toLowerCase()) {
    case "hourly":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is earlier than the scheduled one, simply calculate
        // the difference in minutes, otherwise create the next scheduled
        // date and calculate the difference
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.HOUR_OF_DAY, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        diff = Minutes.minutesBetween(currentDateTime, scheduledDateTimeUpdate).getMinutes();
        break;

    case "daily":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, scheduledDate.getHourOfDay());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is earlier than the scheduled one, simply calculate
        // the difference in minutes, otherwise create the next scheduled
        // date and calculate the difference
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        diff = Minutes.minutesBetween(currentDateTime, scheduledDateTimeUpdate).getMinutes();
        break;

    case "weekly":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, scheduledDate.getDayOfWeek() + 1); // +1
        // =
        // diff
        // between
        // Joda
        // and
        // Calendar
        cal.set(Calendar.HOUR_OF_DAY, scheduledDate.getHourOfDay());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is earlier than the scheduled one, simply calculate
        // the difference in minutes, otherwise create the next scheduled
        // date and calculate the difference
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.WEEK_OF_YEAR, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        diff = Minutes.minutesBetween(currentDateTime, scheduledDateTimeUpdate).getMinutes();
        break;

    default:
        break;
    }

    return diff;
}

From source file:com.francelabs.datafari.servlets.admin.alertsAdmin.java

License:Apache License

private String getNextEvent(final String frequency, final String initialDate) {
    final DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy/HH:mm");
    final DateTime scheduledDate = new DateTime(formatter.parseDateTime(initialDate));
    final Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.set(Calendar.SECOND, 0);//w w  w . java  2  s.  c om
    cal.set(Calendar.MILLISECOND, 0);
    final DateTime currentDateTime = new DateTime(cal.getTime());
    DateTime scheduledDateTimeUpdate = new DateTime(cal.getTime());

    switch (frequency.toLowerCase()) {
    case "hourly":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is later than the scheduled one then create the next
        // scheduled date
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.HOUR_OF_DAY, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        break;

    case "daily":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, scheduledDate.getHourOfDay());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is later than the scheduled one then create the next
        // scheduled date
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        break;

    case "weekly":
        // Create what would be the current scheduled date
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, scheduledDate.getDayOfWeek() + 1); // +1
        // =
        // diff
        // between
        // Joda
        // and
        // Calendar
        cal.set(Calendar.HOUR_OF_DAY, scheduledDate.getHourOfDay());
        cal.set(Calendar.MINUTE, scheduledDate.getMinuteOfHour());
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        scheduledDateTimeUpdate = new DateTime(cal.getTime());

        // Compare the current date with the current scheduled one, if the
        // current date is later than the scheduled one then create the next
        // scheduled date
        if (!currentDateTime.isBefore(scheduledDateTimeUpdate)) {
            cal.add(Calendar.WEEK_OF_YEAR, 1);
            scheduledDateTimeUpdate = new DateTime(cal.getTime());
        }
        break;

    default:
        break;
    }
    return scheduledDateTimeUpdate.toString(formatter);
}

From source file:com.google.android.apps.paco.EsmGenerator2.java

License:Open Source License

private List<Integer> getPeriodDaysForMonthOf(DateTime startDate) {
    int dow = startDate.getDayOfWeek();
    int day = 1;//w ww . j  a  v  a  2  s  .c  o m
    int lastDayOfMonth = startDate.dayOfMonth().withMaximumValue().getDayOfMonth();
    List<Integer> validPeriods = new ArrayList<Integer>();
    while (day < lastDayOfMonth + 1) {
        if (schedule.getEsmWeekends() || !TimeUtil.isWeekend(dow)) {
            validPeriods.add(day);
        }
        dow++;
        if (dow == 8) {
            dow = 1;
        }
        day++;
    }
    return validPeriods;
}

From source file:com.google.android.apps.paco.NonESMSignalGenerator.java

License:Open Source License

private DateTime scheduleWeekly(DateTime now) {
    DateTime nowMidnight = now.toDateMidnight().toDateTime();
    int nowDow = nowMidnight.getDayOfWeek(); // joda starts Monday, I start Sunday
    Integer nowDowIndex = SignalSchedule.DAYS_OF_WEEK[nowDow == 7 ? 0 : nowDow]; // joda is 1 based, and starts on Monday. we are 0-based, Sunday-start
    if ((schedule.getWeekDaysScheduled() & nowDowIndex) == nowDowIndex) {
        DateTime nextTimeToday = getNextTimeToday(now, nowMidnight);
        if (nextTimeToday != null) {
            return nextTimeToday;
        }/*  w w  w.  j  a v a2  s  .  c o  m*/
    }
    DateTime nextDay = getNextScheduleDay(nowMidnight.plusDays(1));
    return getFirstScheduledTimeOnDay(nextDay);
}