Example usage for org.joda.time DateTime getMillisOfSecond

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

Introduction

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

Prototype

public int getMillisOfSecond() 

Source Link

Document

Get the millis of second field value.

Usage

From source file:com.ning.billing.util.clock.ClockMock.java

License:Apache License

private DateTime truncate(final DateTime time) {
    return time.minus(time.getMillisOfSecond());
}

From source file:com.ning.billing.util.clock.DefaultClock.java

License:Apache License

public static DateTime truncateMs(final DateTime input) {
    return input.minus(input.getMillisOfSecond());
}

From source file:com.qcadoo.model.internal.types.DateType.java

License:Open Source License

@Override
public ValueAndError toObject(final FieldDefinition fieldDefinition, final Object value) {
    if (value instanceof Date) {
        return ValueAndError.withoutError(value);
    }//from w  w w.  jav a 2 s. com
    try {
        DateTimeFormatter fmt = DateTimeFormat.forPattern(DateUtils.L_DATE_FORMAT);
        DateTime dt = fmt.parseDateTime(String.valueOf(value));

        int year = dt.getYear();
        if (year < 1500 || year > 2500) {
            return ValueAndError.withError("qcadooView.validate.field.error.invalidDateFormat.range");
        }

        Date date = dt.toDate();

        if (year < 2000) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, dt.getYear());
            c.set(Calendar.MONTH, dt.getMonthOfYear() - 1);
            c.set(Calendar.DAY_OF_MONTH, dt.getDayOfMonth());
            c.set(Calendar.HOUR_OF_DAY, dt.hourOfDay().get());
            c.set(Calendar.MINUTE, dt.getMinuteOfHour());
            c.set(Calendar.SECOND, dt.getSecondOfMinute());
            c.set(Calendar.MILLISECOND, dt.getMillisOfSecond());
            date = c.getTime();
        }

        return ValueAndError.withoutError(date);
    } catch (IllegalArgumentException e) {
        return ValueAndError.withError("qcadooView.validate.field.error.invalidDateFormat");
    }
}

From source file:com.sap.dirigible.runtime.metrics.TimeUtils.java

License:Open Source License

private static DateTime dateTimeCeiling(DateTime dt, Period p) {
    if (p.getYears() != 0) {
        return dt.yearOfEra().roundCeilingCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundCeilingCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundCeilingCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundCeilingCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundCeilingCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundCeilingCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundCeilingCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }/*from  ww w.jav  a2s.  com*/
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:com.serotonin.mango.util.DateUtils.java

License:Open Source License

public static DateTime truncateDateTime(DateTime time, int periodType) {
    if (periodType == TimePeriods.SECONDS)
        time = time.minus(time.getMillisOfSecond());
    else if (periodType == TimePeriods.MINUTES) {
        time = time.minus(time.getMillisOfSecond());
        time = time.minus(Common.getPeriod(TimePeriods.SECONDS, time.getSecondOfMinute()));
    } else if (periodType == TimePeriods.HOURS) {
        time = time.minus(time.getMillisOfSecond());
        time = time.minus(Common.getPeriod(TimePeriods.SECONDS, time.getSecondOfMinute()));
        time = time.minus(Common.getPeriod(TimePeriods.MINUTES, time.getMinuteOfHour()));
    } else if (periodType == TimePeriods.DAYS) {
        time = time.minus(time.getMillisOfDay());
    } else if (periodType == TimePeriods.WEEKS) {
        time = time.minus(time.getMillisOfDay());
        time = time.minus(Common.getPeriod(TimePeriods.DAYS, time.getDayOfWeek() - 1));
    } else if (periodType == TimePeriods.MONTHS) {
        time = time.minus(time.getMillisOfDay());
        time = time.minus(Common.getPeriod(TimePeriods.DAYS, time.getDayOfMonth() - 1));
    } else if (periodType == TimePeriods.YEARS) {
        time = time.minus(time.getMillisOfDay());
        time = time.minus(Common.getPeriod(TimePeriods.DAYS, time.getDayOfYear() - 1));
    }//from w w  w  . j  av  a 2 s .  co m
    return time;
}

From source file:com.sqewd.os.maracache.api.utils.TimeUtils.java

License:Apache License

/**
 * Get the time bucket for the input date based on the Time Unit and Unit multiplier specified.
 *
 * @param dt         - Date time to bucket.
 * @param unit       - Time Unit/*from  ww w  . ja  va 2  s . co  m*/
 * @param multiplier - Unit multiplier.
 * @return
 */
public static DateTime bucket(DateTime dt, TimeUnit unit, int multiplier) {
    DateTime w = null;
    switch (unit) {
    case MILLISECONDS:
        int ms = (dt.getMillisOfSecond() / multiplier) * multiplier;
        w = dt.secondOfMinute().roundFloorCopy().plusMillis(ms);
        break;
    case SECONDS:
        int s = (dt.getSecondOfMinute() / multiplier) * multiplier;
        w = dt.minuteOfHour().roundFloorCopy().plusSeconds(s);
        break;
    case MINUTES:
        int m = (dt.getMinuteOfHour() / multiplier) * multiplier;
        w = dt.hourOfDay().roundFloorCopy().plusMinutes(m);
        break;
    case HOURS:
        int h = (dt.getHourOfDay() / multiplier) * multiplier;
        w = dt.dayOfYear().roundFloorCopy().plusHours(h);
        break;
    case DAYS:
        int d = (dt.getDayOfYear() / multiplier) * multiplier;
        // Need to subtract (1) as the start offset i
        if (dt.getDayOfYear() % multiplier == 0) {
            d -= 1;
        }
        w = dt.yearOfCentury().roundFloorCopy().plusDays(d);
        break;
    }
    return w;
}

From source file:com.yourmediashelf.fedora.util.DateUtility.java

License:Open Source License

public static DateTimeFormatter getXSDFormatter(DateTime date) {
    int len = 0;//from  w  w  w . j ava  2  s  .c  om
    int millis = date.getMillisOfSecond();

    if (millis > 0) {
        // 0.050 becomes .05 (up to three digits, dropping trailing 0s)
        DecimalFormat df = new DecimalFormat(".###");
        double d = millis / 1000.0;
        len = String.valueOf(df.format(d)).length() - 1;
    }
    return getXSDFormatter(len);
}

From source file:edu.illinois.cs.cogcomp.temporal.normalizer.main.timex2interval.Period.java

License:Open Source License

/**
 * This function deals with a period of time with format the xxth day/month, etc
 * @param start the anchor time// w w w .j a  v a2s. co m
 * @param temporalPhrase
  * @return
  */
public static TimexChunk Periodrule(DateTime start, TemporalPhrase temporalPhrase) {

    int year;
    DateTime finish;
    String temp1;
    String temp2;
    Interval interval;
    interval = new Interval(start, start);
    String phrase = temporalPhrase.getPhrase();
    phrase = phrase.toLowerCase();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");

    int modiword = 0;// 0 :no modified words 1:early,ealier 2:late,later
    // Handle some special cases
    TimexChunk tc = new TimexChunk();
    tc.addAttribute(TimexNames.type, TimexNames.DATE);
    if (phrase.contains("now") || phrase.contains("currently") || phrase.contains("current")
            || phrase.contains("today")) {
        DateTime virtualStart = interval.getStart();
        virtualStart = new DateTime(virtualStart.getYear(), virtualStart.getMonthOfYear(),
                virtualStart.getDayOfMonth(), virtualStart.getHourOfDay(), virtualStart.getMinuteOfHour(),
                virtualStart.getSecondOfMinute(), virtualStart.getMillisOfSecond() + 1);
        tc.addAttribute(TimexNames.value, TimexNames.PRESENT_REF);
        return tc;
    }
    if (phrase.contains("early") || phrase.contains("earlier")) {
        modiword = 1;
    }
    if (phrase.contains("late") || phrase.contains("later")) {
        modiword = 2;
    }

    String units = "";

    for (String unitStr : dateUnitSet) {
        units = units + unitStr + "|";
    }
    units += "s$";

    String patternStr = "(?:the)?\\s*(\\d{1,4})(?:th|nd|st|rd)\\s*(" + units + ")\\s*";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(phrase);
    boolean matchFound = matcher.find();
    if (matchFound) {
        temp1 = matcher.group(1);
        temp2 = matcher.group(2);

        String residual = StringUtils.difference(matcher.group(0), phrase);
        String anchorStr = "";
        if (residual.length() > 0) {
            TemporalPhrase anchorPhrase = new TemporalPhrase(residual, temporalPhrase.getTense());
            TimexChunk anchorTimex = TimexNormalizer.normalize(anchorPhrase);
            if (anchorTimex != null) {
                anchorStr = anchorTimex.getAttribute(TimexNames.value);
            }
        }

        if (temp2.equals("century")) {
            year = (Integer.parseInt(temp1) - 1) * 100;
            start = new DateTime(year, 1, 1, 0, 0, 0, 0);
            finish = new DateTime(year + 99, 12, 31, 23, 59, 59, 59);
            tc.addAttribute(TimexNames.value, String.valueOf(finish.getCenturyOfEra()));
            return tc;
        }

        else if (temp2.equals("decade")) {
            // e.g.: 3rd decade (of this century)
            // first we get this century is 20, then the 3rd decade is 203
            int anchorCentury = start.getCenturyOfEra();
            String val = String.valueOf(anchorCentury * 10 + temp1);
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("year")) {
            int anchorCentury = start.getCenturyOfEra();
            String val = String.valueOf(anchorCentury * 100 + temp1);
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("quarter")) {
            int anchorYear = start.getYear();
            String val = String.valueOf(anchorYear) + "-Q" + temp1;
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("month")) {
            int anchorYear = start.getYear();
            String monthStr = Integer.parseInt(temp1) < 10 ? "0" + temp1 : temp1;
            String val = String.valueOf(anchorYear) + "-" + monthStr;
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("day")) {
            String val = "";
            if (anchorStr.length() > 0) {
                List<String> normTimexList = Period.normTimexToList(anchorStr);
                String anchorYear = normTimexList.get(0);
                String anchorDate;
                String anchorMonth;
                if (normTimexList.size() == 1 || Integer.parseInt(temp1) > 31) {
                    anchorMonth = "01";
                } else {
                    anchorMonth = normTimexList.get(1);
                }
                DateTime normDateTime = new DateTime(Integer.parseInt(anchorYear),
                        Integer.parseInt(anchorMonth), 1, 0, 0);
                normDateTime = normDateTime.minusDays(-1 * Integer.parseInt(temp1));
                anchorYear = String.valueOf(normDateTime.getYear());
                anchorMonth = String.valueOf(normDateTime.getMonthOfYear());
                anchorDate = String.valueOf(normDateTime.getDayOfMonth());
                anchorMonth = anchorMonth.length() == 1 ? "0" + anchorMonth : anchorMonth;
                anchorDate = anchorDate.length() == 1 ? "0" + anchorDate : anchorDate;
                val = anchorYear + "-" + anchorMonth + "-" + anchorDate;

            } else {
                int month = Integer.parseInt(temp1) > 31 ? 1 : start.getMonthOfYear();
                DateTime normDateTime = new DateTime(start.getYear(), month, 1, 0, 0);
                normDateTime = normDateTime.minusDays(-1 * Integer.parseInt(temp1));
                String anchorYear = String.valueOf(normDateTime.getYear());
                String anchorMonth = String.valueOf(normDateTime.getMonthOfYear());
                String anchorDate = String.valueOf(normDateTime.getDayOfMonth());
                anchorMonth = anchorMonth.length() == 1 ? "0" + anchorMonth : anchorMonth;
                anchorDate = anchorDate.length() == 1 ? "0" + anchorDate : anchorDate;
                val = String.valueOf(anchorYear) + "-" + anchorMonth + "-" + anchorDate;
            }
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("s")) {

            if (Integer.parseInt(temp1) < 100) {
                year = start.getCenturyOfEra();
                year = year * 100 + Integer.parseInt(temp1);
                if (modiword == 0) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                }

                else if (modiword == 1) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.START);
                }

                else if (modiword == 2) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.END);
                }

                return tc;
            }

            else {
                if (modiword == 0) {
                    start = new DateTime(Integer.parseInt(temp1), 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 9, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                }

                else if (modiword == 1) {
                    start = new DateTime(Integer.parseInt(temp1), 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 3, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.START);
                }

                else if (modiword == 2) {
                    start = new DateTime(Integer.parseInt(temp1) + 7, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 9, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.END);
                }
                return tc;

            }
        }

    }
    return null;
}

From source file:es.ucm.fdi.dalgs.academicTerm.service.AcademicTermService.java

License:Open Source License

@PreAuthorize("hasRole('ROLE_ADMIN')")
@Transactional(readOnly = false)/*from   w  w w  . j  a  v a2s  . c  o m*/
public ResultClass<AcademicTerm> copyAcademicTerm(AcademicTerm academicTerm, Locale locale) {
    AcademicTerm copy = academicTerm.depth_copy();

    ResultClass<AcademicTerm> result = new ResultClass<>();

    if (copy == null) {
        result.setHasErrors(true);
        Collection<String> errors = new ArrayList<String>();
        errors.add("Copy doesn't work");
        result.setErrorsList(errors);

    } else {
        DateTime time = new DateTime();

        copy.setTerm(copy.getTerm() + " " + time.getMillisOfSecond());

        for (Course c : copy.getCourses()) {
            for (Activity a : c.getActivities()) {

                a.getInfo().setCode(a.getInfo().getCode() + "  " + time.getMillisOfSecond());
            }

            for (Group g : c.getGroups()) {
                g.setName(g.getName() + time.getMillisOfSecond());
                for (Activity a : g.getActivities()) {

                    a.getInfo().setCode(a.getInfo().getCode() + "  " + time.getMillisOfSecond());
                }
            }

        }

        boolean success = repositoryAcademicTerm.addAcademicTerm(copy);
        if (success) {
            AcademicTerm exists = repositoryAcademicTerm.exists(copy.getTerm(), copy.getDegree());
            if (exists != null) {
                result.setSingleElement(exists);
                manageAclService.addACLToObject(exists.getId(), exists.getClass().getName());

                for (Course c : exists.getCourses()) {
                    success = success && manageAclService.addACLToObject(c.getId(), c.getClass().getName());

                    for (Activity a : c.getActivities()) {
                        success = success && manageAclService.addACLToObject(a.getId(), a.getClass().getName());
                    }

                    for (Group g : c.getGroups()) {
                        success = success && manageAclService.addACLToObject(g.getId(), g.getClass().getName());

                        for (Activity a : g.getActivities()) {
                            success = success
                                    && manageAclService.addACLToObject(a.getId(), a.getClass().getName());
                        }
                    }
                }
            }
        }

        result.setHasErrors(!success);

    }
    return result;
}

From source file:es.ucm.fdi.dalgs.group.service.GroupService.java

License:Open Source License

@PreAuthorize("hasRole('ROLE_ADMIN')")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public ResultClass<Group> copyGroup(Group group, Long id_course, Locale locale) {

    ResultClass<Group> result = new ResultClass<Group>();
    Group copy = group.depth_copy();

    if (copy == null) {
        result.setHasErrors(true);/*from   w  w  w  .ja  va 2s  .co  m*/
        Collection<String> errors = new ArrayList<String>();
        errors.add("Copy doesn't work");
        result.setErrorsList(errors);

    } else {
        DateTime time = new DateTime();

        copy.setName(copy.getName() + "  " + time.getMillisOfSecond());

        for (Activity a : copy.getActivities()) {
            a.getInfo().setCode(a.getInfo().getCode() + "  " + time.getMillisOfSecond());

        }

        boolean success = repositoryGroup.addGroup(copy);
        if (success) {
            Group exists = repositoryGroup.existInCourse(id_course, copy.getName());

            if (exists != null) {
                result.setSingleElement(exists);
                manageAclService.addACLToObject(exists.getId(), exists.getClass().getName());

                for (Activity a : exists.getActivities()) {
                    success = success && manageAclService.addACLToObject(a.getId(), a.getClass().getName());

                }
            }
        }

        result.setHasErrors(!success);

    }
    return result;

}