Example usage for org.joda.time.chrono GJChronology getInstance

List of usage examples for org.joda.time.chrono GJChronology getInstance

Introduction

In this page you can find the example usage for org.joda.time.chrono GJChronology getInstance.

Prototype

public static GJChronology getInstance(DateTimeZone zone) 

Source Link

Document

Factory method returns instances of the GJ cutover chronology.

Usage

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

License:Apache License

/**
 * @return The default chronology ({@link ISOChronology} or
 *         {@link GJChronology}) using the result of
 *         {@link #getDefaultDateTimeZone()}
 * @see #isUseISOChronology()/*  w w w.j a v a  2s  .  co  m*/
 * @see #getDefaultDateTimeZone()
 */
@Nonnull
public static Chronology getDefaultChronology() {
    if (isUseISOChronology())
        return ISOChronology.getInstance(getDefaultDateTimeZone());
    return GJChronology.getInstance(getDefaultDateTimeZone());
}

From source file:com.phloc.datetime.config.PDTConfig.java

License:Apache License

/**
 * @return The default GJ chronology using the result of
 *         {@link #getDefaultDateTimeZone()}
 *//*from   w  w w .  j ava  2  s.  co m*/
@Nonnull
public static Chronology getDefaultChronology() {
    if (s_bUseISOChronology)
        return ISOChronology.getInstance(getDefaultDateTimeZone());
    return GJChronology.getInstance(getDefaultDateTimeZone());
}

From source file:jp.furplag.util.time.DateTimeUtils.java

License:Apache License

/**
 * {@link org.joda.time.DateTime#DateTime(Object, DateTimeZone)}.
 *
 * @param instant the date-time object, null means current date-time.
 * @param zone the time zone, null means UTC (NOT default) .
 * @param strictly if true, returns null when the instant is invalid.
 * @return {@link org.joda.time.DateTime#DateTime(Object, DateTimeZone)}.
 */// www.j a v a 2  s .  co  m
private static DateTime getDateTime(final Object instant, final DateTimeZone zone, final boolean strictly) {
    if (strictly && instant == null)
        return null;
    if (instant == null)
        return DateTime.now(zone == null ? DateTimeZone.UTC : zone);
    Long millis = getMillis(instant, strictly);
    if (millis == null)
        return null;
    try {
        if (millis > GREGORIAN_CUTOVER)
            return new DateTime((long) millis, zone == null ? DateTimeZone.UTC : zone);

        return new DateTime((long) millis,
                zone == null ? GJChronology.getInstanceUTC() : GJChronology.getInstance(zone));
    } catch (Exception e) {
    }
    if (strictly)
        return null;

    return DateTime.now(zone == null ? DateTimeZone.UTC : zone);
}

From source file:jp.furplag.util.time.JodaPrettifier.java

License:Apache License

/**
 * Return the prettified String if the period includes specified moment.
 *
 * <pre>//from  w w w.j  ava 2s. co  m
 * prettify(DateTime.now().minusHours(1), null, null, null, null) = "one hour ago." prettify(DateTime.now(), DateTime.now().plusYears(1), null, null, null) = "one year ago." prettify(DateTime.now().minusHours(1), null, null, null, new Period().withDays(1)) = "one hour ago." prettify(DateTime.now().minusHours(1), null, null, null, new Period().withMinites(10)) =
 * DateTime.now().withZone(DateTimeZone.UTC).minusHours(1).toString(DateTimeFormat.forStyle("-M"))
 *
 * <pre>
 *
 * @param then the datetime object, null means current date-time.
 * @param reference the moment of a starting point ( {@link org.joda.time.ReadableInstant} and {@link Long} specifiable ). Use {@code DateTime.now()} as a start point if {@code reference} is null.
 * @param locale the language for Localization ( {@code String} and {@code Locale} specifiable ). Use ROOT if {@code locale} is null.
 * @param limit if the moment is in the specified period, return prettified String ( {@code Period} and {@code Interval} specifiable ). Prettify all, if null.
 * @return the prettified String if the period includes specified moment. In other situation, return stringified date-time.
 */
public static String prettify(final Object then, final Object reference, final Locale locale,
        final DateTimeZone zone, final Object limit) {
    DateTime temporary = DateTimeUtils.toDT(then, zone, true);
    if (temporary == null)
        return StringUtils.EMPTY;
    DateTime ref = DateTimeUtils.toDT(reference, temporary.getZone(), true);
    if (ref == null)
        return doPrettify(temporary, null, locale);
    if (ref.isEqual(temporary))
        ref = ref.plusMillis(1);
    if (limit == null)
        return doPrettify(temporary, ref, locale);
    Interval limitter = null;
    if (Interval.class.equals(limit))
        limitter = (Interval) limit;
    if (limit instanceof Period) {
        limitter = new Interval(ref.minus((Period) limit), ref.plusMillis(1).plus((Period) limit));
    }
    if (limit instanceof BaseSingleFieldPeriod) {
        limitter = new Interval(ref.minus(new Period(limit)), ref.plusMillis(1).plus(new Period(limit)));
    }
    if (ObjectUtils.isAny(ClassUtils.primitiveToWrapper(limit.getClass()), Double.class, Float.class)) {
        limitter = new Interval(toDT(toAJD(ref) - NumberUtils.valueOf(limit, double.class), ref),
                toDT(toAJD(ref) + NumberUtils.valueOf(limit, double.class), ref));
    } else if (BigDecimal.class.equals(limit.getClass())) {
        if (NumberUtils.compareTo((BigDecimal) limit, NumberUtils.down(limit)) == 0) {
            limitter = new Interval(ref.minusMillis(NumberUtils.valueOf(limit, int.class)),
                    ref.plusMillis(NumberUtils.valueOf(limit, int.class) + 1));
        } else {
            limitter = new Interval(toDT(toAJD(ref) - NumberUtils.valueOf(limit, double.class), ref),
                    toDT(toAJD(ref) + NumberUtils.valueOf(limit, double.class), ref));
        }
    } else if (Number.class.isAssignableFrom(ClassUtils.primitiveToWrapper(limit.getClass()))) {
        limitter = new Interval(ref.minusMillis(NumberUtils.valueOf(limit, int.class)),
                ref.plusMillis(NumberUtils.valueOf(limit, int.class) + 1));
    }
    if (DateTime.class.equals(limit.getClass())) {
        limitter = new Interval(ref.minus(((DateTime) limit).getMillis()),
                ref.plus(((DateTime) limit).getMillis() + 1L));
    }
    if (Boolean.class.equals(limit.getClass())) {
        limitter = new Interval(temporary.minusMillis(1),
                ((Boolean) limit) ? temporary.plusMillis(1) : temporary.minusMillis(1));
    }
    if (limitter == null)
        return doPrettify(temporary, ref, locale);
    if (limitter.contains(temporary))
        return doPrettify(temporary, ref, locale);

    return toDT(temporary, GJChronology.getInstance(temporary.getZone()))
            .toString(DateTimeFormat.forStyle(isToday(temporary, temporary.getZone()) ? "-M" : "MS")
                    .withLocale(locale == null ? Locale.ROOT : locale));
}

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

License:LGPL

static Chronology getChronology(ThreadContext context, final long sg, final DateTimeZone zone) {
    if (sg == ITALY)
        return GJChronology.getInstance(zone);
    if (sg == JULIAN)
        return JulianChronology.getInstance(zone);
    if (sg == GREGORIAN)
        return GregorianChronology.getInstance(zone);

    Instant cutover = new Instant(DateTimeUtils.fromJulianDay(jd_to_ajd(sg)));
    try {//from  w w w  . j  av a 2  s .  c  o  m
        return GJChronology.getInstance(zone, cutover);
    } // java.lang.IllegalArgumentException: Cutover too early. Must be on or after 0001-01-01.
    catch (IllegalArgumentException ex) {
        debug(context, "invalid date", ex);
        throw context.runtime.newArgumentError("invalid date");
    }
}

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

License:LGPL

/**
 # Create a new DateTime object representing the current time.
 #
 # +sg+ specifies the Day of Calendar Reform.
 **///from  ww w  . j av a  2s  .  c om

@JRubyMethod(meta = true)
public static RubyDateTime now(ThreadContext context, IRubyObject self) { // sg=ITALY
    final DateTimeZone zone = RubyTime.getLocalTimeZone(context.runtime);
    if (zone == DateTimeZone.UTC) {
        return new RubyDateTime(context.runtime, (RubyClass) self, new DateTime(CHRONO_ITALY_UTC), 0);
    }
    final DateTime dt = new DateTime(GJChronology.getInstance(zone));
    final int off = zone.getOffset(dt.getMillis()) / 1000;
    return new RubyDateTime(context.runtime, (RubyClass) self, dt, off, ITALY);
}

From source file:org.jugular.jodatime.factory.ChronologyFactory.java

License:Apache License

public Chronology create(Type chronologyType, String zoneId) {
    DateTimeZone zone;/*w w w. j av a 2  s  . c  o  m*/
    Chronology chronology;

    if (zoneId == null || "".equals(zoneId)) {
        zone = controller.getDateTimeZone();
    } else {
        zone = DateTimeZone.forID(zoneId);
    }

    switch (chronologyType) {
    case BUDDHIST:
        chronology = BuddhistChronology.getInstance(zone);
        break;
    case COPTIC:
        chronology = CopticChronology.getInstance(zone);
        break;
    case ETHIOPIC:
        chronology = EthiopicChronology.getInstance(zone);
        break;
    case GREGORIAN:
        chronology = GregorianChronology.getInstance(zone);
        break;
    case GREGORIAN_JULIAN:
        chronology = GJChronology.getInstance(zone);
        break;
    case ISLAMIC:
        chronology = IslamicChronology.getInstance(zone);
        break;
    case ISO:
        chronology = ISOChronology.getInstance(zone);
        break;
    case JULIAN:
        chronology = JulianChronology.getInstance(zone);
        break;
    default:
        throw new AssertionError("Calender system not supported");
    }

    return chronology;

}