Class DateExpressions


  • public final class DateExpressions
    extends Object
    Defines helper methods for the date expressions
    Since:
    2.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  DateExpressions.DateExpression
      Base class for the date expressions
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static dev.morphia.aggregation.expressions.impls.DateDeltaExpression dateAdd​(dev.morphia.aggregation.expressions.impls.Expression startDate, long amount, TimeUnit unit)
      Increments a Date object by a specified number of time units.
      static dev.morphia.aggregation.expressions.impls.DateDiffExpression dateDiff​(dev.morphia.aggregation.expressions.impls.Expression startDate, dev.morphia.aggregation.expressions.impls.Expression endDate, TimeUnit unit)
      Returns the difference between two dates.
      static dev.morphia.aggregation.expressions.impls.DateFromParts dateFromParts()
      Constructs and returns a Date object given the date’s constituent properties.
      static dev.morphia.aggregation.expressions.impls.DateFromString dateFromString()
      Converts a date/time string to a date object.
      static dev.morphia.aggregation.expressions.impls.DateDeltaExpression dateSubtract​(dev.morphia.aggregation.expressions.impls.Expression startDate, long amount, TimeUnit unit)
      Decrements a Date object by a specified number of time units.
      static dev.morphia.aggregation.expressions.impls.DateToParts dateToParts​(dev.morphia.aggregation.expressions.impls.Expression date)
      Constructs and returns a Date object given the date’s constituent properties.
      static dev.morphia.aggregation.expressions.impls.DateToString dateToString()
      Returns the date as a formatted string.
      static dev.morphia.aggregation.expressions.impls.DateTruncExpression dateTrunc​(dev.morphia.aggregation.expressions.impls.Expression date, TimeUnit unit)
      Truncates a date.
      static DateExpressions.DateExpression dayOfMonth​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the day of the month for a date as a number between 1 and 31.
      static DateExpressions.DateExpression dayOfWeek​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).
      static DateExpressions.DateExpression dayOfYear​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the day of the year for a date as a number between 1 and 366 (leap year).
      static DateExpressions.DateExpression hour​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the hour for a date as a number between 0 and 23.
      static dev.morphia.aggregation.expressions.impls.IsoDates isoDayOfWeek​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).
      static dev.morphia.aggregation.expressions.impls.IsoDates isoWeek​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the week number in ISO 8601 format, ranging from 1 to 53.
      static dev.morphia.aggregation.expressions.impls.IsoDates isoWeekYear​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the year number in ISO 8601 format.
      static DateExpressions.DateExpression milliseconds​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the milliseconds of a date as a number between 0 and 999.
      static DateExpressions.DateExpression minute​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the minute for a date as a number between 0 and 59.
      static DateExpressions.DateExpression month​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the month for a date as a number between 1 (January) and 12 (December).
      static DateExpressions.DateExpression second​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the seconds for a date as a number between 0 and 60 (leap seconds).
      static DateExpressions.DateExpression toDate​(dev.morphia.aggregation.expressions.impls.Expression value)
      Converts a value to a date.
      static dev.morphia.aggregation.expressions.impls.Expression tsIncrement​(dev.morphia.aggregation.expressions.impls.Expression expression)
      Returns the incrementing ordinal from a timestamp as a long.
      static dev.morphia.aggregation.expressions.impls.Expression tsSecond​(dev.morphia.aggregation.expressions.impls.Expression expression)
      Returns the seconds from a timestamp as a long.
      static DateExpressions.DateExpression week​(dev.morphia.aggregation.expressions.impls.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).
      static DateExpressions.DateExpression year​(dev.morphia.aggregation.expressions.impls.Expression value)
      Returns the year for a date as a number (e.g.
    • Method Detail

      • dateAdd

        public static dev.morphia.aggregation.expressions.impls.DateDeltaExpression dateAdd​(dev.morphia.aggregation.expressions.impls.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
        MongoDB documentation
        Aggregration Expression: $dateAdd
        Since server release
        5.0
      • dateDiff

        public static dev.morphia.aggregation.expressions.impls.DateDiffExpression dateDiff​(dev.morphia.aggregation.expressions.impls.Expression startDate,
                                                                                            dev.morphia.aggregation.expressions.impls.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
        MongoDB documentation
        Aggregration Expression: $dateDiff
        Since server release
        5.0
      • dateFromParts

        public static dev.morphia.aggregation.expressions.impls.DateFromParts dateFromParts()
        Constructs and returns a Date object given the date’s constituent properties.
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $dateFromParts
      • dateFromString

        public static dev.morphia.aggregation.expressions.impls.DateFromString dateFromString()
        Converts a date/time string to a date object.
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $dateFromString
      • dateSubtract

        public static dev.morphia.aggregation.expressions.impls.DateDeltaExpression dateSubtract​(dev.morphia.aggregation.expressions.impls.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
        MongoDB documentation
        Aggregration Expression: $dateSubtract
        Since server release
        5.0
      • dateToParts

        public static dev.morphia.aggregation.expressions.impls.DateToParts dateToParts​(dev.morphia.aggregation.expressions.impls.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
      • dateToString

        public static dev.morphia.aggregation.expressions.impls.DateToString dateToString()
        Returns the date as a formatted string.
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $dateToString
      • dateTrunc

        public static dev.morphia.aggregation.expressions.impls.DateTruncExpression dateTrunc​(dev.morphia.aggregation.expressions.impls.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
        MongoDB documentation
        Aggregration Expression: $dateTrunc
        Since server release
        5.0
      • dayOfMonth

        public static DateExpressions.DateExpression dayOfMonth​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the day of the month for a date as a number between 1 and 31.
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $dayOfMonth
      • dayOfWeek

        public static DateExpressions.DateExpression dayOfWeek​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $dayOfWeek
      • dayOfYear

        public static DateExpressions.DateExpression dayOfYear​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the day of the year for a date as a number between 1 and 366 (leap year).
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $dayOfYear
      • hour

        public static DateExpressions.DateExpression hour​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the hour for a date as a number between 0 and 23.
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $hour
      • isoDayOfWeek

        public static dev.morphia.aggregation.expressions.impls.IsoDates isoDayOfWeek​(dev.morphia.aggregation.expressions.impls.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 dev.morphia.aggregation.expressions.impls.IsoDates isoWeek​(dev.morphia.aggregation.expressions.impls.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 dev.morphia.aggregation.expressions.impls.IsoDates isoWeekYear​(dev.morphia.aggregation.expressions.impls.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
      • milliseconds

        public static DateExpressions.DateExpression milliseconds​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the milliseconds of a date as a number between 0 and 999.
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $millisecond
      • minute

        public static DateExpressions.DateExpression minute​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the minute for a date as a number between 0 and 59.
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $minute
      • month

        public static DateExpressions.DateExpression month​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the month for a date as a number between 1 (January) and 12 (December).
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $month
      • second

        public static DateExpressions.DateExpression second​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the seconds for a date as a number between 0 and 60 (leap seconds).
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $second
      • toDate

        public static DateExpressions.DateExpression toDate​(dev.morphia.aggregation.expressions.impls.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 dev.morphia.aggregation.expressions.impls.Expression tsIncrement​(dev.morphia.aggregation.expressions.impls.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
        MongoDB documentation
        Aggregration Expression: $tsIncrement
        Since server release
        5.1
      • tsSecond

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

        public static DateExpressions.DateExpression week​(dev.morphia.aggregation.expressions.impls.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
      • year

        public static DateExpressions.DateExpression year​(dev.morphia.aggregation.expressions.impls.Expression value)
        Returns the year for a date as a number (e.g. 2014).
        Parameters:
        value - the expression containing the date value
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $year