Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取时区的本地日期时间_Java_Date_Time - Fatal编程技术网

Java 如何获取时区的本地日期时间

Java 如何获取时区的本地日期时间,java,date,time,Java,Date,Time,问题就在这里 我从API获取数据,如下所示 curl "http://api.timezonedb.com/?lat=53.7833&lng=-1.75&key=OXFVZEHUDERF&format=json" {"status":"OK","message":"","countryCode":"GB","zoneName":"Europe\/London","abbreviation":"BST","gmtOffset":"3600","dst":"1","times

问题就在这里

我从API获取数据,如下所示

curl "http://api.timezonedb.com/?lat=53.7833&lng=-1.75&key=OXFVZEHUDERF&format=json"
{"status":"OK","message":"","countryCode":"GB","zoneName":"Europe\/London","abbreviation":"BST","gmtOffset":"3600","dst":"1","timestamp":1442983759}Harits-MacBook-Pro-2:~ harit$ curl "http://api.timezonedb.com/?lat=-44.490947&lng=171.220966&key=API_KEY&format=json"
结果是

{"status":"OK","message":"","countryCode":"NZ","zoneName":"Pacific\/Auckland","abbreviation":"NZST","gmtOffset":"43200","dst":"0","timestamp":1443023417}
根据他们的要求,
时间戳是

Unix时间戳中的当前本地时间

如何将其打印为

2013-07-10T14:52:49

在那个时区
欧洲/伦敦

虽然您没有提到,但您可以用java 8实现它,如:

public static void main(String[] args) throws IOException {
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    final long unixTime = 1443023417;
    final String formattedDtm = Instant.ofEpochSecond(unixTime).atZone(ZoneId.of("PLT")).format(formatter);
    System.out.println(formattedDtm);   
    }
它打印:
2015-09-23 11:50:17


Java 8介绍了从Unix时间戳创建的方法,该时间戳可以转换为,然后可以根据您的要求进行格式化。

虽然您没有提到,但可以在Java 8中实现,如:

public static void main(String[] args) throws IOException {
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    final long unixTime = 1443023417;
    final String formattedDtm = Instant.ofEpochSecond(unixTime).atZone(ZoneId.of("PLT")).format(formatter);
    System.out.println(formattedDtm);   
    }
它打印:
2015-09-23 11:50:17


Java 8介绍了从Unix时间戳创建的方法,该时间戳可以转换为,然后可以根据您的要求进行格式化。

因为您有时区和时间戳,所以可以获得LocalDateTime作为

LocalDateTime lt = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of(ZoneId.SHORT_IDS.get("BST")));
System.out.println(lt);

因为你有时区和时间戳,你可以得到LocalDateTime

LocalDateTime lt = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of(ZoneId.SHORT_IDS.get("BST")));
System.out.println(lt);