Scala date 日期處理

來源:互聯網
上載者:User
package com.juanpi.bi.sc_utilsimport java.text.SimpleDateFormatimport java.util.{Calendar, Date}import com.juanpi.bi.streaming.DateHourimport org.joda.time.DateTime/** * Created by juanpi on 2015/8/18. */object DateUtils {  def dateHour(milliseconds: Long):DateHour = {    val dateTime = new DateTime(milliseconds)    DateHour(dateTime.toString("yyyy-MM-dd"), dateTime.toString("H"))  }  /**    * 返回日期加上gu_id最後一位,作為log檔案的儲存目錄    *    * @param milliseconds    * @param gu_id    * @return    */  def dateGuidPartitions(milliseconds: Long, gu_id: String): String = {    val dateTime = new DateTime(milliseconds)    val date = dateTime.toString("yyyy-MM-dd")    val gu_hex = (gu_id.last).toLower    s"date=${date}/gu_hash=${gu_hex}"  }  /**    * 返回 yyyy-MM-dd 格式的日期    *    * @param milliseconds    * @return    */  def dateStr(milliseconds: Long): String = {    val dateTime = new DateTime(milliseconds)    dateTime.toString("yyyy-MM-dd")  }  /**    * 接受一個時間戳記的參數,返回日期和小時    *    * @param milliseconds    * @return    */  def dateHourStr(milliseconds: Long): (String, String) = {    val dateTime = new DateTime(milliseconds)    (dateTime.toString("yyyy-MM-dd"), dateTime.toString("H"))  }  /**    *    * @return 返回當前的日期串    */  def getDateNow():String={    val now:Date = new Date()    val dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")    val dt = dateFormat.format( now )    dt  }  /**    * 指定日期和間隔天數,返回指定日期前N天的日期 date - N days    * @param dt    * @param interval    * @return    */  def getDaysBefore(dt: Date, interval: Int):String = {    val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")    val cal: Calendar = Calendar.getInstance()    cal.setTime(dt);    cal.add(Calendar.DATE, - interval)    val yesterday = dateFormat.format(cal.getTime())    yesterday  }  /**    * 指定日期和間隔天數,返回指定日期前N天的日期: date + N days    * @param dt    * @param interval    * @return    */  def getDaysLater(dt: Date, interval: Int):String = {    val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")    val cal: Calendar = Calendar.getInstance()    cal.setTime(dt);    cal.add(Calendar.DATE, + interval)    val yesterday = dateFormat.format(cal.getTime())    yesterday  }  /**    * 2017-01-17  A Week Ago is 2017-01-07    * @return    */  def getWeekAgoDateStr(): String = {    val dt: Date = new Date()    val dtStr = getDaysBefore(dt, 7)    dtStr  }  /**    *  2017-01-17 A Week Later is  2017-01-21    * @return    */  def getWeekLaterDateStr(): String = {    val dt: Date = new Date()    val dtStr = getDaysLater(dt, 7)    dtStr  }  def getYesterday(): String = {    // Calendar.DATE    val dt: Date = new Date()    val yesterday = getDaysBefore(dt, 1)    return yesterday  }  def main(args: Array[String]) {    println(getWeekAgoDateStr())    println(getWeekLaterDateStr())  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.