Package org.joda.time.format

Examples of org.joda.time.format.PeriodFormatter


      .toFormatter();

  public static String print(DateTime dateTime) {
    Duration duration = new Duration(dateTime, null);

    PeriodFormatter formatter = MONTHS;
    if (duration.isShorterThan(Duration.standardMinutes(1))) {
      formatter = SECONDS;
    }

    else if (duration.isShorterThan(Duration.standardHours(1))) {
      formatter = MINUTES;
    }

    else if (duration.isShorterThan(Duration.standardDays(1))) {
      formatter = HOURS;
    }

    else if (duration.isShorterThan(Duration.standardDays(7))) {
      formatter = DAYS;
    }

    else if (duration.isShorterThan(Duration.standardDays(30))) {
      formatter = WEEKS;
    }

    Period period = new Period(duration, new DateTime());
    return formatter.print(period);
  }
View Full Code Here


     * @return the millisecond duration
     * @throws ClassCastException if the object is invalid
     */
    public void setInto(ReadWritablePeriod period, Object object, Chronology chrono) {
        String str = (String) object;
        PeriodFormatter parser = ISOPeriodFormat.standard();
        period.clear();
        int pos = parser.parseInto(period, str, 0);
        if (pos < str.length()) {
            if (pos < 0) {
                // Parse again to get a better exception thrown.
                parser.withParseType(period.getPeriodType()).parseMutablePeriod(str);
            }
            throw new IllegalArgumentException("Invalid format: \"" + str + '"');
        }
    }
View Full Code Here

            throw new IllegalArgumentException("Format invalid: " + str);
        }

        DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
        dateTimeParser = dateTimeParser.withChronology(chrono);
        PeriodFormatter periodParser = ISOPeriodFormat.standard();
        long startInstant = 0, endInstant = 0;
        Period period = null;
        Chronology parsedChrono = null;
       
        // before slash
        char c = leftStr.charAt(0);
        if (c == 'P' || c == 'p') {
            period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
        } else {
            DateTime start = dateTimeParser.parseDateTime(leftStr);
            startInstant = start.getMillis();
            parsedChrono = start.getChronology();
        }
       
        // after slash
        c = rightStr.charAt(0);
        if (c == 'P' || c == 'p') {
            if (period != null) {
                throw new IllegalArgumentException("Interval composed of two durations: " + str);
            }
            period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
            chrono = (chrono != null ? chrono : parsedChrono);
            endInstant = chrono.add(period, startInstant, 1);
        } else {
            DateTime end = dateTimeParser.parseDateTime(rightStr);
            endInstant = end.getMillis();
View Full Code Here

        return runningDurationMillis == null ? "No duration recorded" : convert(runningDurationMillis.longValue());
    }

    private String convert(long runningDurationMillis) {
        Duration duration = new Duration(runningDurationMillis);
        PeriodFormatter formatter = PeriodFormat.getDefault();
        return formatter.print(duration.toPeriod());
    }
View Full Code Here

      ActionInvocation invocation = new ActionInvocation(action);


      DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm:ss");

      PeriodFormatter pFormatter= new PeriodFormatterBuilder()
      .printZeroAlways()
      .appendHours()
      .appendSeparator(":")
      .appendMinutes()
      .appendSeparator(":")
      .appendSeconds()
      .toFormatter();


      try {
        invocation.setInput("ID",Integer.toString(alarm.getID()));
        invocation.setInput("StartLocalTime",formatter.print(alarm.getStartTime()));
        invocation.setInput("Duration",pFormatter.print(alarm.getDuration()));
        invocation.setInput("Recurrence",alarm.getRecurrence());
        invocation.setInput("RoomUUID",alarm.getRoomUUID());
        invocation.setInput("ProgramURI",alarm.getProgramURI());
        invocation.setInput("ProgramMetaData",alarm.getProgramMetaData());
        invocation.setInput("PlayMode",alarm.getPlayMode());
View Full Code Here

      Service service = device.findService(new UDAServiceId("AVTransport"));
      Action action = service.getAction("SnoozeAlarm");
      ActionInvocation invocation = new ActionInvocation(action);

      Period snoozePeriod = Period.minutes(minutes);
      PeriodFormatter pFormatter= new PeriodFormatterBuilder()
      .printZeroAlways()
      .appendHours()
      .appendSeparator(":")
      .appendMinutes()
      .appendSeparator(":")
      .appendSeconds()
      .toFormatter();

      try {
        invocation.setInput("Duration",pFormatter.print(snoozePeriod));
      } catch (InvalidValueException ex) {
        logger.error("Action Invalid Value Exception {}",ex.getMessage());
      } catch (NumberFormatException ex) {
        logger.error("Action Invalid Value Format Exception {}",ex.getMessage())
      }
View Full Code Here

          DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm:ss");
          finalStartTime = formatter.parseDateTime(startTime);
          LocalTime localDateTime = finalStartTime.toLocalTime();
          finalStartTime = localDateTime.toDateTimeToday();
         
           PeriodFormatter pFormatter= new PeriodFormatterBuilder()
             .appendHours()
             .appendSeparator(":")
             .appendMinutes()
             .appendSeparator(":")
             .appendSeconds()
             .toFormatter();
         
          finalDuration = pFormatter.parsePeriod(duration);   
        } catch(Exception e) {
          logger.error("Error parsing DateTime");
        }
       
        alarms.add(new SonosAlarm(finalID,  finalStartTime,  finalDuration,  recurrence,
View Full Code Here

    if (myWaitAfterLastMessageString == null) {
      logger().debug("No JR_WAIT_AFTER_LAST_MESSAGE property found on message (message={})", getName());
      return null;
    }
   
    PeriodFormatter myPeriodFormatter = ISOPeriodFormat.standard();
    try {
      long myDuration = myPeriodFormatter.parsePeriod(myWaitAfterLastMessageString).toStandardDuration().getMillis();
      return myDuration;
    }
    catch (IllegalArgumentException e) {
      logger().error("Unable to parse JR_WAIT_AFTER_LAST_MESSAGE value for (message={}), value was: \"{}\"  (this is a problem you need to fix!!! e.g. for one day, use \"P1D\")", new Object[] { getName(), myWaitAfterLastMessageString });
      throw new IllegalArgumentException("Error parsing JR_WAIT_AFTER_LAST_MESSAGE value: " + myWaitAfterLastMessageString, e);
View Full Code Here

TOP

Related Classes of org.joda.time.format.PeriodFormatter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.