Example usage for org.joda.time Seconds secondsBetween

List of usage examples for org.joda.time Seconds secondsBetween

Introduction

In this page you can find the example usage for org.joda.time Seconds secondsBetween.

Prototype

public static Seconds secondsBetween(ReadablePartial start, ReadablePartial end) 

Source Link

Document

Creates a Seconds representing the number of whole seconds between the two specified partial datetimes.

Usage

From source file:act.job.JobManager.java

License:Apache License

public void on(DateTime instant, Runnable runnable) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("schedule runnable[%s] on %s", runnable, instant);
    }/* w  w  w  . j ava  2s.  co m*/
    DateTime now = DateTime.now();
    E.illegalArgumentIf(instant.isBefore(now));
    Seconds seconds = Seconds.secondsBetween(now, instant);
    executor().schedule(wrap(runnable), seconds.getSeconds(), TimeUnit.SECONDS);
}

From source file:act.job.JobManager.java

License:Apache License

public <T> Future<T> on(DateTime instant, Callable<T> callable) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("schedule callable[%s] on %s", callable, instant);
    }/*from w  w w  . jav a 2  s  .  c  o m*/
    DateTime now = DateTime.now();
    E.illegalArgumentIf(instant.isBefore(now));
    Seconds seconds = Seconds.secondsBetween(now, instant);
    return executor().schedule(callable, seconds.getSeconds(), TimeUnit.SECONDS);
}

From source file:ar.com.zir.utils.DateUtils.java

License:Open Source License

/**
 * Method that uses Joda Library to obtain the difference between two dates
 * in the specified date part/*from www. ja  v a2  s .  co m*/
 * 
 * @param datePart the datePart to use in the calculation
 * @param date1 first Date object
 * @param date2 second Date object
 * @return the result from date1 - date2 or -1 if specified datePart is not supported
 * @see java.util.Calendar
 */
public static int dateDiff(int datePart, Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    switch (datePart) {
    case DATE_PART_SECOND:
        return Seconds
                .secondsBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getSeconds();
    case DATE_PART_MINUTE:
        return Minutes
                .minutesBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getMinutes();
    case DATE_PART_HOUR:
        return Hours.hoursBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getHours();
    case DATE_PART_DAY:
        return Days.daysBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getDays();
    case DATE_PART_MONTH:
        return Months.monthsBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getMonths();
    case DATE_PART_YEAR:
        return Years.yearsBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getYears();
    default:
        return -1;
    }

}

From source file:cl.usach.managedbeans.EscritorioManagedBean.java

public int obtenerSegundosDiff(Date inicio, Date fin) {
    DateTime dtI = new DateTime(inicio);
    DateTime dtF = new DateTime(fin);
    int tiempo = Seconds.secondsBetween(dtI, dtF).getSeconds();
    return tiempo;
}

From source file:co.malm.mglam_reads.backend.util.DateDiffUtil.java

License:Apache License

/**
 * Calculates time differences using two dates, and stores it into a
 * {@linkplain java.util.Map} object.//  w w  w .ja v  a2s  .  c o m
 *
 * @param map calculations data
 * @param dt1 first date for diff
 * @param dt2 second date for diff
 * @see org.joda.time.DateTime
 * @see org.joda.time.Years
 * @see org.joda.time.Months
 * @see org.joda.time.Weeks
 * @see org.joda.time.Days
 * @see org.joda.time.Hours
 * @see org.joda.time.Minutes
 * @see org.joda.time.Seconds
 */
private void calculate(HashMap<String, Integer> map, DateTime dt1, DateTime dt2) {

    final int SECONDS_IN_MINUTE = 60;
    final int MINUTES_IN_HOUR = 60;
    final int HOURS_IN_DAY = 24;
    final int DAYS_IN_MONTH = 31;
    final int MONTHS_IN_YEAR = 12;
    final int WEEKS_IN_DAY = 7;

    int diffYears = Years.yearsBetween(dt1, dt2).getYears();
    if (diffYears > 0) {
        map.put("years", diffYears);
    }

    int diffMonths = Months.monthsBetween(dt1, dt2).getMonths();
    if (diffMonths >= 1 && diffMonths < MONTHS_IN_YEAR) {
        map.put("months", diffMonths);
    }

    int diffWeeks = Weeks.weeksBetween(dt1, dt2).getWeeks();
    if (diffWeeks >= 1 && diffWeeks < WEEKS_IN_DAY) {
        map.put("weeks", diffWeeks);
    }

    int diffDays = Days.daysBetween(dt1, dt2).getDays();
    if (diffDays >= 1 && diffDays < DAYS_IN_MONTH) {
        map.put("days", diffDays);
    }

    int diffHours = Hours.hoursBetween(dt1, dt2).getHours();
    if (diffHours >= 1 && diffHours < HOURS_IN_DAY) {
        map.put("hours", diffHours);
    }

    int diffMinutes = Minutes.minutesBetween(dt1, dt2).getMinutes();
    if (diffMinutes >= 1 && diffMinutes < MINUTES_IN_HOUR) {
        map.put("minutes", diffMinutes);
    }

    int diffSeconds = Seconds.secondsBetween(dt1, dt2).getSeconds();
    if (diffSeconds >= 1 && diffSeconds < SECONDS_IN_MINUTE) {
        map.put("seconds", diffSeconds);
    }
}

From source file:com.aliakseipilko.flightdutytracker.view.adapter.DayBarChartAdapter.java

License:Open Source License

private void processDutyHours() {
    List<BarEntry> barDutyEntries = new ArrayList<>();

    float xValueCount = 0f;
    int maxDaysInMonth = 0;

    long startDay = new DateTime(flights.minDate("startDutyTime")).getDayOfMonth();

    long currentYear = 0;
    long currentMonth = 0;
    long currentDay = 0;
    boolean isMonthWrapping = false;

    for (Flight flight : flights) {

        DateTime startDateTime = new DateTime(flight.getStartDutyTime());
        DateTime endDateTime = new DateTime(flight.getEndDutyTime());

        float decHoursDiff = (Seconds.secondsBetween(startDateTime, endDateTime).getSeconds() / 60f) / 60f;

        //Dont display stats for today as they will be malformed
        if (startDateTime.getDayOfYear() == DateTime.now().getDayOfYear()
                && startDateTime.getYear() == DateTime.now().getYear()) {
            continue;
        }//  w  ww.ja va  2 s . c  o m

        if (currentYear == 0 && currentMonth == 0 && currentDay == 0) {
            currentYear = startDateTime.getYear();
            currentMonth = startDateTime.getMonthOfYear();
            currentDay = startDateTime.getDayOfMonth();

            maxDaysInMonth = determineDaysInMonth((int) currentMonth, (int) currentYear);

            //Add new entry using day of month as x value
            BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
            barDutyEntries.add(newEntry);
            xValueCount++;
        } else {
            //Check if month is wrapping
            if (currentDay + 1 >= maxDaysInMonth) {
                isMonthWrapping = true;
            } else {
                //Check if days are adjacent
                //Add skipped days onto xValueCount to display blank spaces in graph
                if (currentDay + 1 != startDateTime.getDayOfMonth()) {
                    xValueCount += (startDateTime.getDayOfMonth() - currentDay) - 1;
                }
            }
            //Check if the date is the same as the previous flight
            // All flights are provided in an ordered list by the repo
            if (currentDay == startDateTime.getDayOfMonth() && currentMonth == startDateTime.getMonthOfYear()
                    && currentYear == startDateTime.getYear()) {
                //Get last entry in list
                BarEntry lastEntry = barDutyEntries.get(barDutyEntries.size() - 1);
                //Add the additional hours in that day on, X value (the date) does not change
                lastEntry = new BarEntry(lastEntry.getX(), lastEntry.getY() + decHoursDiff);
                //Remove the last entry and add the modified entry instead of it
                barDutyEntries.remove(barDutyEntries.size() - 1);
                barDutyEntries.add(lastEntry);
            } else {
                //Check if days of month wrap around
                if (startDateTime.getMonthOfYear() != currentMonth) {
                    isMonthWrapping = true;
                }

                //New day
                //Update these for the next iteration
                currentYear = startDateTime.getYear();
                currentMonth = startDateTime.getMonthOfYear();
                currentDay = startDateTime.getDayOfMonth();

                //Add new entry using day of month as x value
                BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
                barDutyEntries.add(newEntry);
                xValueCount++;
            }
        }
    }

    IAxisValueFormatter xAxisValueFormatter = new DayAxisValueFormatter((int) (startDay - 1), maxDaysInMonth);
    IAxisValueFormatter yAxisValueFormatter = new DefaultAxisValueFormatter(2);
    BarDataSet dataSet = new BarDataSet(barDutyEntries, "Duty Hours");
    dataSet.setValueTextSize(16f);
    BarData barDutyData = new BarData(dataSet);
    barDutyData.setBarWidth(0.9f);
    barDutyData.setHighlightEnabled(false);

    view.setupDutyBarChart(barDutyData, xAxisValueFormatter, yAxisValueFormatter);
}

From source file:com.aliakseipilko.flightdutytracker.view.adapter.DayBarChartAdapter.java

License:Open Source License

private void processFlightHours() {
    List<BarEntry> barFlightEntries = new ArrayList<>();

    float xValueCount = 0f;
    int maxDaysInMonth = 0;

    long startDay = new DateTime(flights.minDate("startFlightTime")).getDayOfMonth();

    long currentYear = 0;
    long currentMonth = 0;
    long currentDay = 0;
    boolean isMonthWrapping = false;

    for (Flight flight : flights) {

        DateTime startDateTime = new DateTime(flight.getStartFlightTime());
        DateTime endDateTime = new DateTime(flight.getEndFlightTime());

        float decHoursDiff = (Seconds.secondsBetween(startDateTime, endDateTime).getSeconds() / 60f) / 60f;

        //Dont display stats for today as they will be malformed
        if (startDateTime.getDayOfYear() == DateTime.now().getDayOfYear()
                && startDateTime.getYear() == DateTime.now().getYear()) {
            continue;
        }//from www.java2s. c  o m

        if (currentYear == 0 && currentMonth == 0 && currentDay == 0) {
            currentYear = startDateTime.getYear();
            currentMonth = startDateTime.getMonthOfYear();
            currentDay = startDateTime.getDayOfMonth();

            maxDaysInMonth = determineDaysInMonth((int) currentMonth, (int) currentYear);

            //Add new entry using day of month as x value
            BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
            barFlightEntries.add(newEntry);
            xValueCount++;
        } else {
            //Check if month is wrapping
            if (currentDay + 1 >= maxDaysInMonth) {
                isMonthWrapping = true;
            } else {
                //Check if days are adjacent
                //Add skipped days onto xValueCount to display blank spaces in graph
                if (currentDay + 1 != startDateTime.getDayOfMonth()) {
                    xValueCount = xValueCount + (startDateTime.getDayOfMonth() - currentDay) - 1;
                }
            }
            //Check if the date is the same as the previous flight
            // All flights are provided in an ordered list by the repo
            if (currentDay == startDateTime.getDayOfMonth() && currentMonth == startDateTime.getMonthOfYear()
                    && currentYear == startDateTime.getYear()) {
                //Get last entry in list
                BarEntry lastEntry = barFlightEntries.get(barFlightEntries.size() - 1);
                //Add the additional hours in that day on, X value (the date) does not change
                lastEntry = new BarEntry(lastEntry.getX(), lastEntry.getY() + decHoursDiff);
                //Remove the last entry and add the modified entry instead of it
                barFlightEntries.remove(barFlightEntries.size() - 1);
                barFlightEntries.add(lastEntry);
            } else {
                //Check if days of month wrap around
                if (startDateTime.getMonthOfYear() != currentMonth) {
                    isMonthWrapping = true;
                }

                //New day
                //Update these for the next iteration
                currentYear = startDateTime.getYear();
                currentMonth = startDateTime.getMonthOfYear();
                currentDay = startDateTime.getDayOfMonth();

                //Add new entry using day of month as x value
                BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
                barFlightEntries.add(newEntry);
                xValueCount++;
            }
        }
    }

    IAxisValueFormatter xAxisValueFormatter = new DayAxisValueFormatter((int) (startDay - 1), maxDaysInMonth);
    IAxisValueFormatter yAxisValueFormatter = new DefaultAxisValueFormatter(2);
    BarDataSet dataSet = new BarDataSet(barFlightEntries, "Flight Hours");
    dataSet.setValueTextSize(16f);
    BarData barFlightData = new BarData(dataSet);
    barFlightData.setBarWidth(0.9f);
    barFlightData.setHighlightEnabled(false);

    view.setupFlightBarChart(barFlightData, xAxisValueFormatter, yAxisValueFormatter);
}

From source file:com.aliakseipilko.flightdutytracker.view.adapter.MonthBarChartAdapter.java

License:Open Source License

private void processDutyHours() {
    List<BarEntry> barDutyEntries = new ArrayList<>();

    float xValueCount = 0f;

    long startMonth = new DateTime(flights.minDate("startDutyTime")).getMonthOfYear();

    long currentYear = 0;
    long currentMonth = 0;

    for (Flight flight : flights) {
        DateTime startDateTime = new DateTime(flight.getStartDutyTime());
        DateTime endDateTime = new DateTime(flight.getEndDutyTime());

        float decHoursDiff = (Seconds.secondsBetween(startDateTime, endDateTime).getSeconds() / 60f) / 60f;

        if (currentYear == 0 && currentMonth == 0) {
            currentYear = startDateTime.getYear();
            currentMonth = startDateTime.getMonthOfYear();

            //Add new entry using day of month as x value
            BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
            barDutyEntries.add(newEntry);
            xValueCount++;/* w  ww.j  a v  a 2  s  .  c  o m*/
        } else {
            if (currentMonth + 1 < 13) {
                //Add on any skipped months to xValueCount
                if (currentMonth + 1 != startDateTime.getMonthOfYear()) {
                    xValueCount += (startDateTime.getMonthOfYear() - currentMonth) - 1;
                }
            }
            //Check if the month is the same as the previous flight
            // All flights are provided in an ordered list by the repo
            if (currentMonth == startDateTime.getMonthOfYear() && currentYear == startDateTime.getYear()) {
                //Get last entry in list
                BarEntry lastEntry = barDutyEntries.get(barDutyEntries.size() - 1);
                //Add the additional hours in that day on, X value (the date) does not change
                lastEntry = new BarEntry(lastEntry.getX(), lastEntry.getY() + decHoursDiff);
                //Remove the last entry and add the modified entry instead of it
                barDutyEntries.remove(barDutyEntries.size() - 1);
                barDutyEntries.add(lastEntry);
            } else {
                //Check if days of month wrap around
                if (startDateTime.getYear() != currentYear) {

                }

                //New month
                //Update these for the next iteration
                currentYear = startDateTime.getYear();
                currentMonth = startDateTime.getMonthOfYear();

                //Add new entry using day of month as x value
                BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
                barDutyEntries.add(newEntry);
                xValueCount++;
            }
        }
    }

    IAxisValueFormatter xAxisValueFormatter = new MonthAxisValueFormatter(((int) startMonth - 1));
    IAxisValueFormatter yAxisValueFormatter = new DefaultAxisValueFormatter(2);
    BarDataSet dataSet = new BarDataSet(barDutyEntries, "Duty Hours");
    dataSet.setValueTextSize(16f);
    BarData barDutyData = new BarData(dataSet);
    barDutyData.setBarWidth(0.9f);
    barDutyData.setHighlightEnabled(false);

    view.setupDutyBarChart(barDutyData, xAxisValueFormatter, yAxisValueFormatter);
}

From source file:com.aliakseipilko.flightdutytracker.view.adapter.MonthBarChartAdapter.java

License:Open Source License

private void processFlightHours() {
    List<BarEntry> barFlightEntries = new ArrayList<>();

    float xValueCount = 0f;

    long startMonth = new DateTime(flights.minDate("startFlightTime")).getMonthOfYear();

    long currentYear = 0;
    long currentMonth = 0;

    for (Flight flight : flights) {
        DateTime startDateTime = new DateTime(flight.getStartFlightTime());
        DateTime endDateTime = new DateTime(flight.getEndFlightTime());

        float decHoursDiff = (Seconds.secondsBetween(startDateTime, endDateTime).getSeconds() / 60f) / 60f;

        if (currentYear == 0 && currentMonth == 0) {
            currentYear = startDateTime.getYear();
            currentMonth = startDateTime.getMonthOfYear();

            //Add new entry using day of month as x value
            BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
            barFlightEntries.add(newEntry);
            xValueCount++;/* w ww  .  j  a  v a  2  s  .c om*/
        } else {
            //Check if year wraps around
            if (currentMonth + 1 < 13) {
                //Add on any skipped months to xValueCount
                if (currentMonth + 1 != startDateTime.getMonthOfYear()) {
                    xValueCount += (startDateTime.getMonthOfYear() - currentMonth) - 1;
                }
            }
            //Check if the month is the same as the previous flight
            // All flights are provided in an ordered list by the repo
            if (currentMonth == startDateTime.getMonthOfYear() && currentYear == startDateTime.getYear()) {
                //Get last entry in list
                BarEntry lastEntry = barFlightEntries.get(barFlightEntries.size() - 1);
                //Add the additional hours in that day on, X value (the date) does not change
                lastEntry = new BarEntry(lastEntry.getX(), lastEntry.getY() + decHoursDiff);
                //Remove the last entry and add the modified entry instead of it
                barFlightEntries.remove(barFlightEntries.size() - 1);
                barFlightEntries.add(lastEntry);
            } else {
                //Check if days of month wrap around
                if (startDateTime.getYear() != currentYear) {

                }

                //New month
                //Update these for the next iteration
                currentYear = startDateTime.getYear();
                currentMonth = startDateTime.getMonthOfYear();

                //Add new entry using day of month as x value
                BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
                barFlightEntries.add(newEntry);
                xValueCount++;
            }
        }
    }

    IAxisValueFormatter xAxisValueFormatter = new MonthAxisValueFormatter(((int) startMonth - 1));
    IAxisValueFormatter yAxisValueFormatter = new DefaultAxisValueFormatter(2);
    BarDataSet dataSet = new BarDataSet(barFlightEntries, "Flight Hours");
    dataSet.setValueTextSize(16f);
    BarData barDutyData = new BarData(dataSet);
    barDutyData.setBarWidth(0.9f);
    barDutyData.setHighlightEnabled(false);

    view.setupFlightBarChart(barDutyData, xAxisValueFormatter, yAxisValueFormatter);
}

From source file:com.axelor.controller.ConnectionToPrestashop.java

License:Open Source License

@Transactional
@SuppressWarnings("finally")
public String syncCustomer() {
    String message = "";
    try {/*from   ww  w  .  ja v  a  2  s.  c  o m*/
        List<Integer> prestashopIdList = new ArrayList<Integer>();
        List<Integer> erpIdList = new ArrayList<Integer>();
        List<Partner> erpList = Partner.all().fetch();

        for (Partner prestahopCustomer : erpList) {
            erpIdList.add(prestahopCustomer.getPrestashopid());
        }
        System.out.println("API KEY :: " + apiKey);
        URL url = new URL("http://localhost/client-lib/crud/action.php?resource=customers&action=getallid&Akey="
                + apiKey);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        InputStream inputStream = connection.getInputStream();
        Scanner scan = new Scanner(inputStream);
        while (scan.hasNext()) {
            String data = scan.nextLine();
            System.out.println(data);
            prestashopIdList.add(Integer.parseInt(data));
        }
        System.out.println("From Prestashop :: " + prestashopIdList.size());
        System.out.println("From ERP :: " + erpIdList.size());
        scan.close();

        // Check new entries in the prestshop
        Iterator<Integer> prestaListIterator = prestashopIdList.iterator();
        while (prestaListIterator.hasNext()) {
            Integer tempId = prestaListIterator.next();
            System.out.println("Current prestaid for operation ::" + tempId);
            if (erpIdList.contains(tempId)) {
                Customer tempCustomer = getCustomer(tempId);
                String dateUpdate = tempCustomer.getDate_upd();
                LocalDateTime dt1 = LocalDateTime.parse(dateUpdate,
                        DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
                Partner pCust = Partner.all().filter("prestashopId=?", tempId).fetchOne();
                LocalDateTime dt2 = pCust.getUpdatedOn();
                if (dt2 != null) {
                    int diff = Seconds.secondsBetween(dt2, dt1).getSeconds();
                    if (diff > 1)
                        updateCustomer(tempCustomer, tempId);
                } else {
                    updateCustomer(tempCustomer, tempId);
                }
                erpIdList.remove(tempId);
            } else {
                System.out.println("Current prestaid for insertion operation ::" + tempId);
                // insert new data in ERP Database
                insertCustomer(tempId);
                erpIdList.remove(tempId);
            }
        }
        if (erpIdList.isEmpty()) {
            System.out.println("Synchronization is completed.");
            message = "done";
        } else {
            // delete from ERP
            Iterator<Integer> erpListIterator = erpIdList.iterator();
            while (erpListIterator.hasNext()) {
                Integer tempId = erpListIterator.next();
                if (tempId != 0) {
                    System.out.println("Currently in  Erp ::" + tempId);
                    Partner customerDelete = Partner.all().filter("prestashopid=?", tempId).fetchOne();
                    String firstName = customerDelete.getFirstName();
                    customerDelete.setArchived(Boolean.TRUE);
                    System.out.println("customer deleted ::" + firstName);
                }
            }
            while (prestaListIterator.hasNext()) {
                Integer tempId = prestaListIterator.next();
                System.out.println("Currently in prestashop ::" + tempId);
            }
            System.out.println("Synchronization is completed.");
            message = "done";
        }
    } catch (Exception e) {
        message = "Wrong Authentication Key or Key has been disabled.";
    } finally {
        return message;
    }
}