根据 Google App Engine flexible docs ,对于任何传入请求,作为应用程序的服务,App Engine 将以下 header 添加到所有请求:
X-AppEngine-Country as an ISO 3166-1 alpha-2 country code
X-AppEngine-Region as an ISO-3166-2 standard
X-AppEngine-City
X-AppEngine-CityLatLong
X-Cloud-Trace-Context
X-Forwarded-For: [CLIENT_IP(s)], [global forwarding rule IP]
X-Forwarded-Proto [http | https]
无论如何,我是否可以使用 Java 使用请求 header 中的上述信息获取时区偏移量?
最佳答案
将下面添加到 pom.xml
<dependency>
<groupId>net.iakovlev</groupId>
<artifactId>timeshape</artifactId>
<version>2018d.1</version>
</dependency>
然后运行以下类型的代码
package taruntest;
import net.iakovlev.timeshape.TimeZoneEngine;
import java.time.ZoneId;
import java.util.Optional;
public class ZoneInfo {
public static TimeZoneEngine engine = null;
private static Optional<ZoneId> ZoneID;
public static void main(String[] args) {
ZoneID = getZoneIdFromLatLong("12.971599,77.594563");
System.out.println(ZoneID.toString());
}
public static Optional<ZoneId> getZoneIdFromLatLong(String latLong) {
if (engine == null)
{
engine = TimeZoneEngine.initialize();
}
String[] latLongArr = latLong.split(",");
double _lat = Double.parseDouble(latLongArr[0]);
double _long = Double.parseDouble(latLongArr[1]);
Optional<ZoneId> maybeZoneId = engine.query(_lat, _long);
return maybeZoneId;
}
}
结果是
Optional[Asia/Kolkata]
您可以通过以下方式获取您当前的坐标
关于java - 从 Google App Engine 请求 header 获取时区偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50005903/