简体   繁体   中英

Printing Future Date in Scala

I'm trying to get the future date in scala. Here is my code

 val today = java.util.Calendar.getInstance().getTime()


 val future = Calendar.getInstance()
  future.add(Calendar.DAY_OF_YEAR, 7)

  val futureDate = future.getTime()

  val yearFormat = new SimpleDateFormat("YYYY")
  val monthFormat = new SimpleDateFormat("MM")
  val dateFormat = new SimpleDateFormat("DD")

  val currentYear = yearFormat.format(today)  
  val currentMonth = monthFormat.format(today)
  val currentDate = dateFormat.format(today)

  val futureYear = yearFormat.format(futureDate)  
  val futureMonth = monthFormat.format(futureDate)
  val futureDay = dateFormat.format(futureDate)


  println("Current Year :"+currentYear)
  println("Current Month :"+currentMonth)
  println("Current Date :"+currentDate)
  println("Future Year :"+futureYear)
  println("Future Month :"+futureMonth)
  println("Future Date :"+futureDay)

Code is simple. I want to add 7 days from today's date and print the date. When I run this. It prints the future date in correctly

Current Year :2015
Current Month :01
Current Date :30
Future Year :2015
Future Month :02
Future Date :37

Please correct me what am I missing. I'm new to Scala

You should use dd instead of DD :

  val yearFormat = new SimpleDateFormat("yyyy")
  val monthFormat = new SimpleDateFormat("MM")
  val dateFormat = new SimpleDateFormat("dd")

The D corresponds to Day in year . You can for future ref. here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM