How to implement date manipulation in Scala
This article will explain in detail how to achieve date operation in Scala. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
It is necessary to operate on the date when counting.
Get the date import java.text. {DecimalFormat, DecimalFormatSymbols, SimpleDateFormat} import java.util.Calendardef getPreviousdate (date: String, pre_cnt:Int): String = {var dateFormat:SimpleDateFormat = new SimpleDateFormat ("yyyyMMdd") var cal:Calendar = Calendar.getInstance () var dt:Date = dateFormat.parse (date) cal.setTime (dt) Cal.add (Calendar.DATE,-pre_cnt) var pre_date = dateFormat.format (cal.getTime ()) return pre_date} get the number of days between two dates def getDateDiff (date1: String) Date2: String: Int = {var dateFormat:SimpleDateFormat = new SimpleDateFormat ("yyyyMMdd") var cal_1:Calendar = Calendar.getInstance () var cal_2:Calendar = Calendar.getInstance () var dt_1:Date = dateFormat.parse (date1) var dt_2:Date = dateFormat.parse (date2) cal_1.setTime (dt_1) var time_1 = cal_1.getTimeInMillis () cal_2. SetTime (dt_2) var time_2 = cal_2.getTimeInMillis () var diff_days= (time_1-time_2) / (1000-3600-24) return diff_days.toInt.abs} this is the end of the article on "how Scala implements date manipulation" Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.