scala 时间,时间格式转换
- 1.scala 时间格式转换(String、Long、Date)
- 2.scala 时间和时间戳互转
- 3.获取今天日期,昨天日期,本周时间,本月时间,时间戳转换日期时间比较计算时间差
- 4.计算时间差
- 5.Scala日期处理
import java.time.LocalDate object fileTest { def main(args: Array[String]) { var nowdate = LocalDate.now() println("LocalDate.now()-->现在的时间是"+LocalDate.now()) println("nowdate.plusDays-->明天是-->"+nowdate.plusDays(1)) println("nowdate.minusDays-->昨天是-->"+nowdate.minusDays(1)) println("nowdate.plusMonths-->今天加一个月-->"+nowdate.plusMonths(1)) println("nowdate.minusMonths-->今天减一个月-->"+nowdate.minusMonths(1)) println("nowdate.getDayOfYear-->今天是今年的第"+nowdate.getDayOfYear+"天") println("nowdate.getDayOfMonth->这个月有"+nowdate.getDayOfMonth+"天") println("nowdate.getDayOfWeek-->今天星期"+nowdate.getDayOfWeek) println("nowdate.getMonth-->这个月是"+nowdate.getMonth) } }1.scala 时间格式转换(String、Long、Date)
1.1时间字符类型转Date类型
import java.text.SimpleDateFormat val time = "2017-12-18 00:01:56" val newtime :Date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time) println(newtime)
1.2Long类型转字符类型
val time:Long= 1513839667//秒 val newtime :String = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time*1000) println(newtime)
1.3时间字符类型转Long类型
val time = "2017-12-18 00:01:56" val newtime :Long= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime println(newtime)2.scala 时间和时间戳互转
2.1时间转换为时间戳
import java.text.SimpleDateFormat object test { def main(args: Array[String]): Unit = { val tm = "2017-08-01 16:44:32" val a = tranTimeToLong(tm) println(a) } def tranTimeToLong(tm:String) :Long={ val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") val dt = fm.parse(tm) val aa = fm.format(dt) val tim: Long = dt.getTime() tim } }
2.2时间戳转化为时间
import java.text.SimpleDateFormat import java.util.Date object test { def main(args: Array[String]): Unit = { val tm = "1502036122000" val a = tranTimeToString(tm) println(a) } def tranTimeToString(tm:String) :String={ val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") val tim = fm.format(new Date(tm.toLong)) tim } }
def timeFormat(time:String):String={ var sdf:SimpleDateFormat = new SimpleDateFormat("HH:mm:ss") var date:String = sdf.format(new Date((time.toLong*1000l))) date }
2.3将时间戳转化成日期
时间戳是秒数,需要乘以1000l转化成毫秒
def DateFormat(time:String):String={ var sdf:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd") var date:String = sdf.format(new Date((time.toLong*1000l))) date }3.获取今天日期,昨天日期,本周时间,本月时间,时间戳转换日期时间比较计算时间差
3.1获取今天日期
def getNowDate():String={ var now:Date = new Date() var dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd") var hehe = dateFormat.format( now ) hehe }
3.2获取昨天的日期
def getYesterday():String={ var dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd") var cal:Calendar=Calendar.getInstance() cal.add(Calendar.DATE,-1) var yesterday=dateFormat.format(cal.getTime()) yesterday
3.3获取本周开始日期
def getNowWeekStart():String={ var period:String="" var cal:Calendar =Calendar.getInstance(); var df:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY) //获取本周一的日期 period=df.format(cal.getTime()) period }
3.4获取本周末的时间
def getNowWeekEnd():String={ var period:String="" var cal:Calendar =Calendar.getInstance(); var df:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//这种输出的是上个星期周日的日期,因为老外把周日当成第一天 cal.add(Calendar.WEEK_OF_YEAR, 1)// 增加一个星期,才是我们中国人的本周日的日期 period=df.format(cal.getTime()) period }
3.5本月的第一天
def getNowMonthStart():String={ var period:String="" var cal:Calendar =Calendar.getInstance(); var df:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); cal.set(Calendar.DATE, 1) period=df.format(cal.getTime())//本月第一天 period }
3.6本月的最后一天
def getNowMonthEnd():String={ var period:String="" var cal:Calendar =Calendar.getInstance(); var df:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); cal.set(Calendar.DATE, 1) cal.roll(Calendar.DATE,-1) period=df.format(cal.getTime())//本月最后一天 period }4.计算时间差
//核心工作时间,迟到早退等的的处理 def getCoreTime(start_time:String,end_Time:String)={ var df:SimpleDateFormat=new SimpleDateFormat("HH:mm:ss") var begin:Date=df.parse(start_time) var end:Date = df.parse(end_Time) var between:Long=(end.getTime()-begin.getTime())/1000//转化成秒 var hour:Float=between.toFloat/3600 var decf:DecimalFormat=new DecimalFormat("#.00") decf.format(hour)//格式化 }5.Scala日期处理
5.1计算时间间隔
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) // 时间间隔,单位:分钟
5.2产生日期序列
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