如何使用play框架和scala获取系统时间?

5 scala playframework

如何使用play框架和scala获取系统时间?

使用playframework2.0和scala获取系统时间到web程序是有什么不同的方式...

4le*_*x1v 10

如果要将系统时间粘贴到Twirl html模板中,只需@在System之前添加符号即可

@System.currentTimeMillis
Run Code Online (Sandbox Code Playgroud)

要么

@System.nanoTime
Run Code Online (Sandbox Code Playgroud)

更新

如果你需要一些当前时间的格式,那么在你的Twirl模板中添加一些帮助方法,如下所示:

@currentTime = @{
  import java.util.Calendar
  import java.text.SimpleDateFormat

  val today = Calendar.getInstance.getTime
  val curTimeFormat = new SimpleDateFormat("HH:mm")

  curTimeFormat.format(today)
}
Run Code Online (Sandbox Code Playgroud)

然后在模板中简单地调用此方法:

<div>@currentTime</div>
Run Code Online (Sandbox Code Playgroud)

你可以在这里阅读Twirld模板引擎:Scala模板 和格式规则:SimpleDateFormat