java 时间string long_java时间Long和String相互转化

package com.banksteel.openerp.commons.utils;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

* @description:时间转换

* @projectName:openerp-commons

* @className:TimeTransf.java

* @author:

* @createTime:2016年7月28日 下午6:06:53

* @version 1.0

*/

public class TimeTransf

{

/**

* @description:字符串型时间转换成长整型时间

* @param time 字符串型时间

* @param format 时间表现形式

* @return 长整型时间

* @throws ParseException

* @author:

* @createTime:2016年7月28日 下午6:07:33

*/

public static Long StringToLong(String time, String format) throws ParseException

{

if(time!=null){

return new SimpleDateFormat(format).parse(time).getTime();

}

return null;

}

/**

* @description:长整型时间转换成字符串型时间

* @param time 长整型时间

* @param format 时间表现形式

* @return 字符串型时间

* @author:

* @createTime:2016年7月28日 下午6:10:50

*/

public static String LongToString(Long time, String format)

{

if(time!=null){

return new SimpleDateFormat(format).format(new Date(time));

}

return null;

}

public static void main(String[] args) throws ParseException

{

Long currentTimeMillis = System.currentTimeMillis();

String str = TimeTransf.LongToString(currentTimeMillis, "yyyy-MM-dd HH:mm:ss.SSS");

System.out.println(str);

Long t = TimeTransf.StringToLong(str, "yyyy-MM-dd HH:mm:ss.SSS");

System.out.println(t);

String str1 = TimeTransf.LongToString(currentTimeMillis, "yyyy-MM-dd HH:mm:ss");

System.out.println(str1);

Long t1 = TimeTransf.StringToLong(str1, "yyyy-MM-dd HH:mm:ss");

System.out.println(t1);

String str2 = TimeTransf.LongToString(currentTimeMillis, "yyyy-MM-dd");

System.out.println(str2);

Long t2 = TimeTransf.StringToLong(str2, "yyyy-MM-dd");

System.out.println(t2);

}

}

2

df3c5c0e770b41552ac242b62a803781.png

原文:https://www.cnblogs.com/guilf/p/9298254.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值