Class DateExpressions


  • public final class DateExpressions
    extends Object
    Defines helper methods for the date expressions
    Since:
    2.0
    • Method Detail

      • dateAdd

        public static DateDeltaExpression dateAdd​(Expression startDate,
                                                  long amount,
                                                  TimeUnit unit)
        Increments a Date object by a specified number of time units.
        Parameters:
        startDate - The beginning date, in UTC, for the addition operation. The startDate can be any expression that resolves to a Date, a Timestamp, or an ObjectID.
        amount - The number of units added to the startDate.
        unit - The unit used to measure the amount of time added to the startDate.
        Returns:
        the new expression
        Since:
        2.3
        Since server release
        5.0
        MongoDB documentation
        Aggregration Expression: $dateAdd
      • dateDiff

        public static DateDiffExpression dateDiff​(Expression startDate,
                                                  Expression endDate,
                                                  TimeUnit unit)
        Returns the difference between two dates.
        Parameters:
        startDate - The beginning date, in UTC, for the addition operation. The startDate can be any expression that resolves to a Date, a Timestamp, or an ObjectID.
        endDate - The beginning date, in UTC, for the addition operation. The endDate can be any expression that resolves to a Date, a Timestamp, or an ObjectID.
        unit - The unit used to measure the amount of time added to the startDate.
        Returns:
        the new expression
        Since:
        2.3
        Since server release
        5.0
        MongoDB documentation
        Aggregration Expression: $dateDiff
      • dateSubtract

        public static DateDeltaExpression dateSubtract​(Expression startDate,
                                                       long amount,
                                                       TimeUnit unit)
        Decrements a Date object by a specified number of time units.
        Parameters:
        startDate - The beginning date, in UTC, for the subtraction operation. The startDate can be any expression that resolves to a Date, a Timestamp, or an ObjectID.
        amount - The number of units subtracted to the startDate.
        unit - The unit used to measure the amount of time subtracted to the startDate.
        Returns:
        the new expression
        Since:
        2.3
        Since server release
        5.0
        MongoDB documentation
        Aggregration Expression: $dateSubtract
      • dateToParts

        public static DateToParts dateToParts​(Expression date)
        Constructs and returns a Date object given the date’s constituent properties.
        Parameters:
        date - The input date for which to return parts.
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $dateToParts
      • dateTrunc

        public static DateTruncExpression dateTrunc​(Expression date,
                                                    TimeUnit unit)
        Truncates a date.
        Parameters:
        date - The date to truncate, specified in UTC. The date can be any expression that resolves to a Date, a Timestamp, or an ObjectID.
        unit - The unit used to measure the amount of time added to the startDate.
        Returns:
        the new expression
        Since:
        2.3
        Since server release
        5.0
        MongoDB documentation
        Aggregration Expression: $dateTrunc
      • isoDayOfWeek

        public static IsoDates isoDayOfWeek​(Expression value)
        Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $isoDayOfWeek
      • isoWeek

        public static IsoDates isoWeek​(Expression value)
        Returns the week number in ISO 8601 format, ranging from 1 to 53. Week numbers start at 1 with the week (Monday through Sunday) that contains the year’s first Thursday.
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $isoWeek
      • isoWeekYear

        public static IsoDates isoWeekYear​(Expression value)
        Returns the year number in ISO 8601 format. The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601).
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $isoWeekYear
      • toDate

        public static DateExpressions.DateExpression toDate​(Expression value)
        Converts a value to a date. If the value cannot be converted to a date, $toDate errors. If the value is null or missing, $toDate returns null.
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $toDate
      • tsIncrement

        public static Expression tsIncrement​(Expression expression)
        Returns the incrementing ordinal from a timestamp as a long.
        Parameters:
        expression - the expression to use when incrementing
        Returns:
        the new expression
        Since:
        2.3
        Since server release
        5.1
        MongoDB documentation
        Aggregration Expression: $tsIncrement
      • tsSecond

        public static Expression tsSecond​(Expression expression)
        Returns the seconds from a timestamp as a long.
        Parameters:
        expression - the expression to use
        Returns:
        the new expression
        Since:
        2.3
        Since server release
        5.1
        MongoDB documentation
        Aggregration Expression: $tsSecond
      • week

        public static DateExpressions.DateExpression week​(Expression value)
        Returns the week number for a date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year).
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $week