Package org.joda.time

Examples of org.joda.time.Period


     if (utc == null) return "";
     utc = ReplaceVariable.replaceAll(utc, " ", "T");
     DateTime now = new DateTime();
     DateTimeFormatter f = ISODateTimeFormat.dateTimeParser();
     DateTime other = f.parseDateTime(utc);
     Period period = new Period(other, now); // Period(ReadableInstant startInstant, ReadableInstant endInstant)
     return period.toString();
   }
View Full Code Here


  public String format(Object value) {
    if (value == null) {
      return null;
    }
    try {
      return formatter.print(new Period(0, ((Number) value).longValue()));
    } catch (Throwable t) {
      return null;
    }
  }
View Full Code Here

        // TODO take these from the model


        Interval interval = new Interval(dtFrom, dtTo);

        Period p = interval.toPeriod(PeriodType.days());
        // try to determine length if we had to show milliseconds

        int n = p.getDays();
        int unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("wednesday  ", null)).getWidth();
        if (n < (width / unitSize)) {
            //System.out.println("jour");
            for (int i = 0; i < n; i++) {
                g2d.drawString(dtFrom.plusDays(i).dayOfWeek().getAsText(LOCALE),
                        leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);
                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours());
            }
            return;
        }


        unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("wed ", null)).getWidth();
        if (n < (width / unitSize)) {
            //System.out.println("jou");
            for (int i = 0; i < n; i++) {
                g2d.drawString(dtFrom.plusDays(i).dayOfWeek().getAsShortText(LOCALE),
                        leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);

                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours());
            }
            return;
        }


        p = interval.toPeriod(PeriodType.days());
        n = p.getDays();
        unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("30", null)).getWidth();
        if (n < (width / unitSize)) {
            //System.out.println("j");
            for (int i = 0; i < n; i++) {
                g2d.drawString("" + (dtFrom.getDayOfMonth() + i),
                        leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);

                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours());
            }
            return;
        }

        p = interval.toPeriod(PeriodType.months());
        n = p.getMonths();
        unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("September  ", null)).getWidth();
        if (n < (width / unitSize)) {
            //System.out.println("mois");
            for (int i = 0; i < n; i++) {
                g2d.drawString(dtFrom.plusMonths(i).monthOfYear().getAsText(LOCALE),
                        leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);

                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays());
            }
            return;
        }


        unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("dec ", null)).getWidth();
        if (n < (width / unitSize)) {
            //System.out.println("mo");
            for (int i = 0; i < n; i++) {
                g2d.drawString(dtFrom.plusMonths(i).monthOfYear().getAsShortText(LOCALE),
                        leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);
                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays());
            }
            return;
        }

        unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("29 ", null)).getWidth();
        if (n < (width / unitSize)) {
            //System.out.println("m");
            for (int i = 0; i < n; i++) {
                g2d.drawString("" + (dtFrom.getMonthOfYear() + i), leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);
                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays());
            }
            return;
        }

        p = interval.toPeriod(PeriodType.years());
        n = p.getYears();
        unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("1980 ", null)).getWidth();
        if (n < (width / unitSize)) {
            //System.out.println("year");
            for (int i = 0; i < n; i++) {
                g2d.drawString("" + (dtFrom.getYear() + i), leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);
                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Months.monthsBetween(dtFrom.plusYears(i), dtFrom.plusYears(i + 1)).getMonths());
            }
            return;
        }

        int group = 10;
        n = p.getYears() / group;
        if (n < (width / unitSize)) {
            //System.out.println("10 years");
            for (int i = 0; i < n; i++) {
                g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);
                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Months.monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths());
            }
            return;
        }
        group = 20;
        n = p.getYears() / group;
        if (n < (width / unitSize)) {
            //System.out.println("20 years");
            for (int i = 0; i < n; i++) {
                g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);
                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Months.monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths());
            }
            return;
        }
        group = 50;
        n = p.getYears() / group;
        if (n < (width / unitSize)) {
            //System.out.println("50 years");
            for (int i = 0; i < n; i++) {
                g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n),
                        textTopPosition);
                g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin);
                paintSmallGraduations(g2d,
                        leftMargin + i * (width / n),
                        leftMargin + (i + 1) * (width / n),
                        Months.monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths());
            }
            return;
        }
        group = 100;
        n = p.getYears() / group;
        if (n / 100 < (width / unitSize)) {
            //System.out.println("100 years");
            for (int i = 0; i < n; i++) {
                g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n),
                        textTopPosition);
View Full Code Here

    }

    //-----------------------------------------------------------------------
    public void testParseStandard1() {
        PeriodFormatter parser = PeriodFormat.getDefault();
        Period p = parser.parsePeriod("6 years, 3 months and 2 days");
        assertEquals(new Period(6, 3, 0, 2, 0, 0, 0, 0), p);
    }
View Full Code Here

            .appendSuffix(":")
            .minimumPrintedDigits(2)
            .appendMinutes()
            .toFormatter();

        Period p;

        p = new Period(47, 55, 0, 0);
        assertEquals("47:55", formatter.print(p));
        assertEquals(p, formatter.parsePeriod("47:55"));
        assertEquals(p, formatter.parsePeriod("047:055"));

        p = new Period(7, 5, 0, 0);
        assertEquals("7:05", formatter.print(p));
        assertEquals(p, formatter.parsePeriod("7:05"));
        assertEquals(p, formatter.parsePeriod("7:5"));
        assertEquals(p, formatter.parsePeriod("07:05"));

        p = new Period(0, 5, 0, 0);
        assertEquals("0:05", formatter.print(p));
        assertEquals(p, formatter.parsePeriod("0:05"));
        assertEquals(p, formatter.parsePeriod("0:5"));
        assertEquals(p, formatter.parsePeriod("00:005"));
        assertEquals(p, formatter.parsePeriod("0:005"));

        p = new Period(0, 0, 0, 0);
        assertEquals("0:00", formatter.print(p));
        assertEquals(p, formatter.parsePeriod("0:00"));
        assertEquals(p, formatter.parsePeriod("0:0"));
        assertEquals(p, formatter.parsePeriod("00:00"));
    }
View Full Code Here

    }

    //-----------------------------------------------------------------------
    public void testParseStandard1() {
        PeriodFormatter parser = ISOPeriodFormat.standard();
        Period p = parser.parsePeriod("P1Y2M3W4DT5H6M7.008S");
        assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), p);
    }
View Full Code Here

    }

    //-----------------------------------------------------------------------
    public void testParseStandard2() {
        PeriodFormatter parser = ISOPeriodFormat.standard();
        Period p = parser.parsePeriod("P0Y0M0W0DT5H6M7.008S");
        assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 8), p);
    }
View Full Code Here

    }

    //-----------------------------------------------------------------------
    public void testParseStandard3() {
        PeriodFormatter parser = ISOPeriodFormat.standard();
        Period p = parser.parsePeriod("P0DT5H6M7.008S");
        assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 8), p);
    }
View Full Code Here

    }

    //-----------------------------------------------------------------------
    public void testParseStandard4() {
        PeriodFormatter parser = ISOPeriodFormat.standard();
        Period p = parser.parsePeriod("P2Y3DT5H6M7.008S");
        assertEquals(new Period(2, 0, 0, 3, 5, 6, 7, 8), p);
    }
View Full Code Here

    }

    //-----------------------------------------------------------------------
    public void testParseStandard5() {
        PeriodFormatter parser = ISOPeriodFormat.standard();
        Period p = parser.parsePeriod("P2YT5H6M7.008S");
        assertEquals(new Period(2, 0, 0, 0, 5, 6, 7, 8), p);
    }
View Full Code Here

TOP

Related Classes of org.joda.time.Period

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.