我正在尝试在scala中获取未来的日期。这是我的代码
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)
代码很简单。我想从今天的日期开始添加7天并打印日期。当我跑这个。它正确打印未来日期
Current Year :2015
Current Month :01
Current Date :30
Future Year :2015
Future Month :02
Future Date :37
请纠正我的错过。我是Scala的新手
答案 0 :(得分:1)
您应该使用dd
代替DD
:
val yearFormat = new SimpleDateFormat("yyyy")
val monthFormat = new SimpleDateFormat("MM")
val dateFormat = new SimpleDateFormat("dd")
D
对应Day in year
。你可以将来参考。 here。