Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert float to time in Java?

Is it possible to convert a float to a time in 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

I've passed the service a latitude (34.0888), longitude (-118.40612), day, month and year and I have got values back:

sunrise: 13.6935135 sunset: 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.

Any help is appreciated, thanks

like image 993
ostegg548 Avatar asked Oct 17 '25 13:10

ostegg548


2 Answers

Doesn't look like a trivial thing to calculate... there is a full package to handle that here : https://github.com/mikereedell/sunrisesunsetlib-java

like image 76
Philippe Avatar answered Oct 19 '25 01:10

Philippe


That's probably a number of hours relative to 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 fmt = new SimpleDateFormat();
fmt.setTimeZone(TimeZone.getTimeZone("Europe/Paris")); 
System.out.println(fmt.format(calendar.getTime()));
like image 26
Emmanuel Bourg Avatar answered Oct 19 '25 03:10

Emmanuel Bourg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!