Home Java中获取当前时间的方法
Post
Cancel

Java中获取当前时间的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package test.date.demo;

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

public class GetTimeDemo {
    public static void main(String[] args) {
        // 使用Date类和SimpleDateFormat类
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
        System.out.println(sdf.format(new Date()));
        
        // 使用Timestamp
        long time = System.currentTimeMillis();
        System.out.println(new Timestamp(time).toString());
    }
}
This post is licensed under CC BY 4.0 by the author.