這篇文章將為大家詳細(xì)講解有關(guān)Scala如何實(shí)現(xiàn)日期操作,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
在統(tǒng)計(jì)時(shí)需要對(duì)日期進(jìn)行操作
獲取某一天前 N 天日期importjava.text.{DecimalFormat,DecimalFormatSymbols,SimpleDateFormat}importjava.util.CalendardefgetPreviousdate(date:String,pre_cnt:Int):String={vardateFormat:SimpleDateFormat=newSimpleDateFormat("yyyyMMdd")varcal:Calendar=Calendar.getInstance()vardt:Date=dateFormat.parse(date)cal.setTime(dt);cal.add(Calendar.DATE,-pre_cnt)varpre_date=dateFormat.format(cal.getTime())returnpre_date}獲取兩個(gè)日期的間隔天數(shù)defgetDateDiff(date1:String,date2:String):Int={vardateFormat:SimpleDateFormat=newSimpleDateFormat("yyyyMMdd")varcal_1:Calendar=Calendar.getInstance()varcal_2:Calendar=Calendar.getInstance()vardt_1:Date=dateFormat.parse(date1)vardt_2:Date=dateFormat.parse(date2)cal_1.setTime(dt_1)vartime_1=cal_1.getTimeInMillis()cal_2.setTime(dt_2)vartime_2=cal_2.getTimeInMillis()vardiff_days=(time_1-time_2)/(1000*3600*24)returndiff_days.toInt.abs}
關(guān)于“Scala如何實(shí)現(xiàn)日期操作”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。