普通时间和时间戳

来源:互联网 发布:淘宝商品交易风险保障 编辑:程序博客网 时间:2024/05/01 09:13

这个是我在开发中用到的获取时间显示,发送时间戳格式的时间到服务器,所以就写了一个工具类方便使用,现在分享给大家。

package com.eg.time;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class TDate {/** * 时间格式 *  * @return */public static String getDate() {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = sdf.format(new Date());return time;}/** * 将系统时间转成Unix时间戳 *  * @return */public static long getDate1() {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = sdf.format(new Date());long epoch = 0;try {epoch = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime() / 1000;} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return epoch;}/** * 将Unix时间戳转换成指定格式日期 *  * @param timestampString * @return */public static String TimeStamp2Date(String timestampString) {Long timestamp = Long.parseLong(timestampString) * 1000;String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp));return date;}}


                                             
2 0
原创粉丝点击