当前位置: 移动技术网 > IT编程>开发语言>Java > Java编程实现时间和时间戳相互转换实例

Java编程实现时间和时间戳相互转换实例

2019年07月19日  | 移动技术网IT编程  | 我要评论
时间戳(timestamp),一个能表示一份数据在某个特定时间之前已经存在的、 完整的、 可验证的数据,通常是一个字符序列,唯一地标识某一刻的时间。使用数字签名技术产生的数

时间戳(timestamp),一个能表示一份数据在某个特定时间之前已经存在的、 完整的、 可验证的数据,通常是一个字符序列,唯一地标识某一刻的时间。使用数字签名技术产生的数据, 签名的对象包括了原始文件信息、 签名参数、 签名时间等信息。广泛的运用在知识产权保护、 合同签字、 金融帐务、 电子报价投标、 股票交易等方面。

时间转换为时间戳:

/* 
 * 将时间转换为时间戳
 */  
public static string datetostamp(string s) throws parseexception{
  string res;
  simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  date date = simpledateformat.parse(s);
  long ts = date.gettime();
  res = string.valueof(ts);
  return res;
}

时间戳转换为时间:

/* 
 * 将时间戳转换为时间
 */
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;
}

简单地说,时间戳就是一种类型,只是精度很高,比datetime要精确的多,通常用来防止数据出现脏读现象。

以上就是本文关于java实现时间和时间戳相互转换的方法实例,希望对大家有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网