Example usage for org.joda.time DateTime getMillis

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

Introduction

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

Prototype

public long getMillis() 

Source Link

Document

Gets the milliseconds of the datetime instant from the Java epoch of 1970-01-01T00:00:00Z.

Usage

From source file:com.boha.golfkids.util.NewGolfGroupUtil.java

private static Date getDateOfBirth(int age) {

    DateTime dt = new DateTime();
    dt = dt.minusYears(age);//from w  w w.  ja v a2s.  c o m
    Random rand = new Random(System.currentTimeMillis());
    int mth = rand.nextInt(dt.getMonthOfYear());
    int day = rand.nextInt(28);
    if (mth == 0) {
        mth = 1;
    }
    if (day == 0) {
        day = 1;
    }
    DateTime x = new DateTime(dt.getYear(), mth, day, 0, 0);
    return new Date(x.getMillis());
}

From source file:com.boha.monitor.util.DataUtil.java

public ResponseDTO getServerErrors(long startDate, long endDate) throws DataException {
    ResponseDTO r = new ResponseDTO();
    if (startDate == 0) {
        DateTime ed = new DateTime();
        DateTime sd = ed.minusMonths(3);
        startDate = sd.getMillis();
        endDate = ed.getMillis();/*from   ww  w  .  j a v  a2  s.  c  o m*/
    }
    try {
        Query q = em.createNamedQuery("ErrorStore.findByPeriod", ErrorStore.class);
        q.setParameter("startDate", new Date(startDate));
        q.setParameter("endDate", new Date(endDate));
        List<ErrorStore> list = q.getResultList();
        List<ErrorStoreDTO> dList = new ArrayList();
        for (ErrorStore e : list) {
            dList.add(new ErrorStoreDTO(e));
        }
        r.setErrorStoreList(dList);
        log.log(Level.OFF, "Errors found {0}", r.getErrorStoreList().size());
    } catch (Exception e) {
        log.log(Level.SEVERE, "Failed to getServerErrors");
        throw new DataException("Failed to getServerErrors\n" + getErrorString(e));
    }
    return r;
}

From source file:com.boha.proximity.util.DataUtil.java

public ResponseDTO getServerEvents(long startDate, long endDate) throws DataException {
    ResponseDTO r = new ResponseDTO();
    if (startDate == 0) {
        DateTime ed = new DateTime();
        DateTime sd = ed.minusMonths(3);
        startDate = sd.getMillis();
        endDate = ed.getMillis();//from   w  ww .  j ava 2  s  . c  om
    }
    try {
        Query q = em.createNamedQuery("ErrorStoreAndroid.findByPeriod", ErrorStoreAndroid.class);
        q.setParameter("from", new Date(startDate));
        q.setParameter("to", new Date(endDate));
        List<ErrorStoreAndroid> list = q.getResultList();
        List<ErrorStoreAndroidDTO> dList = new ArrayList();
        for (ErrorStoreAndroid e : list) {
            dList.add(new ErrorStoreAndroidDTO(e));
        }
        r.setErrorStoreAndroidList(dList);
        r.setErrorStoreList(getServerErrors(startDate, endDate).getErrorStoreList());

        String logx = LogfileUtil.getFileString();
        r.setLog(logx);
        log.log(Level.OFF, "Android Errors found {0}", r.getErrorStoreAndroidList().size());
    } catch (DataException | IOException e) {
        log.log(Level.SEVERE, "Failed to findClubsWithinRadius");
        throw new DataException("Failed to findClubsWithinRadius\n" + getErrorString(e));
    }
    return r;
}

From source file:com.brandboat.loader.RandomDataUtil.java

public static DateTime randomDateTime(DateTime minDt, DateTime maxDt) {
    long min = minDt.getMillis();
    long max = maxDt.getMillis();
    return randomDateTime(min, max);
}

From source file:com.bskyb.cg.environments.message.AuditdMessageFormat.java

License:Apache License

@Override
public Message parse(byte[] inMessage) throws ParseException {
    Message message = new LogMessage();
    String textMessage = new String(inMessage);

    StringTokenizer st = new StringTokenizer(textMessage, delimiter);
    String msgType = (String) st.nextElement();
    String timeStamp = (String) st.nextElement();
    DateTime longDate = null;
    try {//w w  w . j  a v a  2 s  . c  o  m
        longDate = parseDate(timeStamp);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (longDate == null) {
        return message;
    }
    String year = new Integer(longDate.getYear()).toString();
    String month = String.format("%02d", new Integer(longDate.getMonthOfYear()));
    String day = String.format("%02d", new Integer(longDate.getDayOfMonth()));

    String hostName = (String) st.nextElement();

    long epochTime = longDate.getMillis();

    UUID uuid = TimeUUIDUtils.getTimeUUID(epochTime);

    String filename = year + "-" + month + "-" + day + "_" + hostName + "_" + msgType + "_" + uuid.toString()
            + "_" + new Long(epochTime).toString();

    String messageText = (String) st.nextElement();
    message.setKey(filename);
    message.setMessage(messageText.getBytes());

    return message;

}

From source file:com.carmatech.cassandra.CorbaTimeUUID.java

License:Apache License

/**
 * Generate a new, unique UUID based on the provided date-time.
 * /*from   ww w .jav a  2s. c om*/
 * @param dateTime
 *            date-time used for the "time" component of the UUID.
 */
public static UUID createUUID(final DateTime dateTime) {
    return createUUID(dateTime.getMillis());
}

From source file:com.carmatech.cassandra.CorbaTimeUUID.java

License:Apache License

/**
 * WARNING: returned UUID is not unique. Get the UUID corresponding to the provided date-time and the clock sequence, which depends on the IP and MAC
 * addresses of the current machine, and a random component per process/JVM.
 * /*ww  w .  ja v  a 2  s  .  c  o m*/
 * @param dateTime
 *            date-time used for the "time" component of the UUID.
 */
public static UUID toUUID(final DateTime dateTime) {
    return toUUID(dateTime.getMillis());
}

From source file:com.catify.processengine.core.util.TimerUtil.java

License:Apache License

/**
 * Calculates the time stamp in millis for a 
 * given ISO 8601 date (e.g. 2013-04-09T16:34:08Z). 
 * The date must have a time zone.//from w  w  w . j a  v  a2 s.c  o m
 * 
 * @param isoDate ISO 8601 date as {@link String}
 * @return time stamp in millis
 */
public static long calculateTimeToFireForDate(String isoDate) {
    DateTime time = dateFormatter.parseDateTime(isoDate);
    return time.getMillis();
}

From source file:com.catify.processengine.core.util.TimerUtil.java

License:Apache License

/**
 * Calculates a list of time stamps in millis. For each
 * repetition a value will be calculated based on the time
 * to fire time of the previous repetition. If the 'R' is 
 * empty (e.g. R/PT1M), which means an unbounded number of repetitions,
 * the next time to fire is calculated.<br/><br/>
 * //w  ww  .  j  a  va2s  .  co  m
 * You can have the following scenarios:<br/>
 * Cycle with duration and bounded repetitions: R5/PT1M<br/>
 * Cycle with duration and unbounded repetitions: R/PT1M<br/>
 * Cycle with duration, bounded repetitions and start date: R3/2013-04-09T16:34:08Z/P1D<br/> 
 * Cycle with duration, unbounded repetitions and start date: R/2013-04-09T16:34:08Z/P1D<br/>
 * Cycle with duration, bounded repetitions and end date: R3/PT1H/2013-01-30T23:01:00Z<br/> 
 * Cycle with duration, unbounded repetitions and end date: R/PT1H/2013-01-30T23:01:00Z<br/>
 * 
 * @param now actual time stamp in millis
 * @param isoDate as {@link String} e.g. R7/2013-04-09T16:34:08Z/P1D
 * @return {@link List} of time stamps in millis
 */
public static List<Long> calculateTimeToFireForCycle(long now, String isoDate) {
    List<Long> result = new ArrayList<Long>();

    String[] split = isoDate.split("/");
    if (split.length < 2 || split.length > 3) {
        throw new IllegalArgumentException("A ISO 8601 date for a cylce shout have a repeat and duration "
                + "section, or a repeat, date and duration section separated by a slash (e.g R5/PT3M).");
    }

    // repeat is always the first one
    String repeat = split[0];
    // get the repeats
    if (!repeat.startsWith("R")) {
        throw new IllegalArgumentException(
                "A ISO 8601 repeat should start with" + " a 'R', followed by the number of cycles.");
    }
    // get all after the 'R'
    repeat = repeat.substring(1);
    boolean unbounded = false;
    if (repeat.equals("")) {
        /*
         * if we have a unbounded number of repetitions,
         * calculate the next fire time.
         */
        repeat = "1";
        unbounded = true;
    }
    int r = Integer.parseInt(repeat);
    DateTime baseTime = new DateTime(now);

    if (split.length == 2) {
        Period period = periodformatter.parsePeriod(split[1]);
        // calculate the timestamps for the cycles
        for (int i = 0; i < r; i++) {
            baseTime = baseTime.plus(period);
            result.add(baseTime.getMillis());
        }
    }

    // we have start or end date
    if (split.length == 3) {

        if (split[1].startsWith("P")) {
            /*
             * end date -- e.g. R4/PT1H/2013-01-30T23:00:00Z
             */
            DateTime end = dateFormatter.parseDateTime(split[2]);
            Period period = periodformatter.parsePeriod(split[1]);
            if (unbounded) {
                /*
                 * --> R/PT1H/2013-01-30T23:00:00Z <--
                 * calculate all times to fire, until the 
                 * end date is reached.
                 */
                while (baseTime.isBefore(end)) {
                    baseTime = baseTime.plus(period);
                    result.add(baseTime.getMillis());
                }
            } else {
                /*
                 * --> R4/PT1H/2013-01-30T23:00:00Z <--
                 * calculate all times to fire until the 
                 * end date, or the maximum number
                 * of cycles is reached.
                 */
                for (int i = 0; i < r; i++) {
                    baseTime = baseTime.plus(period);
                    if (baseTime.isBefore(end)) {
                        result.add(baseTime.getMillis());
                    }
                }
            }
        } else if (split[2].startsWith("P")) {
            /*
             * start date -- e.g. R/2013-04-09T16:34:08Z/P1D
             */
            DateTime start = dateFormatter.parseDateTime(split[1]);
            Period period = periodformatter.parsePeriod(split[2]);
            if (unbounded) {
                /*
                 * --> R/2013-04-09T16:34:08Z/P1D <--
                 * unbounded cycle with start date
                 */
                if (start.isBefore(now)) {
                    /*
                     * if the start date is in the past,
                     * calculate the next time to fire based
                     * on the 'now' time.
                     */
                    baseTime = baseTime.plus(period);
                    result.add(baseTime.getMillis());
                } else {
                    /* 
                     * if the start date is in the future,
                     * the first time to fire is the start
                     * date. 
                     */
                    result.add(start.getMillis());
                }
            } else {
                /*
                 * --> R7/2013-04-09T16:34:08Z/P1D <--
                 * bounded cycle with start date
                 */
                baseTime = new DateTime(start.getMillis());
                // the first entry is the start date
                result.add(baseTime.getMillis());
                for (int i = 0; i < r; i++) {
                    baseTime = baseTime.plus(period);
                    result.add(baseTime.getMillis());
                }
            }
        } else {
            throw new IllegalArgumentException(
                    "Either the middle or last section of a cycle with start or end date must define a duration.");
        }

    }

    return result;
}

From source file:com.catify.processengine.core.util.TimerUtil.java

License:Apache License

/**
 * Calculate the next time to fire time stamp (in millis) 
 * based on the given 'now' time, plus the ISO 8601 duration.
 * /*from w w  w. j  ava 2  s.c  o m*/
 * @param now actual time stamp in millis
 * @param isoDate as {@link String} e.g. PT2H1M10S
 * @return time to fire time stamp in millis
 */
public static long calculateTimeToFireForDuration(long now, String isoDate) {
    DateTime dateTime = new DateTime(now);
    Period period = periodformatter.parsePeriod(isoDate);
    dateTime = dateTime.plus(period);
    return dateTime.getMillis();
}