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

scala 时间,时间格式转换的更多相关文章

  1. Java练习 SDUT-2246_时间日期格式转换

    时间日期格式转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于日期的常用格式,在中国常采用格式的是"年 ...

  2. Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

  3. 使用new data计算时间以及格式转换

    1.时间计算,往后加30(默认一个月的时间),sxTime表示的是在当前时间往后加几天的之后一个月 function maxDate1(){ var nowDate = new Date(); max ...

  4. Java时间日期格式转换

    1.这个是系统自动默认的时间格式,或者说是美式格式: Long time = System.currentTimeMillis();                Date date = new Da ...

  5. [js] js和C# 时间日期格式转换

    下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...

  6. js和C# 时间日期格式转换

    下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...

  7. mysql 根据时间查询 格式转换竟然要注意大小写,天坑

    时间需要转换格式在查询 查询2018年12月24日以后的记录 SELECT id FROM t_client_info WHERE DATE_FORMAT(visit_datetime,'%Y-%m- ...

  8. JAVA中获取当前系统时间及格式转换

    JAVA中获取当前系统时间   一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...

  9. poj 3751 时间日期格式转换

    题目链接:http://poj.org/problem?id=3751 题目大意:按照要求的格式将输入的时间日期进行转化. #include <iostream> #include < ...

  10. Java时间日期格式转换Date转String和String转Date

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

随机推荐

  1. JavaDailyReports10_04

    修改后的出题系统 1.添加用户自定义是否出现乘除法,自由选择符号和个数,并且可以自定义操作数的取值范围. 1 /* 2 * 2.可定制(数量/打印方式):输入大的数量值,测试一下系统是否崩溃,反向查找 ...

  2. 对CAS中atomicInteger实现的思考

    p.p1 { margin: 0; font: 11px Monaco } span.s1 { color: rgba(147, 26, 104, 1) } span.s2 { color: rgba ...

  3. LVS之2---基于LVS负载均衡集群架构

    LVS之2---基于LVS负载均衡集群架构实现 目录 LVS之2---基于LVS负载均衡集群架构实现 ipvsadm software package Options 常用命令 保存及重载规则 内存映 ...

  4. linux的 复制 删除 解压 压缩 打包

    liunx 删除 删除文件夹实例: rm -rf /var/log/httpd/access 将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 2 删除文件使用实例: r ...

  5. 你知道 react-color 的实现原理吗

    一.前言 ReactColor 是一个优秀的 React 颜色选择器组件,官方给了多种布局供开发者选择. 笔者常用的主题为 Sketch,这种主题涵盖了颜色面板.推荐色块.RGB颜色输入等功能,比较完 ...

  6. 为什么 TCP 连接的建立需要三次握手

    TCP 的通讯双方需要发送 3 个包(即:三次握手)才能建立连接,本文将通过 3 副图来解释为什么需要 3 次握手才能建立连接. TCP 连接的建立过程本质是通信双方确认自己和对方都具有通信能力的过程 ...

  7. Spring Security OAuth2.0认证授权五:用户信息扩展到jwt

    历史文章 Spring Security OAuth2.0认证授权一:框架搭建和认证测试 Spring Security OAuth2.0认证授权二:搭建资源服务 Spring Security OA ...

  8. 【Java基础】反射

    反射 反射的概述 反射(Reflection)是被视为动态语言的关键,反射机制允许程序在执行期借助 Reflection API 取得任何类的内部信息,并能直接操作任意对象的内部属性和方法. 加载完类 ...

  9. 在Windows中安装MongoDB--图文并茂

    在Windows环境下安装MongoDB的方法 (1)下载MongoDB Windows版: 进入MongoDB官网 (2)设置数据文件和日志文件的存放目录: 打开刚刚安装MongoDB的目录咋bin ...

  10. C语言指针-从底层原理到花式技巧,用图文和代码帮你讲解透彻

    这是道哥的第014篇原创 目录 一.前言 二.变量与指针的本质 1. 内存地址 2. 32位与64位系统 3. 变量 4. 指针变量 5. 操作指针变量 5.1 指针变量自身的值 5.2 获取指针变量 ...