Example usage for org.joda.time DateTime getChronology

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

Introduction

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

Prototype

public Chronology getChronology() 

Source Link

Document

Gets the chronology of the datetime.

Usage

From source file:org.graylog2.indexer.rotation.TimeBasedRotationStrategy.java

License:Open Source License

/**
 * Determines the starting point ("anchor") for a period.
 *
 * To produce repeatable rotation points in time, the period is "snapped" to a "grid" of time.
 * For example, an hourly index rotation would be anchored to the last full hour, instead of happening at whatever minute
 * the first rotation was started.// w w  w  . j  av  a  2s.  c om
 *
 * This "snapping" is done accordingly with the other parts of a period.
 *
 * For highly irregular periods (those that do not have a small zero component)
 *
 * @param period the rotation period
 * @return the anchor DateTime to calculate rotation periods from
 */
protected static DateTime determineRotationPeriodAnchor(Period period) {
    final Period normalized = period.normalizedStandard();
    int years = normalized.getYears();
    int months = normalized.getMonths();
    int weeks = normalized.getWeeks();
    int days = normalized.getDays();
    int hours = normalized.getHours();
    int minutes = normalized.getMinutes();
    int seconds = normalized.getSeconds();

    if (years == 0 && months == 0 && weeks == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
        throw new IllegalArgumentException("Invalid rotation period specified");
    }

    // find the largest non-zero stride in the period. that's our anchor type. statement order matters here!
    DateTimeFieldType largestStrideType = null;
    if (seconds > 0)
        largestStrideType = secondOfMinute();
    if (minutes > 0)
        largestStrideType = minuteOfHour();
    if (hours > 0)
        largestStrideType = hourOfDay();
    if (days > 0)
        largestStrideType = dayOfMonth();
    if (weeks > 0)
        largestStrideType = weekOfWeekyear();
    if (months > 0)
        largestStrideType = monthOfYear();
    if (years > 0)
        largestStrideType = year();
    if (largestStrideType == null) {
        throw new IllegalArgumentException("Could not determine rotation stride length.");
    }

    final DateTime now = Tools.iso8601();

    final DateTimeField field = largestStrideType.getField(now.getChronology());
    // use normalized here to make sure we actually have the largestStride type available! see https://github.com/Graylog2/graylog2-server/issues/836
    int periodValue = normalized.get(largestStrideType.getDurationType());
    final long fieldValue = field.roundFloor(now.getMillis());

    final int fieldValueInUnit = field.get(fieldValue);
    if (periodValue == 0) {
        // https://github.com/Graylog2/graylog2-server/issues/836
        log.warn(
                "Determining stride length failed because of a 0 period. Defaulting back to 1 period to avoid crashing, but this is a bug!");
        periodValue = 1;
    }
    final long difference = (fieldValueInUnit % periodValue);
    final long newValue = field.add(fieldValue, -1 * difference);
    return new DateTime(newValue, DateTimeZone.UTC);
}

From source file:org.joda.example.time.Examples.java

License:Apache License

private void runDateTime() {
    System.out.println("DateTime");
    System.out.println("=======");
    System.out.println(/* w w w.  j ava 2 s  .  com*/
            "DateTime stores a the date and time using millisecs from 1970-01-01T00:00:00Z internally");
    System.out.println("DateTime is immutable and thread-safe");
    System.out.println("                      in = new DateTime()");
    DateTime in = new DateTime();
    System.out.println("Millisecond time:     in.getMillis():           " + in.getMillis());
    System.out.println("ISO string version:   in.toString():            " + in.toString());
    System.out.println("ISO chronology:       in.getChronology():       " + in.getChronology());
    System.out.println("Your time zone:       in.getDateTimeZone():     " + in.getZone());
    System.out.println("Change millis:        in.withMillis(0):         " + in.withMillis(0L));
    System.out.println("");
    System.out.println("Get year:             in.getYear():             " + in.getYear());
    System.out.println("Get monthOfYear:      in.getMonthOfYear():      " + in.getMonthOfYear());
    System.out.println("Get dayOfMonth:       in.getDayOfMonth():       " + in.getDayOfMonth());
    System.out.println("...");
    System.out.println("Property access:      in.dayOfWeek().get():                   " + in.dayOfWeek().get());
    System.out.println(
            "Day of week as text:  in.dayOfWeek().getAsText():             " + in.dayOfWeek().getAsText());
    System.out.println(
            "Day as short text:    in.dayOfWeek().getAsShortText():        " + in.dayOfWeek().getAsShortText());
    System.out.println("Day in french:        in.dayOfWeek().getAsText(Locale.FRENCH):"
            + in.dayOfWeek().getAsText(Locale.FRENCH));
    System.out.println("Max allowed value:    in.dayOfWeek().getMaximumValue():       "
            + in.dayOfWeek().getMaximumValue());
    System.out.println("Min allowed value:    in.dayOfWeek().getMinimumValue():       "
            + in.dayOfWeek().getMinimumValue());
    System.out.println(
            "Copy & set to Jan:    in.monthOfYear().setCopy(1):            " + in.monthOfYear().setCopy(1));
    System.out.println(
            "Copy & add 14 months: in.monthOfYear().addCopy(14):           " + in.monthOfYear().addToCopy(14));
    System.out.println("Add 14 mnths in field:in.monthOfYear().addWrapFieldCopy(14):  "
            + in.monthOfYear().addWrapFieldToCopy(14));
    System.out.println("...");
    System.out.println("Convert to Instant:   in.toInstant():           " + in.toInstant());
    System.out.println("Convert to DateTime:  in.toDateTime():          " + in.toDateTime());
    System.out.println("Convert to MutableDT: in.toMutableDateTime():   " + in.toMutableDateTime());
    System.out.println("Convert to Date:      in.toDate():              " + in.toDate());
    System.out.println("Convert to Calendar:  in.toCalendar(Locale.UK): "
            + in.toCalendar(Locale.UK).toString().substring(0, 46));
    System.out.println("Convert to GregCal:   in.toGregorianCalendar(): "
            + in.toGregorianCalendar().toString().substring(0, 46));
    System.out.println("");
    System.out.println("                      in2 = new DateTime(in.getMillis() + 10)");
    DateTime in2 = new DateTime(in.getMillis() + 10);
    System.out.println("Equals ms and chrono: in.equals(in2):           " + in.equals(in2));
    System.out.println("Compare millisecond:  in.compareTo(in2):        " + in.compareTo(in2));
    System.out.println("Compare millisecond:  in.isEqual(in2):          " + in.isEqual(in2));
    System.out.println("Compare millisecond:  in.isAfter(in2):          " + in.isAfter(in2));
    System.out.println("Compare millisecond:  in.isBefore(in2):         " + in.isBefore(in2));
}

From source file:org.jruby.ext.date.RubyDateTime.java

License:LGPL

static DateTime withTimeAt0InZone(DateTime dt, DateTimeZone zone) {
    long millis = dt.getZone().getMillisKeepLocal(zone, dt.getMillis());
    final Chronology chronology = dt.getChronology().withZone(zone);
    millis = chronology.millisOfDay().set(millis, 0);
    return new DateTime(millis, chronology);
}

From source file:org.jruby.truffle.core.time.RubyDateFormatter.java

License:LGPL

/**
 * Ruby always follows Astronomical year numbering,
 * that is BC x is -x+1 and there is a year 0 (BC 1)
 * but Joda-time returns -x for year x BC in Julian chronology (no year 0) */
private int year(DateTime dt, int year) {
    Chronology c;//  w w  w .j  a v  a  2 s  .c om
    if (year < 0 && ((c = dt.getChronology()) instanceof JulianChronology
            || (c instanceof GJChronology && ((GJChronology) c).getGregorianCutover().isAfter(dt))))
        return year + 1;
    return year;
}

From source file:TVShowTimelineMaker.util.XML.DateTimeXMLWriter.java

@Override
public Element writeElements(DateTime ObjectToWrite) {
    Element newElement = new Element("DateTime");
    JodaTimeUtil timeUtil = JodaTimeUtil.getInstance();
    DateTimeZone zone = ObjectToWrite.getZone();
    Element TimeZoneElement = new Element("TimeZone");
    TimeZoneElement.setText(timeUtil.getStringForDateTimeZone(zone));
    newElement.addContent(TimeZoneElement);
    Chronology chronology = ObjectToWrite.getChronology();
    Element ChronologyElement = new Element("Chronology");
    ChronologyElement.setText(timeUtil.getStringForChronology(chronology));
    newElement.addContent(ChronologyElement);
    Element DateElement = new Element("Date");
    DateElement.setText(timeUtil.writeTimeString(ObjectToWrite));
    newElement.addContent(DateElement);// w  w  w  .  jav a  2s  .c o m
    return newElement;
}