LoginSignup
6
5

More than 5 years have passed since last update.

ScalaでTimeの0埋め方法

Last updated at Posted at 2014-04-22

仕事でこんなコードを書いたので、Scalaっぽいかもと思ってメモ

ある時間を表す文字列"HH:mm"というのが場合によっては"H:mm"となる場合があるので format を使って0埋めします。

  /**
   * HH:mm形式で返す(0:00の場合は前0を付ける)
   * @param time
   * @return
   */
  private def formatTime(time:String):String = {
    time.split(':').map{
      t => "%02d".format(t.toInt)
    }.mkString(":")
  }

':'でsplitして配列にして、format関数で前0つけた文字列を返して':'で繋ぐ。もっと簡単に書けるかもしれないけど今の自分ではちょっとはScalaっぽく書けたつもり。もっとScala勉強しよう。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5