简体   繁体   English

在Java中将float转换为时间?

[英]Convert float to time in Java?

Is it possible to convert a float to a time in Java? 是否可以将浮点数转换为Java中的时间?

I'm new to web services and I'm trying to invoke a simple sunrise/sunset time webservice, the WSDL for this is here 我是Web服务的新手,我正在尝试调用一个简单的日出/日落时间webservice, 这里的WSDL就在这里

I've passed the service a latitude (34.0888), longitude (-118.40612), day, month and year and I have got values back: 我已经通过该服务的纬度(34.0888),经度(-118.40612),日,月和年,我有价值回来:

sunrise: 13.6935135 sunset: 26.607845 日出:13.6935135日落:26.607845

But the values are floats, does anyone know why this would be? 但价值观是浮动的,有谁知道为什么会这样? Or if it is possible to work out a time from them? 或者,如果有可能从他们那里计算出来的时间? I thought it might be unix time but I've not had much luck with that. 我认为这可能是unix时间,但我没有太多运气。

Any help is appreciated, thanks 任何帮助表示赞赏,谢谢

That's probably a number of hours relative to UTC. 这可能是相对于UTC的几个小时。 I would try something like this: 我会尝试这样的事情:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.add(Calendar.HOUR_OF_DAY, (int) sunrise);
calendar.set(Calendar.MINUTE, (sunrise * 60) % 60);
calendar.set(Calendar.SECOND, (sunrise * 3600) % 3600);

Then display the date in the timezone you want with a DateFormat : 然后使用DateFormat在您想要的时区中显示日期:

DateFormat fmt = new SimpleDateFormat();
fmt.setTimeZone(TimeZone.getTimeZone("Europe/Paris")); 
System.out.println(fmt.format(calendar.getTime()));

看起来不是一件很容易计算的东西...这里有一个完整的包来处理: https//github.com/mikereedell/sunrisesunsetlib-java

Float sunriseFloat = new Float(sunrise);  
Float sunsetFloat = new Float(sunset);

    Date startDate = new Date(sunriseFloat.longValue());  
    Date endDate = new Date(sunsetFloat.longValue());

It seems this is not a java problem, and you found a webservice with a underspecified interface. 看来这不是一个java问题,你发现了一个带有未指定接口的web服务。 I googled for a solution and I found several libraries able to calculate sunrise and sunset. 我搜索了一个解决方案,我找到了几个能够计算日出和日落的图书馆。 For example, this was one of the first results: http://www.jstott.me.uk/jsuntimes/ 例如,这是最初的结果之一: http//www.jstott.me.uk/jsuntimes/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM