1. 程式人生 > 其它 >獲取伺服器時間

獲取伺服器時間

獲取伺服器時間

package com.xiang.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/17 16:38
 */
public class SysData {
    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance(); // get current instance of the calendar
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        System.out.println(formatter.format(calendar.getTime()));
        System.out.println("/********************************************/");


        new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(date);

        System.out.println("/********************************************/");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        String format = dateFormat.format(date);
        System.out.println(format);

        System.out.println("/********************************************/");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設定日期格式
        String s = df.format(date);
        System.out.println(s);
        System.out.println("/********************************************/");

        LocalDateTime.now(); // get the current date and time
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        System.out.println(timeFormatter);

    }
}

控制檯執行結果

部落格連線:

https://www.cnblogs.com/d534/p/15257174.html