根据国家/地区获取日期和时间

时间:2017-12-15 05:36:01

标签: java spring-boot time calendar timezone

我想根据用户的位置(基本上是国家/地区)获取日期和时间并将其存储在表格中。为此,我尝试了以下代码,但我不确定它是否获取服务器时间或用户所在的时区。有人可以指导我清除这个疑问吗?

@RequestMapping(value = "/tzonee", method = RequestMethod.GET)
public static void main(String[] args) {

    // get Calendar instance
    Calendar now = Calendar.getInstance();

    // get current TimeZone using getTimeZone method of Calendar class
    TimeZone timeZone = now.getTimeZone();
    Date d = new Date();
    timeZone.getID();

    System.out.println("in day light time is:" + timeZone.getID());

    Calendar c = Calendar.getInstance(TimeZone.getTimeZone(timeZone.getID()));

    System.out.println("Timestamp is:" + c.getTime());
    // display current TimeZone using getDisplayName() method of TimeZone
    // class
    System.out.println("Current TimeZone is : " + timeZone.getDisplayName());
}

0 个答案:

没有答案