Java 将日期时间、日出和日落从毫秒转换为实时格式

Java 将日期时间、日出和日落从毫秒转换为实时格式,java,android,json,datetime,weather,Java,Android,Json,Datetime,Weather,我的天气应用程序以毫秒(即1620792785)的形式显示OpenWeatherMap中的日期时间(上次更新)、日出和日落数据。 我正在尝试将其转换为实时格式(即hh:mm a) 我尝试在示例类中使用dt的以下代码: public String getPrettyDate() { SimpleDateFormat HMM = new SimpleDateFormat("hh:mm a", Locale.US); final Date date

我的天气应用程序以毫秒(即1620792785)的形式显示OpenWeatherMap中的日期时间(上次更新)、日出和日落数据。 我正在尝试将其转换为实时格式(即hh:mm a)

我尝试在示例类中使用dt的以下代码:

public String getPrettyDate() {
        SimpleDateFormat HMM = new SimpleDateFormat("hh:mm a", Locale.US);
        final Date date = new Date(dt*1000);
        return HMM.format(date);
    }
public class Example {
    @SerializedName("coord")
    private Coord coord;
    @SerializedName("weather")
    private List<Weather> weather = null;
    @SerializedName("base")
    private String base;
    @SerializedName("main")
    private Main main;
    @SerializedName("visibility")
    private Integer visibility;
    @SerializedName("wind")
    private Wind wind;
    @SerializedName("clouds")
    private Clouds clouds;
    @SerializedName("dt")
    private Integer dt;
    @SerializedName("sys")
    private Sys sys;
    @SerializedName("timezone")
    private Integer timezone;
    @SerializedName("id")
    private Integer id;
    @SerializedName("name")
    private String name;
    @SerializedName("cod")
    private Integer cod;

    public Coord getCoord() {
        return coord;
    }

    public void setCoord(Coord coord) {
        this.coord = coord;
    }

    public List<Weather> getWeather() {
        return weather;
    }

    public void setWeather(List<Weather> weather) {
        this.weather = weather;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public Main getMain() {
        return main;
    }

    public void setMain(Main main) {
        this.main = main;
    }

    public Integer getVisibility() {
        return visibility;
    }

    public void setVisibility(Integer visibility) {
        this.visibility = visibility;
    }

    public Wind getWind() {
        return wind;
    }

    public void setWind(Wind wind) {
        this.wind = wind;
    }

    public Clouds getClouds() {
        return clouds;
    }

    public void setClouds(Clouds clouds) {
        this.clouds = clouds;
    }

    public Integer getDt() {
        return dt;
    }

    public void setDt(Integer dt) {
        this.dt = dt;
    }

    public Sys getSys() {
        return sys;
    }

    public void setSys(Sys sys) {
        this.sys = sys;
    }

    public Integer getTimezone() {
        return timezone;
    }

    public void setTimezone(Integer timezone) {
        this.timezone = timezone;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getCod() {
        return cod;
    }

    public void setCod(Integer cod) {
        this.cod = cod;
    }

    public class Clouds {

        @SerializedName("all")
        private Integer all;

        public Integer getAll() {
            return all;
        }

        public void setAll(Integer all) {
            this.all = all;
        }

    }

    public class Coord {

        @SerializedName("lon")
        private Double lon;
        @SerializedName("lat")
        private Double lat;

        public Double getLon() {
            return lon;
        }

        public void setLon(Double lon) {
            this.lon = lon;
        }

        public Double getLat() {
            return lat;
        }

        public void setLat(Double lat) {
            this.lat = lat;
        }

    }

    public class Main {

        @SerializedName("temp")
        private Double temp;
        @SerializedName("feels_like")
        private Double feelsLike;
        @SerializedName("temp_min")
        private Double tempMin;
        @SerializedName("temp_max")
        private Double tempMax;
        @SerializedName("pressure")
        private Integer pressure;
        @SerializedName("humidity")
        private Integer humidity;

        public Double getTemp() {
            return temp;
        }

        public void setTemp(Double temp) {
            this.temp = temp;
        }

        public Double getFeelsLike() {
            return feelsLike;
        }

        public void setFeelsLike(Double feelsLike) {
            this.feelsLike = feelsLike;
        }

        public Double getTempMin() {
            return tempMin;
        }

        public void setTempMin(Double tempMin) {
            this.tempMin = tempMin;
        }

        public Double getTempMax() {
            return tempMax;
        }

        public void setTempMax(Double tempMax) {
            this.tempMax = tempMax;
        }

        public Integer getPressure() {
            return pressure;
        }

        public void setPressure(Integer pressure) {
            this.pressure = pressure;
        }

        public Integer getHumidity() {
            return humidity;
        }

        public void setHumidity(Integer humidity) {
            this.humidity = humidity;
        }

    }

    public class Sys {

        @SerializedName("type")
        private Integer type;
        @SerializedName("id")
        private Integer id;
        @SerializedName("country")
        private String country;
        @SerializedName("sunrise")
        private Integer sunrise;
        @SerializedName("sunset")
        private Integer sunset;

        public Integer getType() {
            return type;
        }

        public void setType(Integer type) {
            this.type = type;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public Integer getSunrise() {
            return sunrise;

        }

        public void setSunrise(Integer sunrise) {
            this.sunrise = sunrise;
        }

        public Integer getSunset() {
            return sunset;

        }

        public void setSunset(Integer sunset) {
            this.sunset = sunset;
        }

    }

    public class Weather {

        @SerializedName("id")
        private Integer id;
        @SerializedName("main")
        private String main;
        @SerializedName("description")
        private String description;
        @SerializedName("icon")
        private String icon;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getMain() {
            return main;
        }

        public void setMain(String main) {
            this.main = main;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

    }

    public class Wind {

        @SerializedName("speed")
        private Double speed;
        @SerializedName("deg")
        private Integer deg;
        @SerializedName("gust")
        private Double gust;

        public Double getSpeed() {
            return speed;
        }

        public void setSpeed(Double speed) {
            this.speed = speed;
        }

        public Integer getDeg() {
            return deg;
        }

        public void setDeg(Integer deg) {
            this.deg = deg;
        }

        public Double getGust() {
            return gust;
        }

        public void setGust(Double gust) {
            this.gust = gust;
        }
    }

    public String getPrettyDate() {
        SimpleDateFormat HMM = new SimpleDateFormat("hh:mm a", Locale.US);
        final Date date = new Date(dt*1000);
        return HMM.format(date);
    }
}
它很好地转换了时间,但没有准确地显示数据(即当这里是下午3点时,它显示的是晚上9点)

我也检查了这个网站的类似问题,但没有发现

所以我想:

  • 准确地转换dt
  • 准确转换日出和日落时间 使用正确的代码和类
编辑: 我的应用程序可以搜索任何城市,所以我不会搜索特定时区,而是搜索所有城市

我的JSON响应:

{
   "coord":{
      "lon":-0.1257,
      "lat":51.5085
   },
   "weather":[
      {
         "id":804,
         "main":"Clouds",
         "description":"overcast clouds",
         "icon":"04d"
      }
   ],
   "base":"stations",
   "main":{
      "temp":289.16,
      "feels_like":288.07,
      "temp_min":286.87,
      "temp_max":290.76,
      "pressure":1009,
      "humidity":48
   },
   "visibility":10000,
   "wind":{
      "speed":0.45,
      "deg":109,
      "gust":2.68
   },
   "clouds":{
      "all":100
   },
   "dt":1620830862,
   "sys":{
      "type":2,
      "id":2019646,
      "country":"GB",
      "sunrise":1620792785,
      "sunset":1620848444
   },
   "timezone":3600,
   "id":2643743,
   "name":"London",
   "cod":200
}
我的示例类:

public String getPrettyDate() {
        SimpleDateFormat HMM = new SimpleDateFormat("hh:mm a", Locale.US);
        final Date date = new Date(dt*1000);
        return HMM.format(date);
    }
public class Example {
    @SerializedName("coord")
    private Coord coord;
    @SerializedName("weather")
    private List<Weather> weather = null;
    @SerializedName("base")
    private String base;
    @SerializedName("main")
    private Main main;
    @SerializedName("visibility")
    private Integer visibility;
    @SerializedName("wind")
    private Wind wind;
    @SerializedName("clouds")
    private Clouds clouds;
    @SerializedName("dt")
    private Integer dt;
    @SerializedName("sys")
    private Sys sys;
    @SerializedName("timezone")
    private Integer timezone;
    @SerializedName("id")
    private Integer id;
    @SerializedName("name")
    private String name;
    @SerializedName("cod")
    private Integer cod;

    public Coord getCoord() {
        return coord;
    }

    public void setCoord(Coord coord) {
        this.coord = coord;
    }

    public List<Weather> getWeather() {
        return weather;
    }

    public void setWeather(List<Weather> weather) {
        this.weather = weather;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public Main getMain() {
        return main;
    }

    public void setMain(Main main) {
        this.main = main;
    }

    public Integer getVisibility() {
        return visibility;
    }

    public void setVisibility(Integer visibility) {
        this.visibility = visibility;
    }

    public Wind getWind() {
        return wind;
    }

    public void setWind(Wind wind) {
        this.wind = wind;
    }

    public Clouds getClouds() {
        return clouds;
    }

    public void setClouds(Clouds clouds) {
        this.clouds = clouds;
    }

    public Integer getDt() {
        return dt;
    }

    public void setDt(Integer dt) {
        this.dt = dt;
    }

    public Sys getSys() {
        return sys;
    }

    public void setSys(Sys sys) {
        this.sys = sys;
    }

    public Integer getTimezone() {
        return timezone;
    }

    public void setTimezone(Integer timezone) {
        this.timezone = timezone;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getCod() {
        return cod;
    }

    public void setCod(Integer cod) {
        this.cod = cod;
    }

    public class Clouds {

        @SerializedName("all")
        private Integer all;

        public Integer getAll() {
            return all;
        }

        public void setAll(Integer all) {
            this.all = all;
        }

    }

    public class Coord {

        @SerializedName("lon")
        private Double lon;
        @SerializedName("lat")
        private Double lat;

        public Double getLon() {
            return lon;
        }

        public void setLon(Double lon) {
            this.lon = lon;
        }

        public Double getLat() {
            return lat;
        }

        public void setLat(Double lat) {
            this.lat = lat;
        }

    }

    public class Main {

        @SerializedName("temp")
        private Double temp;
        @SerializedName("feels_like")
        private Double feelsLike;
        @SerializedName("temp_min")
        private Double tempMin;
        @SerializedName("temp_max")
        private Double tempMax;
        @SerializedName("pressure")
        private Integer pressure;
        @SerializedName("humidity")
        private Integer humidity;

        public Double getTemp() {
            return temp;
        }

        public void setTemp(Double temp) {
            this.temp = temp;
        }

        public Double getFeelsLike() {
            return feelsLike;
        }

        public void setFeelsLike(Double feelsLike) {
            this.feelsLike = feelsLike;
        }

        public Double getTempMin() {
            return tempMin;
        }

        public void setTempMin(Double tempMin) {
            this.tempMin = tempMin;
        }

        public Double getTempMax() {
            return tempMax;
        }

        public void setTempMax(Double tempMax) {
            this.tempMax = tempMax;
        }

        public Integer getPressure() {
            return pressure;
        }

        public void setPressure(Integer pressure) {
            this.pressure = pressure;
        }

        public Integer getHumidity() {
            return humidity;
        }

        public void setHumidity(Integer humidity) {
            this.humidity = humidity;
        }

    }

    public class Sys {

        @SerializedName("type")
        private Integer type;
        @SerializedName("id")
        private Integer id;
        @SerializedName("country")
        private String country;
        @SerializedName("sunrise")
        private Integer sunrise;
        @SerializedName("sunset")
        private Integer sunset;

        public Integer getType() {
            return type;
        }

        public void setType(Integer type) {
            this.type = type;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public Integer getSunrise() {
            return sunrise;

        }

        public void setSunrise(Integer sunrise) {
            this.sunrise = sunrise;
        }

        public Integer getSunset() {
            return sunset;

        }

        public void setSunset(Integer sunset) {
            this.sunset = sunset;
        }

    }

    public class Weather {

        @SerializedName("id")
        private Integer id;
        @SerializedName("main")
        private String main;
        @SerializedName("description")
        private String description;
        @SerializedName("icon")
        private String icon;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getMain() {
            return main;
        }

        public void setMain(String main) {
            this.main = main;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

    }

    public class Wind {

        @SerializedName("speed")
        private Double speed;
        @SerializedName("deg")
        private Integer deg;
        @SerializedName("gust")
        private Double gust;

        public Double getSpeed() {
            return speed;
        }

        public void setSpeed(Double speed) {
            this.speed = speed;
        }

        public Integer getDeg() {
            return deg;
        }

        public void setDeg(Integer deg) {
            this.deg = deg;
        }

        public Double getGust() {
            return gust;
        }

        public void setGust(Double gust) {
            this.gust = gust;
        }
    }

    public String getPrettyDate() {
        SimpleDateFormat HMM = new SimpleDateFormat("hh:mm a", Locale.US);
        final Date date = new Date(dt*1000);
        return HMM.format(date);
    }
}
公共类示例{
@序列化名称(“协调”)
私人合作社;
@序列化名称(“天气”)
私有列表天气=空;
@序列化名称(“基”)
私有字符串基;
@序列化名称(“主”)
私人主楼;
@序列化名称(“可见性”)
私有整数可见性;
@序列化名称(“风”)
私人风;
@序列化名称(“云”)
私密的云朵;
@序列化名称(“dt”)
私有整数dt;
@序列化名称(“sys”)
私有系统;
@序列化名称(“时区”)
专用整数时区;
@序列化名称(“id”)
私有整数id;
@序列化名称(“名称”)
私有字符串名称;
@序列化名称(“cod”)
私有整数cod;
公共合作社{
返回坐标;
}
公共无效设置坐标(坐标坐标坐标){
this.coord=coord;
}
公共列表getWeather(){
回归天气;
}
公众天气(列出天气){
这个天气=天气;
}
公共字符串getBase(){
返回基地;
}
公共无效收进框(字符串基){
this.base=base;
}
公共Main getMain(){
回水总管;
}
公共无效设置主(主){
this.main=main;
}
公共整数getVisibility(){
返回能见度;
}
公共void集合可见性(整数可见性){
这个.可见性=可见性;
}
公共风{
回风;
}
公共空间风(风){
风=风;
}
公共云getClouds(){
回归云;
}
公共云(云){
云=云;
}
公共整数getDt(){
返回dt;
}
公共void setDt(整数dt){
这1.dt=dt;
}
公共系统getSys(){
返回系统;
}
公共无效设置系统(Sys Sys){
this.sys=sys;
}
公共整数getTimezone(){
返回时区;
}
公共无效设置时区(整数时区){
this.timezone=时区;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getCod(){
返回cod;
}
公共void setCod(整数cod){
this.cod=cod;
}
公共类云{
@序列化名称(“全部”)
私有整数all;
公共整数getAll(){
全部归还;
}
公共void setAll(整数all){
this.all=all;
}
}
公共类合作社{
@序列化名称(“lon”)
私人双伦敦;
@序列化名称(“lat”)
私人双lat;
公共双getLon(){
返回离子;
}
公共空间设置(双设置){
this.lon=lon;
}
公共双getLat(){
返回lat;
}
公共无效设置lat(双lat){
this.lat=lat;
}
}
公共班机{
@序列化名称(“临时”)
私人双温;
@序列化名称(“感觉像”)
私人的双重感觉;
@序列化名称(“临时名称”)
私人双温敏;
@序列化名称(“临时最大值”)
私人双tempMax;
@序列化名称(“压力”)
私人整数压力;
@序列化名称(“湿度”)
私人整数湿度;
公共双getTemp(){
返回温度;
}
公共无效设置(双临时){
this.temp=temp;
}
公共双getFeelsLike(){
回归的感觉;
}
公共无效设置feelsLike(双feelsLike){
this.feelsLike=feelsLike;
}
公共双getTempMin(){
返回tempMin;
}
公共无效设置时间最小值(双时间最小值){
this.tempMin=tempMin;
}
公共双getTempMax(){
返回tempMax;
}
public void setTempMax(双tempMax){
this.tempMax=tempMax;
}
公共整数getPressure(){
回流压力;
}
公共空隙设定压力(整定压力){
这个。压力=压力;
}
公共整数{
返回湿度;
}
公共空间设置湿度(整数湿度){
这个。湿度=湿度;
}
}
公共类系统{
@序列化名称(“类型”)
私有整数类型;
@序列化名称(“id”)
私有整数id;
@序列化名称(“国家”)
私人国家;
@序列化名称(“日出”)
私人整数日出;
@序列化名称(“日落”)
私人整数日落;
公共整数getType(){
返回类型;
}
公共void集合类型(整数类型){
this.type=type;
}
公共整数getId(){
Instant instant = Instant.ofEpochSecond( 1_620_792_785L ) ;
ZoneId z = ZoneId.of( "America/Managua" ) ;
ZonedDateTime z = instant.atZone( z ) ;
Set< String > timeZoneNames = ZoneId.getAvailableZoneIds() ;