java - 从 Google App Engine 请求 header 获取时区偏移量?

标签 java google-app-engine app-engine-flexible

根据 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]

您可以通过以下方式获取您当前的坐标

https://mylocationtest.appspot.com/

关于java - 从 Google App Engine 请求 header 获取时区偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50005903/

相关文章:

gcloud app 部署灵活环境挂起

java - 调用方法时出错 - java.lang.RuntimeException : Uncompilable source code - Erroneous tree type: <any>

java - 无法实例化配置单元中的错误

java - Android多项目应用结构

java - 从 Java 执行 screen 命令

google-app-engine - 即使修改了 html 页面,应用程序引擎也返回 304

python - 使用 Python 和 GAE 对编码的 URL 查询参数进行不一致的解码

google-app-engine - golang appengine goon lib 本地内存限制

mysql - 将 Spring Boot 应用程序连接到 Google Cloud 中的 MySQL 数据库

java - App Engine 灵活环境部署失败