java编程之怎样把Long转换成Date的日期格式

在进行软件开发的过程中经常会碰到需要对时间格式进行转换的场景。比如对于数据库中的时间保存成long类型的更加方便查找,而对于前端展示来说,肯定是要年月日这种格式的日期。那么怎么把Long类型转换为日期格式或者String呢?


Long类型的时间转换为date,可以通过SimpleDateFormat对象对格式进行定义,然后创建一个Date类型的对象封装时间,再通过SimpleDateFormat对象的format(date)方法就可以获取指定的日期格式了。


有了上面的介绍,看看我是怎么封装一个简单的Long转换为Date的函数:

/**

* 把毫秒转化成日期

* @param dateFormat(日期格式,例如:MM/ dd/yyyy HH:mm:ss)

* @param millSec(毫秒数)

* @return

*/

private String transferLongToDate(String dateFormat,Long millSec){

SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

Date date= new Date(millSec);

return sdf.format(date);

}


写一个main函数测试一下我们写的方法:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class test {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub System.out.println(transferLongToDate("MM/dd/yyyy",System.currentTimeMillis()));
}
/**
* 把毫秒转化成日期
* @param dateFormat(日期格式,例如:MM/ dd/yyyy HH:mm:ss)
* @param millSec(毫秒数)
* @return
*/
private static String transferLongToDate(String dateFormat,Long millSec){ SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date= new Date(millSec);
return sdf.format(date);
}
}

注意事项

注意我在这里用的是在main函数中调用,所以必须把定义的transferLongToDate方法生声明为静态函数

[size=large]camel骆驼男士凉鞋 真皮潮流魔术贴沙滩鞋男鞋 夏季新款正品凉鞋 只要56元![/size]
[img]http://dl2.iteye.com/upload/attachment/0100/0992/48f19884-1441-3164-b472-bc04a9e7dd39.jpg[/img]
[url]http://redirect.simba.taobao.com/rd?w=unionnojs&f=http%3A%2F%2Fai.taobao.com%2Fauction%2Fedetail.htm%3Fe%3DYNbrUj%252FZdJwjmraEDZVrLkKA%252ByOYgzU6TbuPAry6zvGLltG5xFicOdXrTUTgh9sMDPIwxrc30rhF03SVjj78hGqYCHH8uv2oZb7Xhy%252F%252BGHmWC8e6JwspUeIZWR1bMnHu%26unid%3D96391090%26ptype%3D100010%26from%3Dbasic&k=5ccfdb950740ca16&c=un&b=alimm_0&p=mm_96391090_7268811_24064425[/url]

[size=x-large]原博客地址:

java编程之怎样把Long转换成Date的日期格式

[url]http://jingyan.baidu.com/article/19192ad8312e6ae53f57074c.html[/url]

java编程之String的日期转换成Long类型的时间

[url]http://jingyan.baidu.com/article/915fc414cdd2eb51384b204c.html[/url]

mysql数据库中null值与其他值的比较

[url]http://jingyan.baidu.com/article/9113f81b2adc882b3214c7cb.html[/url]

Java编程经验分享之Map使用

[url]http://jingyan.baidu.com/article/215817f7d9d6b31eda1423d8.html[/url]

MySql使用经验——通过in查询怎样排序问题

[url]http://jingyan.baidu.com/article/67662997325ccf54d51b84d5.html[/url]

Mybatis使用经验之xml注意事项

[url]http://jingyan.baidu.com/article/fec7a1e51d7b451190b4e729.html[/url]

Mybatis使用经验分享之批量操作

[url]http://jingyan.baidu.com/article/11c17a2c7f376af446e39d21.html[/url]

MyBatis使用经验分享之查询

[url]http://jingyan.baidu.com/article/af9f5a2dd8143b43140a4520.html[/url]

linux怎样搭建tomcat服务器

[url]http://jingyan.baidu.com/article/3052f5a1d93c1497f31f860d.html[/url]

怎样用linux脚本查询数据中的数据

[url]http://jingyan.baidu.com/article/2c8c281dfb0add0008252a04.html[/url]

linux编程实践经验sshpass、md5sum、ssh

[url]http://jingyan.baidu.com/article/cd4c2979ca556c756e6e60aa.html[/url]
[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值