Scala日期處理

智慧先行者發表於2016-11-30

計算時間間隔 

val d = new java.text.SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new java.util.Date())

    val dateFormat = new java.text.SimpleDateFormat("yyyyMMdd HH:mm:ss")

    // 系統時間
    val d1 = new java.util.Date()
    val nowDate: String = dateFormat.format(d1)

    // 輸入指定時間
    val dd: Date = dateFormat.parse("20161229 14:20:50")

    // 時間差
    val d3 = new java.util.Date()
    val d4 = new java.util.Date()
    val diff = d4.getTime - d3.getTime  // 返回自此Date物件表示的1970年1月1日,00:00:00 GMT以來的毫秒數。
    val diffMinutes = diff / (1000 * 60)  // 時間間隔,單位:分鐘

 

 

產生日期序列

 import java.util.Calendar
    import java.util.Date
    import java.text.SimpleDateFormat
    import scala.collection.mutable.ListBuffer

    // 輸入開始日期和結束日期
    val stringDateBegin: String = "20160101"
    val stringDateEnd: String = "20160209"

    val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyyMMdd")
    val dateBegin: Date = dateFormat.parse(stringDateBegin)
    val dateEnd: Date = dateFormat.parse(stringDateEnd)

    val calendarBegin: Calendar = Calendar.getInstance()
    val calendarEnd: Calendar = Calendar.getInstance()

    calendarBegin.setTime(dateBegin)
    calendarEnd.setTime(dateEnd)

    // 計算日期間隔天數
    val diff = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis()
    val diffDay = (diff / (1000 * 60 * 60 * 24)).toInt
    val calendarList = new ListBuffer[String]()
    for (d <- 0 to diffDay) {
      // 日期轉化成"yyyyMMdd"
      calendarList.append(dateFormat.format(calendarBegin.getTime()))
      calendarBegin.add(Calendar.DAY_OF_MONTH, 1)
    }

    calendarList.mkString(",")

 執行結果

 

// 輸入開始日期和結束日期
val stringDateBegin: String = "20160101"
stringDateBegin: String = 20160101

val stringDateEnd: String = "20160209"
stringDateEnd: String = 20160209

val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyyMMdd")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@ef87e460

val dateBegin: Date = dateFormat.parse(stringDateBegin)
dateBegin: java.util.Date = Fri Jan 01 00:00:00 UTC 2016

val dateEnd: Date = dateFormat.parse(stringDateEnd)
dateEnd: java.util.Date = Tue Feb 09 00:00:00 UTC 2016

val calendarBegin: Calendar = Calendar.getInstance()
calendarBegin: java.util.Calendar = java.util.GregorianCalendar[time=1480484154627,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Etc/Universal",offse
t=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=10,WEEK_OF_YEAR=49,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=335,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=5,HOUR_OF_DAY=5,MINUTE=35,SECOND=54,MILLISECOND=627,ZONE_OFFSET=0,DST_OFFSET=0]

val calendarEnd: Calendar = Calendar.getInstance()
calendarEnd: java.util.Calendar = java.util.GregorianCalendar[time=1480484154845,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Etc/Universal",offset=
0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=10,WEEK_OF_YEAR=49,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=335,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=5,HOUR_OF_DAY=5,MINUTE=35,SECOND=54,MILLISECOND=845,ZONE_OFFSET=0,DST_OFFSET=0]

calendarBegin.setTime(dateBegin)
calendarEnd.setTime(dateEnd)

// 計算日期間隔天數
val diff = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis()
diff: Long = 3369600000

val diffDay = (diff / (1000 * 60 * 60 * 24)).toInt
diffDay: Int = 39

val calendarList = new ListBuffer[String]()
calendarList: scala.collection.mutable.ListBuffer[String] = ListBuffer()

for (d <- 0 to diffDay) {
    // 日期轉化成"yyyyMMdd"
    calendarList.append(dateFormat.format(calendarBegin.getTime()))
    calendarBegin.add(Calendar.DAY_OF_MONTH, 1)
          }

calendarList.mkString(",")
res12: String = 20160101,20160102,20160103,20160104,20160105,20160106,20160107,20160108,20160109,20160110,20160111,20160112,20160113,20160114,20160115,20160116,20160117,20160118,20160119,2016
0120,20160121,20160122,20160123,20160124,20160125,20160126,20160127,20160128,20160129,20160130,20160131,20160201,20160202,20160203,20160204,20160205,20160206,20160207,20160208,20160209

 

相關文章