scala对时间(date)字符串转化为时间戳timestamp的实现
来源:互联网 发布:访客网络怎么开启 编辑:程序博客网 时间:2024/04/29 19:36
在scala编程中,想实现从字符串转化为timestamp的过程中,查找相关资料,才发现资料还是不多。因此在这里将实现过程记录一下。
(注:这种应用情况非常广泛,如从文件中解析某个时间字符串,转化为时间戳再存到HIVE中)
首先写一个转换的函数。
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.Date
/*
* change date string to timestamp value
*/
def getTimestamp(x:String) :java.sql.Timestamp = {
// "20151021235349"
val format = new SimpleDateFormat("yyyyMMddHHmmss")
var ts = new Timestamp(System.currentTimeMillis());
try {
if (x == "")
return null
else {
val d = format.parse(x);
val t = new Timestamp(d.getTime());
return t
}
} catch {
case e: Exception => println("cdr parse timestamp wrong")
}
return null
}
然后进行测试:
//## test .only gettime can return timestamp value
var dates = getTimestamp("20151021235349")
System.out.println(dates.getTime())
在输出结果的时候,还是必须转化为gettime函数才能获得长的时间戳字符串。。
- scala对时间(date)字符串转化为时间戳timestamp的实现
- 时间戳字符串转化为date格式
- 时间格式字符串转化为date和时间戳
- 将英文时间字符串转化为Date
- 时间字符串和date类型转化为时间字符串比较
- javascript转换日期字符串---转化为Date对象---再转化为时间戳
- 3.利用SimpleDateFormat()把字符串格式的时间转化为Date类型的时间
- JS 时间格式化 对Date的扩展,将 Date 转化为指定格式的String
- 将 string形式的时间转化为 timestamp
- 时间戳和日期时间Timestamp的转化
- 时间戳转化为日期TimeStamp转NSDate转NSString
- java中以毫秒表示的时间字符串转化为Date对象
- 特殊的字符串转化为时间
- C# 读取 timestamp 时间戳 值为byte[] 类型时,需要转换成 16进制的字符串 和 数据库中的时间戳值进行比对
- 1.把字符串转化为时间戳,再将时间戳转化为Date对象 /** *@parame time = 2016-12-12 19:41:59 * 或者2016
- js 字符串转化为时间
- Java中将时间戳转化为Date类型
- javascript将时间戳转化为Date格式
- Linux tcpdump命令详解
- google可用host3月2日
- Iptables之FORWARD转发链
- 内存优化学习
- 74.Which statements are true regarding single row functions? (Choose all that apply.)
- scala对时间(date)字符串转化为时间戳timestamp的实现
- Maven依赖排除 禁止依赖传递 取消依赖的方法
- Find the Kth number c++ version
- vim字符串查找
- java-String类深入剖析和 intern()方法解析
- C++中在容器Vector中使用结构体Struct
- ArrayUtils学习
- CSS绘制各种形状
- 二叉树的先序,中序,后序遍历实现