zoukankan      html  css  js  c++  java
  • scala 时间格式转换(String、Long、Date)

    1)scala 时间格式转换(String、Long、Date)

    1、时间字符类型转Date类型

    [java] view plain copy
    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) 
    //output:Mon Dec 18 00:01:56 CST 2017

    2、Long类型转字符类型

    [java] view plain copy
    val time:Long= 1513839667//
    val newtime :String = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time*1000) 
    println(newtime) 
    //output:2017-12-21 15:01:07 

    3、时间字符类型转Long类型

    [java] view plain copy
    val time = "2017-12-18 00:01:56" 
    val newtime :Long= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime 
    println(newtime) 
    //output:1513526516000

    2)scala 时间和时间戳互转

    时间转换为时间戳:

    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
    }
    }

    时间戳转化为时间:

    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
    }
    }
  • 相关阅读:
    OAccflow集成sql
    集成备注
    CCflow与基础框架组织机构整合
    Jeecg_Jflow整合记录
    Problem opening .cshtml files
    The document cannot be opened. It has been renamed, deleted or moved.
    js中!和!!的区别及用法
    hdu 4757 Tree(可持久化字典树)
    Tomcat 学习进阶历程之Tomcat架构与核心类分析
    BZOJ 3000(Big Number-Stirling公式求n!近似值)
  • 原文地址:https://www.cnblogs.com/mlfh1234/p/9210046.html
Copyright © 2011-2022 走看看