scala 时间,时间格式转换
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
scala 时间,时间格式转换的更多相关文章
- Java练习 SDUT-2246_时间日期格式转换
时间日期格式转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于日期的常用格式,在中国常采用格式的是"年 ...
- Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html
Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...
- 使用new data计算时间以及格式转换
1.时间计算,往后加30(默认一个月的时间),sxTime表示的是在当前时间往后加几天的之后一个月 function maxDate1(){ var nowDate = new Date(); max ...
- Java时间日期格式转换
1.这个是系统自动默认的时间格式,或者说是美式格式: Long time = System.currentTimeMillis(); Date date = new Da ...
- [js] js和C# 时间日期格式转换
下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...
- js和C# 时间日期格式转换
下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...
- mysql 根据时间查询 格式转换竟然要注意大小写,天坑
时间需要转换格式在查询 查询2018年12月24日以后的记录 SELECT id FROM t_client_info WHERE DATE_FORMAT(visit_datetime,'%Y-%m- ...
- JAVA中获取当前系统时间及格式转换
JAVA中获取当前系统时间 一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...
- poj 3751 时间日期格式转换
题目链接:http://poj.org/problem?id=3751 题目大意:按照要求的格式将输入的时间日期进行转化. #include <iostream> #include < ...
- Java时间日期格式转换Date转String和String转Date
Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...
随机推荐
- 第六章节 BJROBOT 动态导航壁障
导航前说明:一定要确保你小车在构建好地图的基础上进行! 1.把小车平放在你想要构建地图区域的地板上,打开资料里的虚拟机,打开一个终端, ssh 过去主控端启动 roslaunch znjrobot b ...
- 这些JS技巧,看看你是否都会用?
问题1:以下代码在浏览器控制台上会打印什么? var a = 10; function foo() { console.log(a); // ?? var a = 20; } foo(); 问题2:如 ...
- vuetify 属性集合
1.v-form 组件具有 three 功能,可以通过在该组件上设置 ref 来访问它们. ref 允许我们访问组件上的内部方法,例如 <v-form ref="form"& ...
- SQL Server 数据库还原进度查看
SQL Server 数据库还原进度查看 关键字:数据库,还原,进度,查看 文档说明: 本文档受某实际需求启发,某约500G大小数据库还原,由于对应服务器性能较差(内存仅4G且可用内存仅2.8G),数 ...
- [Leetcode刷题]——链表
一.找出两个链表的交点 160.相交链表(easy)2021-01-05 编写一个程序,找到两个单链表相交的起始节点 如下面的两个链表,在c1 处相交: public class Soluti ...
- TCP/IP五层模型概述
• 为什么要分层? ○ 协议太多,将众多协议分层解决,能提高效率,复杂问题简单化,更容易发现问题,并针对性解决问题.• OSI七层模型 ○ 同层使用相同的协议,下层为上层提供服务. ...
- If you see someone without smile
If you see someone without smile, give them one of yours. 难怪我每次和不认识的人说话都放肆大笑.
- 二 prometheus 监控 Redis
Prometheus 监控Redis需要用到redis_exporter客户端, Prometheus -> redis_exporter这个模式, 类似监控Mysql 一个思路. 1 ) 设置 ...
- Docker学习笔记之使用Docker数据卷
Docker数据卷将数据存储到主机而非容器,多个容器需要共享数据时,常常使用数据卷. 1. 为容器设置数据卷(不指定主机目录) 2. 容器与主机之间.容器与容器之间共享数据卷(指定主机目录) 3. 使 ...
- wpf 通过为DataGrid所绑定的数据源类型的属性设置Attribute改变DataGrid自动生成列的顺序
环境Win10 VS2019 .Net Framework4.8 在wpf中,如果为一个DataGrid绑定到一个数据源,默认情况下DataGrid会为数据源类型的每个属性生成一个列(Column)对 ...