Home  >  Article  >  Java  >  Java Example - Convert timestamp to time

Java Example - Convert timestamp to time

黄舟
黄舟Original
2017-02-18 09:54:181863browse

The following example demonstrates how to use the format() method of the SimpleDateFormat class to convert a timestamp into a time:

/*
 author by w3cschool.cc
 文件名:Main.java
 */import java.text.SimpleDateFormat;import java.util.Date;public class Main{
    public static void main(String[] args){
        Long timeStamp = System.currentTimeMillis();  //获取当前时间戳
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));   // 时间戳转换成时间
        System.out.println(sd);
   }}

The output of the above code is:

2015-03-28

The above is the Java example - the content of converting timestamp into time. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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