Example usage for org.joda.time MutablePeriod MutablePeriod

List of usage examples for org.joda.time MutablePeriod MutablePeriod

Introduction

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

Prototype

public MutablePeriod(Object period) 

Source Link

Document

Creates a period by converting or copying from another object.

Usage

From source file:com.helger.datetime.config.PDTTypeConverterRegistrar.java

License:Apache License

public void registerTypeConverter(@Nonnull final ITypeConverterRegistry aRegistry) {
    // Register Joda native converters
    _registerJodaConverter();//w  w w  .j a v  a2  s. c o m

    final Class<?>[] aSourceClasses = new Class<?>[] { String.class, Calendar.class, GregorianCalendar.class,
            Date.class, AtomicInteger.class, AtomicLong.class, BigDecimal.class, BigInteger.class, Byte.class,
            Double.class, Float.class, Integer.class, Long.class, Short.class };

    // DateTime
    aRegistry.registerTypeConverter(aSourceClasses, DateTime.class, new ITypeConverter() {
        @Nonnull
        public DateTime convert(@Nonnull final Object aSource) {
            return new DateTime(aSource, PDTConfig.getDefaultChronology());
        }
    });
    aRegistry.registerTypeConverter(LocalDate.class, DateTime.class, new ITypeConverter() {
        @Nonnull
        public DateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createDateTime((LocalDate) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalTime.class, DateTime.class, new ITypeConverter() {
        @Nonnull
        public DateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createDateTime((LocalTime) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalDateTime.class, DateTime.class, new ITypeConverter() {
        @Nonnull
        public DateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createDateTime((LocalDateTime) aSource);
        }
    });

    // LocalDateTime
    aRegistry.registerTypeConverter(aSourceClasses, LocalDateTime.class, new ITypeConverter() {
        @Nonnull
        public LocalDateTime convert(@Nonnull final Object aSource) {
            return new LocalDateTime(aSource, PDTConfig.getDefaultChronology());
        }
    });
    aRegistry.registerTypeConverter(DateTime.class, LocalDateTime.class, new ITypeConverter() {
        @Nonnull
        public LocalDateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDateTime((DateTime) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalDate.class, LocalDateTime.class, new ITypeConverter() {
        @Nonnull
        public LocalDateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDateTime((LocalDate) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalTime.class, LocalDateTime.class, new ITypeConverter() {
        @Nonnull
        public LocalDateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDateTime((LocalTime) aSource);
        }
    });

    // LocalDate
    aRegistry.registerTypeConverter(aSourceClasses, LocalDate.class, new ITypeConverter() {
        @Nonnull
        public LocalDate convert(@Nonnull final Object aSource) {
            return new LocalDate(aSource, PDTConfig.getDefaultChronology());
        }
    });
    aRegistry.registerTypeConverter(DateTime.class, LocalDate.class, new ITypeConverter() {
        @Nonnull
        public LocalDate convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDate((DateTime) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalDateTime.class, LocalDate.class, new ITypeConverter() {
        @Nonnull
        public LocalDate convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDate((LocalDateTime) aSource);
        }
    });

    // LocalTime
    aRegistry.registerTypeConverter(aSourceClasses, LocalTime.class, new ITypeConverter() {
        @Nonnull
        public LocalTime convert(@Nonnull final Object aSource) {
            return new LocalTime(aSource, PDTConfig.getDefaultChronology());
        }
    });
    aRegistry.registerTypeConverter(DateTime.class, LocalTime.class, new ITypeConverter() {
        @Nonnull
        public LocalTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalTime((DateTime) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalDateTime.class, LocalTime.class, new ITypeConverter() {
        @Nonnull
        public LocalTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalTime((LocalDateTime) aSource);
        }
    });

    // Duration
    aRegistry.registerTypeConverter(new Class<?>[] { String.class, AtomicInteger.class, AtomicLong.class,
            BigDecimal.class, BigInteger.class, Byte.class, Double.class, Float.class, Integer.class,
            Long.class, Short.class }, Duration.class, new ITypeConverter() {
                @Nonnull
                public Duration convert(@Nonnull final Object aSource) {
                    return new Duration(aSource);
                }
            });

    // Period
    aRegistry.registerTypeConverter(new Class<?>[] { String.class, AtomicInteger.class, AtomicLong.class,
            BigDecimal.class, BigInteger.class, Byte.class, Double.class, Float.class, Integer.class,
            Long.class, Short.class }, Period.class, new ITypeConverter() {
                @Nonnull
                public Period convert(@Nonnull final Object aSource) {
                    return new Period(aSource);
                }
            });

    // MutablePeriod
    aRegistry.registerTypeConverter(new Class<?>[] { String.class, AtomicInteger.class, AtomicLong.class,
            BigDecimal.class, BigInteger.class, Byte.class, Double.class, Float.class, Integer.class,
            Long.class, Short.class }, MutablePeriod.class, new ITypeConverter() {
                @Nonnull
                public MutablePeriod convert(@Nonnull final Object aSource) {
                    return new MutablePeriod(aSource);
                }
            });
}

From source file:io.warp10.script.functions.ISODURATION.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
    Object top = stack.pop();/*  w w  w  .j  a va  2 s .  c o  m*/

    if (!(top instanceof Long)) {
        throw new WarpScriptException(
                getName() + " expects a number of time units (LONG) on top of the stack.");
    }

    long duration = ((Number) top).longValue();

    StringBuffer buf = new StringBuffer();
    ReadablePeriod period = new MutablePeriod(duration / Constants.TIME_UNITS_PER_MS);
    ISOPeriodFormat.standard().getPrinter().printTo(buf, period, Locale.US);

    stack.push(buf.toString());

    return stack;
}

From source file:org.oscarehr.common.dao.PopulationReportDao.java

License:Open Source License

public int[] getUsages(int numYears) {

    int[] shelterUsages = new int[3];

    Map<Integer, Set<Stay>> clientIdToStayMap = new HashMap<Integer, Set<Stay>>();

    Calendar instant = Calendar.getInstance();
    Date end = instant.getTime();
    Date start = DateTimeFormatUtils.getPast(instant, numYears);

    for (Object o : getHibernateTemplate().find(HQL_GET_USAGES, start)) {
        Object[] tuple = (Object[]) o;

        Integer clientId = (Integer) tuple[0];
        Date admission = (Date) tuple[1];
        Date discharge = (Date) tuple[2];

        if (!clientIdToStayMap.containsKey(clientId)) {
            clientIdToStayMap.put(clientId, new HashSet<Stay>());
        }/*  w ww.j  av  a  2 s. c o  m*/

        try {
            Stay stay = new Stay(admission, discharge, start, end);
            clientIdToStayMap.get(clientId).add(stay);
        } catch (IllegalArgumentException e) {
            logger.error("client id: " + clientId);
        }
    }

    for (Entry<Integer, Set<Stay>> entry : clientIdToStayMap.entrySet()) {
        MutablePeriod period = new MutablePeriod(PeriodType.days());

        for (Stay stay : entry.getValue()) {
            period.add(stay.getInterval());
        }

        int days = Days.standardDaysIn(period).getDays();

        if (days <= 10) {
            shelterUsages[LOW] += 1;
        } else if (11 <= days && days <= 179) {
            shelterUsages[MEDIUM] += 1;
        } else if (180 <= days) {
            shelterUsages[HIGH] += 1;
        }
    }

    return shelterUsages;
}