Scala date 日期处理
2017-01-14 15:30
567 查看
package com.juanpi.bi.sc_utils import java.text.SimpleDateFormat import java.util.{Calendar, Date} import com.juanpi.bi.streaming.DateHour import 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()) } }
相关文章推荐
- [转载]日期处理datetime和date之间的相互转换
- 使用SimpleDateFormat对日期进行处理
- delphi时间日期处理DateUtils时间单元
- Java API的Date, Calendar日期处理相关类分析
- sql SET DATEFORMAT对日期处理的影响
- 日期处理datetime和date之间的相互转换
- java 日期处理工具类 DateUtil 2
- js Date 对象用于处理日期和时间
- Oracle 中的 TO_DATE 和 TO_CHAR 函数 日期处理
- java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类
- JAVA处理日期(Date)时间(Time)以及相关类的介绍
- SET DATEFORMAT对日期处理的影响
- Date对日期的处理
- Java API的Date, Calendar日期处理相关类分析
- java Date 时间 日期 处理
- 日期处理datetime和date之间的相互转换
- Java API的Date, Calendar日期处理相关类分析
- SQL Server日期处理datetime和date之间的相互转换
- java日期处理一(Calendar,Date)
- Hibernate中日期(Date)类型 与oracle中日期的映射处理