You can get timezone offset using App Engine flexible headers’ information. Use this code I have created based on App Engine flexible Java quickstart to extract and check the headers information:
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]
There is a official Java class to get the timezone once the region is defined: ZonedDateTime. Find an example here that uses it.
ZonedDateTime klDateTime = ldt.atZone(ZoneId.of("Asia/Kuala_Lumpur"));
Notice that you have to transform the headers to a ZoneId
legible string, adding the continent before the city. You could read a continent key based on a city type containing them in a Map
of List
using HashMap, with the continents as keys and the cities in lists, each list assigned to one continent.
Map<String, List<String>> hm = new HashMap<String, List<String>>();