Example usage for org.joda.time DateTimeFieldType dayOfMonth

List of usage examples for org.joda.time DateTimeFieldType dayOfMonth

Introduction

In this page you can find the example usage for org.joda.time DateTimeFieldType dayOfMonth.

Prototype

public static DateTimeFieldType dayOfMonth() 

Source Link

Document

Get the day of month field type.

Usage

From source file:com.alliander.osgp.adapter.protocol.oslp.elster.application.mapping.OslpGetConfigurationResponseToConfigurationConverter.java

License:Open Source License

/**
 * For a given Month of this year, find the date for the weekday {@link day}
 * ./*from   w ww .j  ava  2  s  .  c o m*/
 */
private int getLastDayOfMonth(final int month, final int day) {
    final DateTime dateTime = DateTime.now();
    MutableDateTime x = dateTime.toMutableDateTime();
    x.set(DateTimeFieldType.monthOfYear(), month);
    x.set(DateTimeFieldType.dayOfMonth(), 31);

    x = this.findLastDayOfOfMonth(day, x);
    return x.getDayOfMonth();
}

From source file:com.alliander.osgp.adapter.protocol.oslp.elster.application.mapping.OslpGetConfigurationResponseToConfigurationConverter.java

License:Open Source License

/**
 * Loop backwards through the days of the month until we find {@link day} of
 * the month. For example the last Sunday of the month March of this year.
 *//*www  . ja va2s. c o  m*/
private MutableDateTime findLastDayOfOfMonth(final int day, final MutableDateTime x) {
    final int yodaTimeDay = day + 1;
    while (true) {
        if (yodaTimeDay == x.getDayOfWeek()) {
            break;
        } else {
            final int dayOfMonth = x.getDayOfMonth() - 1;
            x.set(DateTimeFieldType.dayOfMonth(), dayOfMonth);
        }
    }
    return x;
}

From source file:com.google.code.tickconverter.convert.ConvertAdapter.java

License:Open Source License

/**
 * This method is the main method of the convert process. While the {@link BlockingQueue} of {@link IDukascopyRO}
 * have for 10 seconds no objects in the {@link BlockingQueue} add this method this object into the
 * {@link MetatraderConverter}. If an {@link InvalidTimeException} will threw the converter put a new
 * {@link MetatraderBean} into the {@link BlockingQueue} of {@link IMetatraderRO} and get the information of the
 * values from the {@link MetatraderConverter} object.
 * /* w  w w . ja  va  2s. c om*/
 * @throws InterruptedException will threw if {@link Thread#interrupt()} is called in the poll phase
 * @throws InvalidTimeException will threw if {@link MetatraderConverter#incrementInterval()} have a low range for
 *             the new object after the creation of a new {@link MetatraderBean}
 */
public void convertProcess() throws InterruptedException {
    while (true) {
        IDukascopyRO object = dukaQueue.poll(2, TimeUnit.SECONDS);
        LoggerUtils.createDebugLog("poll object: " + object);
        if (null == object) {
            if (null != converter && converter.hasElements()) {
                putMetatraderObject();
            }

            break;
        }

        if (null == converter) {
            DateTime timestamp = object.getTimeStamp();
            DateTime start = new DateTime(timestamp.get(DateTimeFieldType.year()),
                    timestamp.get(DateTimeFieldType.monthOfYear()),
                    timestamp.get(DateTimeFieldType.dayOfMonth()), timestamp.get(DateTimeFieldType.hourOfDay()),
                    timestamp.get(DateTimeFieldType.minuteOfHour()));
            converter = new MetatraderConverter(start, Period.minutes(1));
        }
        try {
            converter.addDukascopy(object);
        } catch (InvalidTimeException e) {
            putMetatraderObject();
            incrementWhileAdd(object);
        }
    }
}

From source file:com.gst.portfolio.account.data.StandingInstructionData.java

License:Apache License

public Integer recurrenceOnDay() {
    Integer recurrenceOnDay = 0;/*from   w  ww  .  j  a va 2  s.  c om*/
    if (this.recurrenceOnMonthDay != null) {
        recurrenceOnDay = this.recurrenceOnMonthDay.get(DateTimeFieldType.dayOfMonth());
    }
    return recurrenceOnDay;
}

From source file:com.splicemachine.derby.utils.SpliceDateFunctions.java

License:Apache License

/**
 * Implements the trunc_date function//from w  w  w.j a  v a2s .  c  o m
 */
public static Timestamp TRUNC_DATE(Timestamp source, String field) throws SQLException {
    if (source == null || field == null)
        return null;
    DateTime dt = new DateTime(source);
    field = field.toLowerCase();
    String lowerCaseField = field.toLowerCase();
    if ("microseconds".equals(lowerCaseField)) {
        int nanos = source.getNanos();
        nanos = nanos - nanos % 1000;
        source.setNanos(nanos);
        return source;
    } else if ("milliseconds".equals(lowerCaseField)) {
        int nanos = source.getNanos();
        nanos = nanos - nanos % 1000000;
        source.setNanos(nanos);
        return source;
    } else if ("second".equals(lowerCaseField)) {
        source.setNanos(0);
        return source;

    } else if ("minute".equals(lowerCaseField)) {
        DateTime modified = dt.minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(modified.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("hour".equals(lowerCaseField)) {
        DateTime modified = dt.minusMinutes(dt.getMinuteOfHour()).minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(modified.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("day".equals(lowerCaseField)) {
        DateTime modified = dt.minusHours(dt.getHourOfDay()).minusMinutes(dt.getMinuteOfHour())
                .minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(modified.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("week".equals(lowerCaseField)) {
        DateTime modified = dt.minusDays(dt.getDayOfWeek()).minusHours(dt.getHourOfDay())
                .minusMinutes(dt.getMinuteOfHour()).minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(modified.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("month".equals(lowerCaseField)) {
        DateTime modified = dt.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1)
                .minusHours(dt.getHourOfDay()).minusMinutes(dt.getMinuteOfHour())
                .minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(modified.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("quarter".equals(lowerCaseField)) {
        int month = dt.getMonthOfYear();
        DateTime modified = dt;
        if ((month + 1) % 3 == 1) {
            modified = dt.minusMonths(2);
        } else if ((month + 1) % 3 == 0) {
            modified = dt.minusMonths(1);
        }
        DateTime fin = modified.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1)
                .minusHours(dt.getHourOfDay()).minusMinutes(dt.getMinuteOfHour())
                .minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(fin.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("year".equals(lowerCaseField)) {
        DateTime modified = dt.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1)
                .minusHours(dt.getHourOfDay()).minusMonths(dt.getMonthOfYear() - 1)
                .minusMinutes(dt.getMinuteOfHour()).minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(modified.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("decade".equals(lowerCaseField)) {
        DateTime modified = dt.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1)
                .minusYears(dt.getYear() % 10).minusHours(dt.getHourOfDay())
                .minusMonths(dt.getMonthOfYear() - 1).minusMinutes(dt.getMinuteOfHour())
                .minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(modified.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("century".equals(lowerCaseField)) {
        DateTime modified = dt.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1)
                .minusHours(dt.getHourOfDay()).minusYears(dt.getYear() % 100)
                .minusMonths(dt.getMonthOfYear() - 1).minusMinutes(dt.getMinuteOfHour())
                .minusSeconds(dt.getSecondOfMinute());
        Timestamp ret = new Timestamp(modified.getMillis());
        ret.setNanos(0);
        return ret;
    } else if ("millennium".equals(lowerCaseField)) {
        int newYear = dt.getYear() - dt.getYear() % 1000;
        //noinspection deprecation (converstion from joda to java.sql.Timestamp did not work for millennium < 2000)
        return new Timestamp(newYear - 1900, Calendar.JANUARY, 1, 0, 0, 0, 0);
    } else {
        throw new SQLException(String.format("invalid time unit '%s'", field));
    }
}

From source file:de.openali.odysseus.chart.ext.base.axisrenderer.provider.DateTimeAxisFieldProvider.java

License:Open Source License

@Override
public IDateTimeAxisField getDateTimeAxisField(final IDataRange<Number> range) {

    final long dr = range.getMax().longValue() - range.getMin().longValue();
    final long sec = 1000;
    final long min = 60 * sec;
    final long hour = 60 * min;
    final long day = 24 * hour;

    if (dr < 3 * sec)
        return new DateTimeAxisField(DateTimeFieldType.millisOfSecond(), "YYYY.dd.MM\nHH:mm:ss:SSS", //$NON-NLS-1$
                new int[] { 1, 10, 100, 500 }, new int[] {});
    else if (dr < 3 * min)
        return new DateTimeAxisField(DateTimeFieldType.secondOfMinute(), "YY.dd.MM\nHH:mm:ss", //$NON-NLS-1$
                new int[] { 1, 15, 30 }, new int[] {});
    else if (dr < 3 * hour)
        return new DateTimeAxisField(DateTimeFieldType.minuteOfHour(), "YY.dd.MM\nHH:mm:ss", //$NON-NLS-1$
                new int[] { 1, 15, 30 }, new int[] {});
    else if (dr < 3 * day)
        return new DateTimeAxisField(DateTimeFieldType.minuteOfDay(), "dd.MM\nHH:mm", new int[] { 1, 15, 30 }, //$NON-NLS-1$
                new int[] {});
    else if (dr < 7 * day)
        return new DateTimeAxisField(DateTimeFieldType.hourOfDay(), "dd.MM\nHH:mm", //$NON-NLS-1$
                new int[] { 1, 2, 4, 6, 8, 12 }, new int[] { 12 });
    else if (dr < 30 * day)
        return new DateTimeAxisField(DateTimeFieldType.dayOfMonth(), "YYYY.dd.MM\ndddd", //$NON-NLS-1$
                new int[] { 1, 2, 7, 14 }, new int[] { 7, 14, 28 });
    else/*from ww w.  j a  v  a  2s .c o  m*/
        return new DateTimeAxisField(DateTimeFieldType.monthOfYear(), "YYYY.dd.MM", new int[] { 1, 2, 3, 4, 6 }, //$NON-NLS-1$
                new int[] { 6 });
}

From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java

License:Open Source License

public void setField(Granulaarsus field, int value) {
    // ------ Time
    if (field == Granulaarsus.AM_PM) {
        try {//w w  w.j  a va2 s. c  o  m
            this.underlyingTime = (this.underlyingTime).withField(DateTimeFieldType.halfdayOfDay(), value);
            updateTimeRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    if (field == Granulaarsus.HOUR_OF_HALF_DAY) {
        try {
            if (value == 12) {
                value = 0;
            }
            this.underlyingTime = (this.underlyingTime).withField(DateTimeFieldType.hourOfHalfday(), value);
            // NB! Tunni seadistamisel nullime ka minutid, et ei tekiks nt ankurdamisel kummalisi vrtuseid
            this.underlyingTime = (this.underlyingTime).withField(DateTimeFieldType.minuteOfHour(), 0);
            updateTimeRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    if (field == Granulaarsus.MINUTE) {
        try {
            this.underlyingTime = (this.underlyingTime).withField(DateTimeFieldType.minuteOfHour(), value);
            updateTimeRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    // ------ Kuup2evad ja n2dalad
    if (field == Granulaarsus.DAY_OF_WEEK) {
        try {
            this.underlyingDate = (this.underlyingDate).withField(DateTimeFieldType.dayOfWeek(), value);
            dateModified = true;
            updateDateRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    if (field == Granulaarsus.WEEK_OF_YEAR) {
        try {
            this.underlyingDate = (this.underlyingDate).withField(DateTimeFieldType.weekOfWeekyear(), value);
            dateModified = true;
            updateDateRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    if (field == Granulaarsus.DAY_OF_MONTH) {
        try {
            this.underlyingDate = (this.underlyingDate).withField(DateTimeFieldType.dayOfMonth(), value);
            dateModified = true;
            updateDateRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    if (field == Granulaarsus.MONTH) {
        try {
            this.underlyingDate = (this.underlyingDate).withField(DateTimeFieldType.monthOfYear(), value);
            dateModified = true;
            updateDateRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    if (field == Granulaarsus.YEAR) {
        try {
            this.underlyingDate = (this.underlyingDate).withField(DateTimeFieldType.year(), value);
            dateModified = true;
            updateDateRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    if (field == Granulaarsus.YEAR_OF_CENTURY) {
        try {
            this.underlyingDate = (this.underlyingDate).withField(DateTimeFieldType.yearOfCentury(), value);
            dateModified = true;
            // NB! Toimib nagu tavalise aastaarvu muutmine
            updateDateRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
    if (field == Granulaarsus.CENTURY_OF_ERA) {
        try {
            this.underlyingDate = (this.underlyingDate).withField(DateTimeFieldType.centuryOfEra(), value);
            dateModified = true;
            updateDateRepresentation(field, null, false, SET_TYPE_OPERATION);
        } catch (Exception e) {
        }
    }
}

From source file:gg.db.datamodel.Periods.java

License:Open Source License

/**
 * Gets the adjusted start date: the date is adjusted to the first day of the period<BR/>
 * Depending on periodType, the date will be:
 * <UL>//  w w w.  j a  va2  s. c o m
 * <LI>First day of the week (monday)</LI>
 * <LI>First day of the month</LI>
 * <LI>First day of the year</LI>>
 * </UL>
 * Example:
 * <UL>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.DAY) should return: <B>05/20/2006</B></LI>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.WEEK) should return: <B>05/15/2006</B> (if Configuration.getWeekStartingOn() == MONDAY)</LI>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.MONTH) should return: <B>05/01/2006</B></LI>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.YEAR) should return: <B>01/01/2006</B></LI>
 * <LI>getAdjustedStartDate(new LocalDate(2006, 5, 20), PeriodType.FREE) should return: <B>05/20/2006</B></LI>
 * </UL>
 * @param date Date to adjust to the start of the period
 * @param periodType Type of the Period to adjust the period
 * @return Date adjusted to the start of the period
 */
public static LocalDate getAdjustedStartDate(LocalDate date, PeriodType periodType) {
    if (date == null || periodType == null) {
        throw new IllegalArgumentException("One of the following parameters is null: 'date', 'periodType'");
    }

    LocalDate adjustedStartDate = null;
    switch (periodType) {
    case DAY: // No adjustement to do
        adjustedStartDate = date;
        break;
    case FREE: // No adjustement to do
        adjustedStartDate = date;
        break;
    case WEEK: // Adjust the date to the first day of the week (Monday)
        adjustedStartDate = date.toDateMidnight()
                .withField(DateTimeFieldType.dayOfWeek(), DateTimeConstants.MONDAY).toLocalDate();
        break;
    case MONTH: // Adjust the date to the first day of the month (<MONTH>/01/<YEAR>)
        adjustedStartDate = date.withField(DateTimeFieldType.dayOfMonth(), 1);
        break;
    case YEAR: // Adjust the date to the first day of the year (01/01/<YEAR>)
        adjustedStartDate = date.withField(DateTimeFieldType.dayOfMonth(), 1)
                .withField(DateTimeFieldType.monthOfYear(), 1);
        break;
    default:
        throw new AssertionError("Unknown PeriodType"); // should never happen
    }

    assert (adjustedStartDate != null);
    return adjustedStartDate;
}

From source file:gg.db.datamodel.Periods.java

License:Open Source License

/**
 * Gets the adjusted end date: the date is adjusted to the last day of the period<BR/>
 * Depending on periodType, the date will be:
 * <UL>/*from   w  ww.j a  va  2s  .  c om*/
 * <LI>Last day of the week (sunday)</LI>
 * <LI>Last day of the month</LI>
 * <LI>Last day of the year</LI>>
 * </UL>
 * Example:
 * <UL>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.DAY) should return: <B>05/20/2006</B></LI>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.WEEK) should return: <B>05/21/2006</B> (if Configuration.getWeekStartingOn() == MONDAY)</LI>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.MONTH) should return: <B>05/31/2006</B></LI>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.YEAR) should return: <B>12/31/2006</B></LI>
 * <LI>getAdjustedEndDate(new LocalDate(2006, 5, 20), PeriodType.FREE) should return: <B>05/20/2006</B></LI>
 * </UL>
 * @param date Date to adjust to the end of the period
 * @param periodType Type of the Period to adjust the date
 * @return Date adjusted to the end of the period
 */
public static LocalDate getAdjustedEndDate(LocalDate date, PeriodType periodType) {
    if (date == null || periodType == null) {
        throw new IllegalArgumentException("One of the following parameters is null: 'date', 'periodType'");
    }

    LocalDate adjustedEndDate = null;
    switch (periodType) {
    case DAY: // No adjustement to do
        adjustedEndDate = date;
        break;
    case FREE: // No adjustement to do
        adjustedEndDate = date;
        break;
    case WEEK: // Adjust the date to the last day of the week (sunday)
        adjustedEndDate = date.toDateMidnight()
                .withField(DateTimeFieldType.dayOfWeek(), DateTimeConstants.MONDAY + 6).toLocalDate();
        break;
    case MONTH: // Adjust the date to the last day of the month
        adjustedEndDate = date.withField(DateTimeFieldType.dayOfMonth(), 1).plusMonths(1).minusDays(1);
        break;
    case YEAR: // Adjust the date to the last day of the year (12/31/<YEAR>)
        adjustedEndDate = date.withField(DateTimeFieldType.dayOfMonth(), 1).plusMonths(1).minusDays(1)
                .withField(DateTimeFieldType.monthOfYear(), 12);
        break;
    default:
        throw new AssertionError("Unknown PeriodType"); // should never happen
    }

    assert (adjustedEndDate != null);
    return adjustedEndDate;
}

From source file:net.karlmartens.platform.datatable.DataTableCell.java

License:Apache License

private static JsonElement creasteJsonElement(Object o) {
    if (o == null)
        return JsonNull.INSTANCE;

    if (o instanceof String)
        return new JsonPrimitive((String) o);

    if (o instanceof Boolean)
        return new JsonPrimitive((Boolean) o);

    if (o instanceof Number)
        return new JsonPrimitive((Number) o);

    if (o instanceof BaseLocal) {
        final BaseLocal date = (BaseLocal) o;
        final JsonObject json = new JsonObject();
        json.add("year", createJsonElement(DateTimeFieldType.year(), date));
        json.add("month", createJsonElement(DateTimeFieldType.monthOfYear(), date));
        json.add("day", createJsonElement(DateTimeFieldType.dayOfMonth(), date));
        json.add("hour", createJsonElement(DateTimeFieldType.hourOfDay(), date));
        json.add("minute", createJsonElement(DateTimeFieldType.minuteOfHour(), date));
        json.add("second", createJsonElement(DateTimeFieldType.secondOfMinute(), date));
        json.add("millis", createJsonElement(DateTimeFieldType.millisOfSecond(), date));
        return json;
    }//from w w  w  . j  a  v a2s .co  m

    throw new UnsupportedOperationException();
}