JAVA Date String 转 timestamp

1.当前日转String转时间戳

Date dt=new Date();
SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(matter1.format(dt));

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Timestamp ts = new Timestamp(format.parse(matter1.format(dt)).getTime());
System.out.println(ts.toString());



2.当前日期转时间戳

java.util.Date today = new java.util.Date();
return new java.sql.Timestamp(today.getTime());


3.时间戳转String

public static String timestamp2Str(Timestamp tp) {
if (tp == null) {
return "";
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = df.format(tp);
return str;
}


timestamp to Date

[img]http://dl2.iteye.com/upload/attachment/0097/8482/525e7e1d-ebf9-3e1e-bd61-b45ce1632a36.jpg[/img]


比较


<code>
String DateStr1 = "2014-10-1 10:20:16";
String DateStr2 = "2014-10-07 15:50:35";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateTime1 = dateFormat.parse(DateStr1);
Date dateTime2 = dateFormat.parse(DateStr2);
int i = dateTime1.compareTo(dateTime2);
System.out.println(i < 0);

</code>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值