Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to convert days into human-readable years, months and days in Scala

2 Answers

0 votes
import java.time.{LocalDate, Period}

case class YMD(years: Int, months: Int, days: Int)

def splitDays(totalDays: Int): YMD = {
  val start = LocalDate.of(1970, 1, 1)
  val end   = start.plusDays(totalDays.toLong)

  val p = Period.between(start, end)

  YMD(p.getYears, p.getMonths, p.getDays)
}

val r = splitDays(452)

println(s"${r.years} years, ${r.months} months, ${r.days} days")




/*
run:

1 years, 2 months, 28 days

*/

 



answered Jan 1 by avibootz
0 votes
case class YMD(years: Int, months: Int, days: Int)

def splitDays(total: Int): YMD = {
  val years  = total / 365
  val rem1   = total % 365

  val months = rem1 / 30
  val days   = rem1 % 30

  YMD(years, months, days)
}


val r = splitDays(452)

println(s"${r.years} years, ${r.months} months, ${r.days} days")



/*
run:

1 years, 2 months, 27 days

*/

 



answered Jan 1 by avibootz

Related questions

...