The CalendarInterval class represents time intervals with separate month and microsecond components, providing precise handling of calendar-based time periods commonly used in SQL operations. This design accounts for the variable length of months while maintaining microsecond precision for sub-day intervals.
import org.apache.spark.unsafe.types.CalendarInterval;// Create interval with 2 months and 1.5 days
CalendarInterval interval = new CalendarInterval(2, 1500000000L); // 1.5 days in microseconds
// Create intervals from string representations
CalendarInterval fromString = CalendarInterval.fromString("2 months 3 days");
CalendarInterval caseInsensitive = CalendarInterval.fromCaseInsensitiveString("2 MONTHS 3 DAYS");
// Parse specific formats
CalendarInterval yearMonth = CalendarInterval.fromYearMonthString("1-6"); // 1 year 6 months
CalendarInterval dayTime = CalendarInterval.fromDayTimeString("5 12:30:45.123"); // 5 days, 12:30:45.123CalendarInterval interval1 = new CalendarInterval(1, CalendarInterval.MICROS_PER_DAY * 10); // 1 month, 10 days
CalendarInterval interval2 = new CalendarInterval(0, CalendarInterval.MICROS_PER_HOUR * 5); // 5 hours
// Arithmetic operations
CalendarInterval sum = interval1.add(interval2); // Add intervals
CalendarInterval diff = interval1.subtract(interval2); // Subtract intervals
CalendarInterval negated = interval1.negate(); // Negate interval
// Get milliseconds component
long millis = interval1.milliseconds(); // Convert microseconds to milliseconds// Using predefined time constants
long fiveMinutes = 5 * CalendarInterval.MICROS_PER_MINUTE;
long twoHours = 2 * CalendarInterval.MICROS_PER_HOUR;
long threeDays = 3 * CalendarInterval.MICROS_PER_DAY;
long oneWeek = CalendarInterval.MICROS_PER_WEEK;
CalendarInterval timeInterval = new CalendarInterval(0, fiveMinutes + twoHours);// Parse intervals with multiple units
CalendarInterval complex = CalendarInterval.fromString("1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds");
// Parse year-month format (SQL standard)
CalendarInterval yearMonthInterval = CalendarInterval.fromYearMonthString("2-5"); // 2 years, 5 months
// Parse day-time format (SQL standard)
CalendarInterval dayTimeInterval = CalendarInterval.fromDayTimeString("10 15:30:45.500"); // 10 days, 15:30:45.500
// Parse single unit intervals
CalendarInterval hours = CalendarInterval.fromSingleUnitString("hour", "24");
CalendarInterval days = CalendarInterval.fromSingleUnitString("day", "7");public final class CalendarInterval {
/**
* Number of months in the interval.
*/
public final int months;
/**
* Number of microseconds in the interval.
*/
public final long microseconds;
/**
* Creates a calendar interval with specified months and microseconds.
*/
public CalendarInterval(int months, long microseconds);
}public static final long MICROS_PER_MILLI = 1000L;
public static final long MICROS_PER_SECOND = 1000000L;
public static final long MICROS_PER_MINUTE = 60000000L;
public static final long MICROS_PER_HOUR = 3600000000L;
public static final long MICROS_PER_DAY = 86400000000L;
public static final long MICROS_PER_WEEK = 604800000000L;/**
* Returns microseconds converted to milliseconds.
*/
public long milliseconds();/**
* Adds two calendar intervals.
*/
public CalendarInterval add(CalendarInterval that);
/**
* Subtracts one interval from another.
*/
public CalendarInterval subtract(CalendarInterval that);
/**
* Returns the negated interval.
*/
public CalendarInterval negate();/**
* Parses interval from string representation (case-sensitive).
* Supports formats like "1 year 2 months 3 days 4 hours 5 minutes 6 seconds".
*/
public static CalendarInterval fromString(String s);
/**
* Parses interval from string representation (case-insensitive).
*/
public static CalendarInterval fromCaseInsensitiveString(String s);
/**
* Parses year-month format interval (e.g., "1-6" for 1 year 6 months).
*/
public static CalendarInterval fromYearMonthString(String s);
/**
* Parses day-time format interval (e.g., "5 12:30:45.123").
*/
public static CalendarInterval fromDayTimeString(String s);
/**
* Parses single unit interval (e.g., unit="day", s="5").
*/
public static CalendarInterval fromSingleUnitString(String unit, String s);/**
* Parses long value with range validation.
*/
public static long toLongWithRange(String fieldName, String s, long minValue, long maxValue);
/**
* Parses seconds with nanosecond precision, returns microseconds.
*/
public static long parseSecondNano(String secondNano);/**
* Compares intervals for equality.
*/
public boolean equals(Object other);
/**
* Returns hash code for the interval.
*/
public int hashCode();
/**
* Returns string representation of the interval.
*/
public String toString();The general string format supports combinations of the following units:
year, yearsmonth, monthsweek, weeksday, dayshour, hoursminute, minutessecond, secondsmillisecond, millisecondsmicrosecond, microsecondsExamples:
CalendarInterval.fromString("1 year 2 months")
CalendarInterval.fromString("3 days 4 hours 30 minutes")
CalendarInterval.fromString("2 weeks 5 seconds 500 milliseconds")SQL standard format for year-month intervals:
"Y-M" where Y is years and M is months"1-6" (1 year 6 months), "0-18" (18 months)SQL standard format for day-time intervals:
"D H:M:S.f" where D is days, H is hours, M is minutes, S is seconds, f is fractional seconds"5 12:30:45", "10 15:30:45.500"For parsing single units with a specific value:
CalendarInterval.fromSingleUnitString("day", "5") // 5 days
CalendarInterval.fromSingleUnitString("hour", "24") // 24 hoursMonth Precision: Months are stored separately because they have variable lengths (28-31 days).
Microsecond Precision: Sub-day intervals are stored with microsecond precision for maximum accuracy.
Immutability: CalendarInterval objects are immutable; arithmetic operations return new instances.
SQL Compatibility: String parsing follows SQL standard formats for INTERVAL types.
Range Limitations: Be aware of overflow when working with very large intervals.
Parsing Errors: Invalid string formats will throw exceptions during parsing.
Case Sensitivity: Use fromCaseInsensitiveString() for case-insensitive parsing.
// Common time periods
CalendarInterval oneDay = new CalendarInterval(0, CalendarInterval.MICROS_PER_DAY);
CalendarInterval oneWeek = new CalendarInterval(0, CalendarInterval.MICROS_PER_WEEK);
CalendarInterval oneMonth = new CalendarInterval(1, 0);
CalendarInterval oneYear = new CalendarInterval(12, 0);
// Mixed intervals
CalendarInterval quarter = new CalendarInterval(3, 0); // 3 months
CalendarInterval businessWeek = new CalendarInterval(0, 5 * CalendarInterval.MICROS_PER_DAY);try {
CalendarInterval interval = CalendarInterval.fromString("invalid format");
} catch (IllegalArgumentException e) {
// Handle parsing errors
System.err.println("Invalid interval format: " + e.getMessage());
}