1

I have a date string and a date interval which I want to add it to that date in Scala, but I don't know how I can do this (preferably without using Java Calendar)!

Something like this:

val d1 = new SimpleDateFormat("YYYY-MM-DD").parse("1999-12-01")
val d2 = ??? // 1 Year interval
d1 + d2
kianoosh
  • 610
  • 6
  • 22
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Use `Period` for an interval of 1 year and everything should go smoothly. – Ole V.V. Sep 11 '18 at 13:32
  • Possible duplicate of [Adding Years from Date class](https://stackoverflow.com/questions/11642701/adding-years-from-date-class) – Ole V.V. Sep 11 '18 at 13:33
  • @OleV.V. I was looking for a straight-forward approach and avoid using Calendar! Which Joda seems to be the answer – kianoosh Sep 11 '18 at 13:39
  • I agree that `Calendar` is a poor and overly complicated answer. IMHO the good answer is java.time (not Joda-Time, though it isn’t too bad either). – Ole V.V. Sep 11 '18 at 13:41

4 Answers4

4

java.time

    LocalDate d1 = LocalDate.parse("1999-12-01");
    Period p2 = Period.ofYears(1);

    LocalDate oneYearLater = d1.plus(p2);
    System.out.println(oneYearLater);

Output from this snippet is:

2000-12-01

Sorry that I cannot write Scala code. I trust you to translate from Java.

Provided that you are based on at least Java 8 you don’t need an external library. For Java 6 and 7 use the same code, only add the ThreeTen Backport library to your project and import from the org.threeten.bp package. For Java 5 Joda-Time is the good solution, see the other answer.

Both java.time and the ThreeTen Backport were developed by the same folks that developed Joda-Time and drew heavily on the good (and the few poor) experiences from there. Joda-Time is now in maintenance mode. The Joda-Time home page says: “Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).”

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
2

You are better off using joda dates rather than "vanilla" java:

import org.joda.time.DateTime

val d1 = DateTime.parse("1999-12-01")
val d2 = d1.plusYears(1)
Dima
  • 39,570
  • 6
  • 44
  • 70
1

There is no need for external dependencies. You can use java.time.LocalDate:

import java.time.LocalDate

val date1 = LocalDate.parse("1999-12-01")
val date2 = date1.plusYears(1)
senjin.hajrulahovic
  • 2,961
  • 2
  • 17
  • 32
0

As mentioned, you can use the API provided by Java. However, and in case you need a more Scala-like library, you could use https://github.com/nscala-time/nscala-time which is a wrapper around JodaTime. Some examples:

DateTime.now() + 2.months // returns org.joda.time.DateTime = 2009-06-27T13:25:59.195-07:00

DateTime.nextMonth < DateTime.now() + 2.months // returns Boolean = true

DateTime.now() to DateTime.tomorrow  // return org.joda.time.Interval = > 2009-04-27T13:47:14.840/2009-04-28T13:47:14.840

(DateTime.now() to DateTime.nextSecond).millis // returns Long = 1000

2.hours + 45.minutes + 10.seconds // returns com.github.nscala_time.time.DurationBuilder
// (can be used as a Duration or as a Period)

(2.hours + 45.minutes + 10.seconds).millis // returns Long = 9910000

2.months + 3.days // returns Period

Cheers.

GuilleK
  • 46
  • 4