根据Google App Engine flexible docs,对于任何传入请求,作为应用程序的服务,App Engine会将以下标头添加到所有请求中:
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从请求标头中获取上述信息来获得时区偏移吗?
答案 0 :(得分:2)
在下方添加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]
您可以使用
获取当前的坐标答案 1 :(得分:-1)
您可以使用App Engine灵活标头信息获取时区偏移量。使用我基于App Engine flexible Java quickstart创建的代码来提取和检查标题信息:
package com.example.appengine.gettingstartedjava.helloworld;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// [START example]
@SuppressWarnings("serial")
@WebServlet(name = "helloworld", value = "/" )
public class HelloServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
out.println("Hello, world - Flex Servlet");
String country = req.getHeader("X-AppEngine-Country");
String region = req.getHeader("X-AppEngine-Region");
String city = req.getHeader("X-AppEngine-City");
Float cityLatLong = Float.valueOf(req.getHeader("X-AppEngine-CityLatLong"));
out.println("Country: " + country);
out.println("Region: " + region);
out.println("City: " + city);
out.println("CityLatLong: " + cityLatLong);
}
}
// [END example]
在定义区域后,有一个官方Java类来获取时区:ZonedDateTime。查找使用它的示例here。
ZonedDateTime klDateTime = ldt.atZone(ZoneId.of("Asia/Kuala_Lumpur"));
请注意,您必须将标头转换为ZoneId
易读字符串,并在城市之前添加大陆。您可以使用HashMap在Map
List
Map<String, List<String>> hm = new HashMap<String, List<String>>();
中使用{{3}}读取大陆密钥,其中大陆为密钥,列表中包含城市,每个列表分配到一个大陆
int, uint, int8, uint8 ....