Home  >  Article  >  Java  >  Code example of converting time and timestamp in Java

Code example of converting time and timestamp in Java

黄舟
黄舟Original
2017-09-21 10:21:561824browse

This article mainly introduces what a timestamp is, as well as examples of converting time and timestamps through Java programming. It has certain reference value and friends in need can learn about it.

Timestamp (timestamp), a complete and verifiable data that can represent a piece of data that existed before a specific time, is usually a sequence of characters that uniquely identifies a certain moment in time. Using data generated by digital signature technology, the signature object includes original file information, signature parameters, signature time and other information. It is widely used in intellectual property protection, contract signing, financial accounting, electronic quotation and bidding, stock trading, etc.

Convert time to 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;
}

Convert timestamp to time:


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

Simply put, timestamp is a type, but the accuracy is very high, much more accurate than datetime. It is usually used to prevent dirty reading of data.

The above is the detailed content of Code example of converting time and timestamp in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn