英文:
Not Reading Millisecond Time Correctly
问题
以下是您提供的代码部分的翻译:
从OpenWeatherMap API中获取数据一切都进行得很好,直到我开始返回内布拉斯加州奥马哈的日出和日落时间为止。我查看了这里的先前帖子,并将其用作参考,例如使用TimeUnit类的一个帖子,但在我的情况下仍然存在问题,以及使用Date / SimpleDateFormat的帖子,但它们似乎输出的时间几乎相同。我用于转换这些时间的方法在我看来可能是正确的,但它们转换成的时间却不正确。我是否应该使用特定的方法来进行转换,或者我在使用当前方法时做错了什么?
接收以毫秒为单位的时间的方法
public String getSunrise() {
valueString = main.substring(main.indexOf("sunrise") + 10, main.indexOf("sunrise") + 20);
System.out.println(valueString);
Long rise = Long.parseLong(valueString);
Date d = new Date(rise);
SimpleDateFormat format = new SimpleDateFormat("h:mm a");
format.setTimeZone(TimeZone.getTimeZone("CDT"));
String t = format.format(d);
return t;
}
public String getSunset() {
valueString = main.substring(main.indexOf("sunset") + 9, main.indexOf("sunset") + 19);
System.out.println(valueString);
Long set = Long.parseLong(valueString);
Date d = new Date(set);
SimpleDateFormat format = new SimpleDateFormat("h:mm a");
format.setTimeZone(TimeZone.getTimeZone("CDT"));
String t = format.format(d);
return t;
}
方法的控制台输出
1588504675
日出时间:9:15 AM
1588555387
日落时间:9:15 AM
从当前获取的数据中,日出时间应为早上6:17,日落时间应为晚上8:23,取自此链接:https://openweathermap.org/city/5074472。main
是完整的JSON字符串。
英文:
So I am pulling data from the OpenWeatherMap api and everying was going fine until I got to returning the sunrise and sunset times for Omaha, Nebraska. Ive looked at previous posts on here and used those as reference such as one using the TimeUnit class which is still off in my case and ones using Date/SimlpeDateFormatter but it seems they are outputting nearly the same time. The method to convert these times may look right to me but the times they convert to are not so. Is there a certain method I should use to convert them or am i doing something wrong with the current method I am using?
Methods that intake the time in milliseconds
public String getSunrise() {
valueString = main.substring(main.indexOf("\"sunrise\"") + 10, main.indexOf("\"sunrise\"") + 20);
System.out.println(valueString);
Long rise = Long.parseLong(valueString);
Date d = new Date(rise);
SimpleDateFormat format = new SimpleDateFormat("h:mm a");
format.setTimeZone(TimeZone.getTimeZone("CDT"));
String t = format.format(d);
return t;
}
public String getSunset() {
valueString = main.substring(main.indexOf("\"sunset\"") + 9, main.indexOf("\"sunset\"") + 19);
System.out.println(valueString);
Long set = Long.parseLong(valueString);
Date d = new Date(set);
SimpleDateFormat format = new SimpleDateFormat("h:mm a");
format.setTimeZone(TimeZone.getTimeZone("CDT"));
String t = format.format(d);
return t;
}
Console output of the methods
1588504675
Sunrise: 9:15 AM
1588555387
Sunset: 9:15 AM
From the current data it is pulling, the time should be sunrise = 6:17am and sunset = 8:23pm taken from this link: https://openweathermap.org/city/5074472. main
is the full JSON string.
答案1
得分: 2
接受以毫秒为单位的时间的方法:Open Weather Map API 中的值以秒为单位,而不是毫秒。在您的问题中,1588504675
表示的是1970年。但是,1588504675000
表示的是2020年5月3日(时间部分为UTC)。
英文:
Methods that intake the time in milliseconds: The value in the Open Weather Map API is in seconds, not milliseconds. In your question, 1588504675
is in the year 1970. But 1588504675000
is May 3rd 2020 (and the time portion is UTC).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论