首页 > 其他分享 >将时间转换为时间戳

将时间转换为时间戳

时间:2022-08-23 19:39:04浏览次数:37  
标签:转换 String res return Date 时间 date new

    /**
     * 将时间转换为时间戳
     *
     * @param s 2019-03-01 18:00:00
     * @return res
     */
    public static String dateToStamp(String s) {
        String res = null;
        Date date;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
        } catch (ParseException | NullPointerException e) {
            e.printStackTrace();
        }
        return res;
    }


    /**
     * 将时间戳转换为时间
     *
     * @param s 传入时间戳 1551434400000
     * @return 2019-03-01 18:00:00
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     *
     * @param s 通过时间戳获取到时间的 1551434400000
     * @return 年月日 2019-3-1
     */
    public static String stampToDate1(String s) {
        String res;
        long lt = new Long(s);
        Date date = new Date(lt);
        res = DateFormat.getDateInstance().format(date);
        return res;
    }

标签:转换,String,res,return,Date,时间,date,new
From: https://www.cnblogs.com/liuzonglin/p/16617485.html

相关文章

  • Delphi 内存流与字符串之间互相转换
    Delphi中的内存流还算比较好用的,有时候需要与字符串互转,网络上找到几个函数保存备用!//字符串保存到文件中,返回成功与否functionStringToFile(mString:string;mFileNam......
  • 文件IO-文件时间
    时间字段区别函数更改时间作用......
  • 把数字转换RMB形式
    方法1:varstr='12345679'letstrNew=str.replace(/\B(?=(?:\d{3})+\b)/g,',')//匹配单词边界替换为逗号方法2://定义一个反转函数反转函数......
  • python基础——数据转换与运算符
    数据转换转换数据类型的作用input()接收用户输入的数据都是字符串类型,想得到整型该如何操作?转换数据类型即可,即将字符串类型转换成整型转换数据类型的函数函数说......
  • 基础之 - python数字转换为字符串
     问题:当需要将数字和字符串相加时,会报如下  解决:#1、数字转字符串a=12b='13'#1.1字符串格式化运算符#c='%d'%(a)+b#1.2使用str()函数#......
  • Delphi 十六进制,十进制,字符串直接的类型转换
    //内存数据(字符串或者内存指针)转化为十六进制字符串//'111'==>>'31313131'//E58D40004572726F==>>'E58D40004572726F'functionStrToHex(Value:PAnsiChar;Le......
  • 有时间, 看看这个问题
    Failedtoexecutegoalorg.springframework.boot:spring-boot-maven-plugin:2.7.2:repackage(repackage)onprojectcommon:Executionrepackageofgoalorg.springf......
  • C++强制类型转换
    强制类型转换旧风格的强制转型(typename)value来自C语言。typename(value)纯粹的C++格式使用C++的类型转换符static_caststatic_cast<新类型>(表达式)......
  • 前端格式化拼接时间字符串
    letdateStr=this.dateFormat();this.formData1.mesOrderNum="SD"+dateStr;//时间格式化dateFormat(){vardate=newDate();varyear=date.getFullYear()......
  • Java-时间日期类
    2.时间日期类2.1Date类(应用)Date类概述​ Date代表了一个特定的时间,精确到毫秒Date类构造方法方法名说明publicDate()分配一个Date对象,并初始化,以便......