You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Wenchen Fan (JIRA)" <ji...@apache.org> on 2019/07/15 12:52:00 UTC

[jira] [Resolved] (SPARK-28389) Use Java 8 API to add months to dates

     [ https://issues.apache.org/jira/browse/SPARK-28389?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Wenchen Fan resolved SPARK-28389.
---------------------------------
       Resolution: Fixed
    Fix Version/s: 3.0.0

Issue resolved by pull request 25153
[https://github.com/apache/spark/pull/25153]

> Use Java 8 API to add months to dates
> -------------------------------------
>
>                 Key: SPARK-28389
>                 URL: https://issues.apache.org/jira/browse/SPARK-28389
>             Project: Spark
>          Issue Type: Improvement
>          Components: SQL
>    Affects Versions: 2.4.3
>            Reporter: Maxim Gekk
>            Assignee: Maxim Gekk
>            Priority: Minor
>             Fix For: 3.0.0
>
>
> The dateAddMonths() methods can be simplified by using the plusMonths() method of Java 8 LocalDate:
> {code}
> LocalDate.ofEpochDay(days).plusMonths(months)
> {code}
> This method adds the specified amount to the months field in three steps:
> # Add the input months to the month-of-year field
> # Check if the resulting date would be invalid
> # Adjust the day-of-month to the last valid day if necessary
> For example, 2019-01-31 plus one month would result in the invalid date 2019-02-31. Instead of returning an invalid result, the last valid day of the month, 2019-02-28, is selected instead.
> The valid result is returned as is. For example, adding 1 month to 2019-02-28 is 2019-03-28.
> Mysql and PostgreSQL have the same behavior:
> {code}
> mysql> SELECT DATE_ADD("2019-01-31", INTERVAL 1 MONTH);
> +------------------------------------------+
> | DATE_ADD("2019-01-31", INTERVAL 1 MONTH) |
> +------------------------------------------+
> | 2019-02-28                               |
> +------------------------------------------+
> 1 row in set (0.00 sec)
> mysql> SELECT DATE_ADD("2019-02-28", INTERVAL 1 MONTH);
> +------------------------------------------+
> | DATE_ADD("2019-02-28", INTERVAL 1 MONTH) |
> +------------------------------------------+
> | 2019-03-28                               |
> +------------------------------------------+
> 1 row in set (0.00 sec)
> {code}
> {code}
> maxim=# SELECT DATE_ADD("2019-01-31", INTERVAL 1 MONTH);
> ERROR:  syntax error at or near "1"
> LINE 1: SELECT DATE_ADD("2019-01-31", INTERVAL 1 MONTH);
>                                                ^
> maxim=# SELECT DATE '2019-01-31' + INTERVAL '1 month';
>       ?column?       
> ---------------------
>  2019-02-28 00:00:00
> (1 row)
> maxim=# SELECT DATE '2019-02-28' + INTERVAL '1 month';
>       ?column?       
> ---------------------
>  2019-03-28 00:00:00
> (1 row)
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org