0
package com.javacodegeeks.groovy.date
class GroovyDateArithmetic {
static main(args) {
def date = new Date().parse("dd.MM.yyy", '18.05.1988')
def datePlus = date.clone()
def dateMinus = date.clone()
datePlus = datePlus + 5
println datePlus 

The above code will print Mon May 23 00:00:00 EST 1988. How can I format the final result to produce Mon May 23 1988.

ComplexData
  • 1,091
  • 4
  • 19
  • 36
  • This code will print `Mon May 23 00:00:00 EEST 1988`, and you expect final result should be `Mon May 23 00:00:00 EEST 1988` ? – Corey Jan 25 '19 at 01:16
  • @KuanlinChen Just edited the question. – ComplexData Jan 25 '19 at 01:27
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 25 '19 at 03:40
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 25 '19 at 09:04
  • The **Groovy** v2.5+ answer which takes advantage of Java 8's Date/Time API would be `date = LocalDate.parse('18.05.1988', 'dd.MM.yyyy')` `println((date + 5).format('EEE MMM d yyyy'))`. Writing here because while I was about to post this as an answer, the question was closed as duplicate for quite debatable reasons (the "duplicated" question is about Java) :( Writing only now because at that time I couldn't comment on the post :) For reference, see [DZone article by Joe Wolf, the API's author](https://dzone.com/articles/groovy-additions-to-the-java-8-datetime-api) Happy groovying :) – Dmitry Khamitov Jan 28 '19 at 10:41

2 Answers2

5

Use modern java.time classes.

In Java syntax:

String input = "18.05.1988" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu" ) ;
LocalDate ld = LocalDate.parse( input , f ) ;

To add or subtract spans of time, call plus… or minus… methods.

LocalDate later = ld.plusDays( 5 ) ;

To generate text representing that date value in standard ISO 8601 format, call toString.

String output = later.toString():

2018-05-23


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Try this way.

new Date().parse("dd.MM.yyy", '18.05.1988').format('EEE MMM d yyyy')
Corey
  • 1,217
  • 3
  • 22
  • 39
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 25 '19 at 03:44