Easy Tutorial
❮ Collection Readonly Data Reverse ❯

Java Example - Timestamp Conversion to Time

Java Example

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

Date and Time Patterns (Note the case sensitivity, as they represent different meanings):

Main.java File

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        Long timeStamp = System.currentTimeMillis();  // Get current timestamp
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));  // Convert timestamp to time
        System.out.println("Formatted Result: " + sd);

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String sd2 = sdf2.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        System.out.println("Formatted Result: " + sd2);
    }
}

The above code outputs the following result when run:

Formatted Result: 2018-07-10 12:17:34
Formatted Result: 2018年07月10日12时17分34秒

Java Example

❮ Collection Readonly Data Reverse ❯