Java Code Examples for java.util.GregorianCalendar#AD

The following examples show how to use java.util.GregorianCalendar#AD . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateTime.java    From jTDS with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Constructs a DateTime object from a <code>java.sql.Timestamp</code>.
 *
 * @param ts <code>Timestamp</code> object representing the datetime
 * @throws SQLException if the date is out of range
 */
DateTime(Timestamp ts) throws SQLException {
    tsValue = ts;
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(ts);

    if (cal.get(Calendar.ERA) != GregorianCalendar.AD)
        throw new SQLException(Messages.get("error.datetime.range.era"), "22007");

    year   = (short)cal.get(Calendar.YEAR);
    month  = (short)(cal.get(Calendar.MONTH) + 1);
    day    = (short)cal.get(Calendar.DAY_OF_MONTH);
    hour   = (short)cal.get(Calendar.HOUR_OF_DAY);
    minute = (short)cal.get(Calendar.MINUTE);
    second = (short)cal.get(Calendar.SECOND);
    millis = (short)cal.get(Calendar.MILLISECOND);
    packDate();
    packTime();
    unpacked = true;
}
 
Example 2
Source File: DateTime.java    From jTDS with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Constructs a DateTime object from a <code>java.sql.Time</code>.
 *
 * @param t <code>Time</code> object representing the datetime
 * @throws SQLException if the time (date) is out of range
 */
DateTime(Time t) throws SQLException {
    timeValue = t;
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(t);

    if (cal.get(Calendar.ERA) != GregorianCalendar.AD)
        throw new SQLException(Messages.get("error.datetime.range.era"), "22007");

    date   = DATE_NOT_USED;
    year   = 1900;
    month  = 1;
    day    = 1;
    hour   = (short)cal.get(Calendar.HOUR_OF_DAY);
    minute = (short)cal.get(Calendar.MINUTE);
    second = (short)cal.get(Calendar.SECOND);
    millis = (short)cal.get(Calendar.MILLISECOND);
    packTime();
    year  = 1970;
    month = 1;
    day   = 1;
    unpacked   = true;
}
 
Example 3
Source File: DateTime.java    From jTDS with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Constructs a DateTime object from a <code>java.sql.Date</code>.
 *
 * @param d <code>Date</code> object representing the datetime
 * @throws SQLException if the Date is out of range
 */
DateTime(Date d) throws SQLException {
    dateValue = d;
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(d);

    if (cal.get(Calendar.ERA) != GregorianCalendar.AD)
        throw new SQLException(Messages.get("error.datetime.range.era"), "22007");

    year   = (short)cal.get(Calendar.YEAR);
    month  = (short)(cal.get(Calendar.MONTH) + 1);
    day    = (short)cal.get(Calendar.DAY_OF_MONTH);
    hour   = 0;
    minute = 0;
    second = 0;
    millis = 0;
    packDate();
    time = TIME_NOT_USED;
    unpacked  = true;
}
 
Example 4
Source File: JulianDate.java    From solarpositioning with MIT License 6 votes vote down vote up
private double calcJulianDate(GregorianCalendar calendar) {
    int y = (calendar.get(Calendar.ERA) == GregorianCalendar.AD) ? calendar.get(Calendar.YEAR) : -calendar
            .get(Calendar.YEAR);
    int m = calendar.get(Calendar.MONTH) + 1;

    if (m < 3) {
        y = y - 1;
        m = m + 12;
    }

    final double d = calendar.get(Calendar.DAY_OF_MONTH)
            + (calendar.get(Calendar.HOUR_OF_DAY) + (calendar.get(Calendar.MINUTE) + calendar.get(Calendar.SECOND) / 60.0) / 60.0)
            / 24.0;
    final double jd = Math.floor(365.25 * (y + 4716.0)) + Math.floor(30.6001 * (m + 1)) + d - 1524.5;
    final double a = Math.floor(y / 100.0);
    final double b = jd > 2299160.0 ? (2.0 - a + Math.floor(a / 4.0)) : 0.0;

    return jd + b;
}
 
Example 5
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reported bug is that a GregorianCalendar with a cutover of Date(Long.MAX_VALUE)
 * doesn't behave as a pure Julian calendar.
 * CANNOT REPRODUCE THIS BUG
 */
public void Test4149677() {
    TimeZone[] zones = {TimeZone.getTimeZone("GMT"),
        TimeZone.getTimeZone("PST"),
        TimeZone.getTimeZone("EAT")};
    for (int i = 0; i < zones.length; ++i) {
        GregorianCalendar calendar = new GregorianCalendar(zones[i]);

        // Make sure extreme values don't wrap around
        calendar.setTime(new Date(Long.MIN_VALUE));
        if (calendar.get(ERA) != GregorianCalendar.BC) {
            errln("Fail: Date(Long.MIN_VALUE) has an AD year in " + zones[i]);
        }
        calendar.setTime(new Date(Long.MAX_VALUE));
        if (calendar.get(ERA) != GregorianCalendar.AD) {
            errln("Fail: Date(Long.MAX_VALUE) has a BC year in " + zones[i]);
        }

        calendar.setGregorianChange(new Date(Long.MAX_VALUE));
        // to obtain a pure Julian calendar

        boolean is100Leap = calendar.isLeapYear(100);
        if (!is100Leap) {
            errln("test failed with zone " + zones[i].getID());
            errln(" cutover date is Date(Long.MAX_VALUE)");
            errln(" isLeapYear(100) returns: " + is100Leap);
        }
    }
}
 
Example 6
Source File: SumkDate.java    From sumk with Apache License 2.0 4 votes vote down vote up
public static SumkDate of(Calendar cal) {

		int y = cal.get(Calendar.ERA) == GregorianCalendar.AD ? cal.get(Calendar.YEAR) : 1 - cal.get(Calendar.YEAR);
		return of(y, cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY),
				cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), cal.get(Calendar.MILLISECOND));
	}
 
Example 7
Source File: PlatformTimezone.java    From Time4A with Apache License 2.0 4 votes vote down vote up
@Override
public ZonalOffset getOffset(
    GregorianDate localDate,
    WallTime localTime
) {

    if (this.fixedOffset != null) {
        return this.fixedOffset;
    }

    int year = localDate.getYear();
    int month = localDate.getMonth();
    int dom = localDate.getDayOfMonth();

    int era;
    int yearOfEra;

    if (localTime.getHour() == 24) {
        long mjd = MathUtils.safeAdd(GregorianMath.toMJD(localDate), 1);
        long pd = GregorianMath.toPackedDate(mjd);
        year = GregorianMath.readYear(pd);
        month = GregorianMath.readMonth(pd);
        dom = GregorianMath.readDayOfMonth(pd);
    }

    if (year > 0) {
        era = GregorianCalendar.AD;
        yearOfEra = year;
    } else {
        era = GregorianCalendar.BC;
        yearOfEra = 1 - year;
    }

    int dow = GregorianMath.getDayOfWeek(year, month, dom) + 1;

    if (dow == 8) {
        dow = Calendar.SUNDAY;
    }

    int millis;

    if (localTime.getHour() == 24) {
        millis = 0;
    } else {
        millis = (
            localTime.getHour() * 3600
            + localTime.getMinute() * 60
            + localTime.getSecond()
        ) * 1000 + (localTime.getNanosecond() / 1000000);
    }

    java.util.TimeZone inner;

    if (this.id == null) {
        inner = java.util.TimeZone.getDefault();
    } else {
        inner = this.tz;
    }

    return fromOffsetMillis(
        inner.getOffset(era, yearOfEra, month - 1, dom, dow, millis));

}
 
Example 8
Source File: Time_12_LocalDateTime_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Constructs a LocalDateTime from a <code>java.util.Calendar</code>
 * using exactly the same field values.
 * <p>
 * Each field is queried from the Calendar and assigned to the LocalDateTime.
 * This is useful if you have been using the Calendar as a local date,
 * ignoring the zone.
 * <p>
 * One advantage of this method is that this method is unaffected if the
 * version of the time zone data differs between the JDK and Joda-Time.
 * That is because the local field values are transferred, calculated using
 * the JDK time zone data and without using the Joda-Time time zone data.
 * <p>
 * This factory method ignores the type of the calendar and always
 * creates a LocalDateTime with ISO chronology. It is expected that you
 * will only pass in instances of <code>GregorianCalendar</code> however
 * this is not validated.
 *
 * @param calendar  the Calendar to extract fields from, not null
 * @return the created local date-time, not null
 * @throws IllegalArgumentException if the calendar is null
 * @throws IllegalArgumentException if the date is invalid for the ISO chronology
 */
public static LocalDateTime fromCalendarFields(Calendar calendar) {
    if (calendar == null) {
        throw new IllegalArgumentException("The calendar must not be null");
    }
    int era = calendar.get(Calendar.ERA);
    int yearOfEra = calendar.get(Calendar.YEAR);
    return new LocalDateTime(
        (era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH),
        calendar.get(Calendar.HOUR_OF_DAY),
        calendar.get(Calendar.MINUTE),
        calendar.get(Calendar.SECOND),
        calendar.get(Calendar.MILLISECOND)
    );
}
 
Example 9
Source File: Timestamp.java    From ion-java with Apache License 2.0 4 votes vote down vote up
/**
 * Copies data from a {@link Calendar} into this timestamp.
 * Must only be called during construction due to timestamp immutabliity.
 *
 * @param cal must have at least one field set.
 *
 * @throws IllegalArgumentException if the calendar has no fields set.
 */
private void set_fields_from_calendar(Calendar cal,
                                      Precision precision,
                                      boolean setLocalOffset)
{
    _precision = precision;
    _offset = UNKNOWN_OFFSET;
    boolean dayPrecision = false;
    boolean calendarHasMilliseconds = cal.isSet(Calendar.MILLISECOND);

    switch (this._precision) {
        case FRACTION:
        case SECOND:
            this._second = checkAndCastSecond(cal.get(Calendar.SECOND));
            if (calendarHasMilliseconds) {
                BigDecimal millis = BigDecimal.valueOf(cal.get(Calendar.MILLISECOND));
                this._fraction = millis.movePointLeft(3); // convert to fraction
                checkFraction(precision, this._fraction);
            }
        case MINUTE:
        {
            this._hour   = checkAndCastHour(cal.get(Calendar.HOUR_OF_DAY));
            this._minute = checkAndCastMinute(cal.get(Calendar.MINUTE));

            // If this test is made before calling get(), it will return
            // false even when Calendar.setTimeZone() was called.
            if (setLocalOffset && cal.isSet(Calendar.ZONE_OFFSET))
            {
                int offset = cal.get(Calendar.ZONE_OFFSET);
                if (cal.isSet(Calendar.DST_OFFSET)) {
                    offset += cal.get(Calendar.DST_OFFSET);
                }

                // convert ms to minutes
                _offset = offset / (1000*60);
            }
        }
        case DAY:
            dayPrecision = true;
        case MONTH:
            // Calendar months are 0 based, Timestamp months are 1 based
            this._month  = checkAndCastMonth((cal.get(Calendar.MONTH) + 1));
        case YEAR:
            int year;
            if(cal.get(Calendar.ERA) == GregorianCalendar.AD) {
                year = cal.get(Calendar.YEAR);
            }
            else {
                year = -cal.get(Calendar.YEAR);
            }

            this._year = checkAndCastYear(year);
    }

    if (dayPrecision)
    {
        this._day = checkAndCastDay(cal.get(Calendar.DAY_OF_MONTH), _year, _month);
    }

    if (_offset != UNKNOWN_OFFSET) {
        // Transform our members from local time to Zulu
        this.apply_offset(_offset);
    }
}
 
Example 10
Source File: LocalDateTime.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a LocalDateTime from a <code>java.util.Calendar</code>
 * using exactly the same field values.
 * <p>
 * Each field is queried from the Calendar and assigned to the LocalDateTime.
 * This is useful if you have been using the Calendar as a local date,
 * ignoring the zone.
 * <p>
 * One advantage of this method is that this method is unaffected if the
 * version of the time zone data differs between the JDK and Joda-Time.
 * That is because the local field values are transferred, calculated using
 * the JDK time zone data and without using the Joda-Time time zone data.
 * <p>
 * This factory method ignores the type of the calendar and always
 * creates a LocalDateTime with ISO chronology. It is expected that you
 * will only pass in instances of <code>GregorianCalendar</code> however
 * this is not validated.
 *
 * @param calendar  the Calendar to extract fields from, not null
 * @return the created local date-time, not null
 * @throws IllegalArgumentException if the calendar is null
 * @throws IllegalArgumentException if the date is invalid for the ISO chronology
 */
public static LocalDateTime fromCalendarFields(Calendar calendar) {
    if (calendar == null) {
        throw new IllegalArgumentException("The calendar must not be null");
    }
    int era = calendar.get(Calendar.ERA);
    int yearOfEra = calendar.get(Calendar.YEAR);
    return new LocalDateTime(
        (era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH),
        calendar.get(Calendar.HOUR_OF_DAY),
        calendar.get(Calendar.MINUTE),
        calendar.get(Calendar.SECOND),
        calendar.get(Calendar.MILLISECOND)
    );
}
 
Example 11
Source File: LocalDateTime.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a LocalDateTime from a <code>java.util.Calendar</code>
 * using exactly the same field values.
 * <p>
 * Each field is queried from the Calendar and assigned to the LocalDateTime.
 * This is useful if you have been using the Calendar as a local date,
 * ignoring the zone.
 * <p>
 * One advantage of this method is that this method is unaffected if the
 * version of the time zone data differs between the JDK and Joda-Time.
 * That is because the local field values are transferred, calculated using
 * the JDK time zone data and without using the Joda-Time time zone data.
 * <p>
 * This factory method ignores the type of the calendar and always
 * creates a LocalDateTime with ISO chronology. It is expected that you
 * will only pass in instances of <code>GregorianCalendar</code> however
 * this is not validated.
 *
 * @param calendar  the Calendar to extract fields from, not null
 * @return the created local date-time, not null
 * @throws IllegalArgumentException if the calendar is null
 * @throws IllegalArgumentException if the date is invalid for the ISO chronology
 */
public static LocalDateTime fromCalendarFields(Calendar calendar) {
    if (calendar == null) {
        throw new IllegalArgumentException("The calendar must not be null");
    }
    int era = calendar.get(Calendar.ERA);
    int yearOfEra = calendar.get(Calendar.YEAR);
    return new LocalDateTime(
        (era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH),
        calendar.get(Calendar.HOUR_OF_DAY),
        calendar.get(Calendar.MINUTE),
        calendar.get(Calendar.SECOND),
        calendar.get(Calendar.MILLISECOND)
    );
}
 
Example 12
Source File: Time_12_LocalDate_t.java    From coming with MIT License 3 votes vote down vote up
/**
 * Constructs a LocalDate from a <code>java.util.Calendar</code>
 * using exactly the same field values.
 * <p>
 * Each field is queried from the Calendar and assigned to the LocalDate.
 * This is useful if you have been using the Calendar as a local date,
 * ignoring the zone.
 * <p>
 * One advantage of this method is that this method is unaffected if the
 * version of the time zone data differs between the JDK and Joda-Time.
 * That is because the local field values are transferred, calculated using
 * the JDK time zone data and without using the Joda-Time time zone data.
 * <p>
 * This factory method ignores the type of the calendar and always
 * creates a LocalDate with ISO chronology. It is expected that you
 * will only pass in instances of <code>GregorianCalendar</code> however
 * this is not validated.
 *
 * @param calendar  the Calendar to extract fields from, not null
 * @return the created local date, not null
 * @throws IllegalArgumentException if the calendar is null
 * @throws IllegalArgumentException if the date is invalid for the ISO chronology
 */
public static LocalDate fromCalendarFields(Calendar calendar) {
    if (calendar == null) {
        throw new IllegalArgumentException("The calendar must not be null");
    }
    int era = calendar.get(Calendar.ERA);
    int yearOfEra = calendar.get(Calendar.YEAR);
    return new LocalDate(
        (era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH)
    );
}
 
Example 13
Source File: LocalDate.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Constructs a LocalDate from a <code>java.util.Calendar</code>
 * using exactly the same field values.
 * <p>
 * Each field is queried from the Calendar and assigned to the LocalDate.
 * This is useful if you have been using the Calendar as a local date,
 * ignoring the zone.
 * <p>
 * One advantage of this method is that this method is unaffected if the
 * version of the time zone data differs between the JDK and Joda-Time.
 * That is because the local field values are transferred, calculated using
 * the JDK time zone data and without using the Joda-Time time zone data.
 * <p>
 * This factory method ignores the type of the calendar and always
 * creates a LocalDate with ISO chronology. It is expected that you
 * will only pass in instances of <code>GregorianCalendar</code> however
 * this is not validated.
 *
 * @param calendar  the Calendar to extract fields from, not null
 * @return the created local date, not null
 * @throws IllegalArgumentException if the calendar is null
 * @throws IllegalArgumentException if the date is invalid for the ISO chronology
 */
public static LocalDate fromCalendarFields(Calendar calendar) {
    if (calendar == null) {
        throw new IllegalArgumentException("The calendar must not be null");
    }
    int era = calendar.get(Calendar.ERA);
    int yearOfEra = calendar.get(Calendar.YEAR);
    return new LocalDate(
        (era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH)
    );
}
 
Example 14
Source File: LocalDate.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Constructs a LocalDate from a <code>java.util.Calendar</code>
 * using exactly the same field values.
 * <p>
 * Each field is queried from the Calendar and assigned to the LocalDate.
 * This is useful if you have been using the Calendar as a local date,
 * ignoring the zone.
 * <p>
 * One advantage of this method is that this method is unaffected if the
 * version of the time zone data differs between the JDK and Joda-Time.
 * That is because the local field values are transferred, calculated using
 * the JDK time zone data and without using the Joda-Time time zone data.
 * <p>
 * This factory method ignores the type of the calendar and always
 * creates a LocalDate with ISO chronology. It is expected that you
 * will only pass in instances of <code>GregorianCalendar</code> however
 * this is not validated.
 *
 * @param calendar  the Calendar to extract fields from, not null
 * @return the created local date, not null
 * @throws IllegalArgumentException if the calendar is null
 * @throws IllegalArgumentException if the date is invalid for the ISO chronology
 */
public static LocalDate fromCalendarFields(Calendar calendar) {
    if (calendar == null) {
        throw new IllegalArgumentException("The calendar must not be null");
    }
    int era = calendar.get(Calendar.ERA);
    int yearOfEra = calendar.get(Calendar.YEAR);
    return new LocalDate(
        (era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH)
    );
}