B4J Question Calculate Sunrise and Sunset times from Log,Lat

TomDuncan

Active Member
Licensed User
Longtime User
Hi All,
Has anyone seen a way of getting the Sunset, Sunrise times.
I did find one from a few years ago but results are not what I expected.
I can get it in php but
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

an example (date 23-Jun-2018) using this library. The lib is not wrapped, accessed via JavaObjects. The B4J source and external jar attached.

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
    ' Ensure located in the B4J additional libraries folder
    ' Taken from https://github.com/mikereedell/sunrisesunsetlib-java
    #AdditionalJar: SunriseSunsetCalculator-1.1
#End Region

Sub Process_Globals
    ' Hamburg Germany
    Private LAT As String = "53.551086"
    Private LON As String = "9.993682"
    Private TZ As String = "Europe/Berlin"    ' https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
End Sub

Sub AppStart (Args() As String)
    CalculateSunRiseSet
End Sub

Sub CalculateSunRiseSet
    ' Location object with given LAT/LON
    Private joLoc As JavaObject
    joLoc.InitializeNewInstance("com.luckycatlabs.sunrisesunset.dto.Location", Array(LAT, LON))
    ' Sunsetrise object using location and timezone
    Private joSRS As JavaObject
    joSRS.InitializeNewInstance("com.luckycatlabs.sunrisesunset.SunriseSunsetCalculator", Array(joLoc, TZ))
    ' Calender object with actual date - use method set(int year, int month, int date) for other date
    Private joCal As JavaObject
    joCal.InitializeStatic("java.util.Calendar")
    ' Sunrise & Sunset
    Log(joSRS.RunMethod("getOfficialSunriseForDate", Array(joCal.RunMethod("getInstance", Null))))    '04:51
    Log(joSRS.RunMethod("getOfficialSunsetForDate", Array(joCal.RunMethod("getInstance", Null))))    '21:53
End Sub

EDIT: Sample sub for given date Year, Month, Day
B4X:
' Get sunriseset for a given date.
' year - set YEAR calendar field.
' month - set MONTH calendar field. Month value 0-based. e.g., 0 For January.
' date - set DAY_OF_MONTH calendar field.
'Example for 23-Jun-2018<code>
'CalculateSunRiseSetForDate(2018,5,23)
'</code>
Sub CalculateSunRiseSetForDate(year As Int, month As Int, day As Int)   'ignore
   ' Location object with given LAT/LON
   Private joLoc As JavaObject
   joLoc.InitializeNewInstance("com.luckycatlabs.sunrisesunset.dto.Location", Array(LAT, LON))
   ' Sunsetrise object using location and timezone
   Private joSRS As JavaObject
   joSRS.InitializeNewInstance("com.luckycatlabs.sunrisesunset.SunriseSunsetCalculator", Array(joLoc, TZ))
   ' Calender object with actual date - use method set(int year, int month, int date) for other date
   Private joCal As JavaObject
   joCal.InitializeStatic("java.util.Calendar")
   Private joCalI As JavaObject = joCal.RunMethod("getInstance", Null)
   joCalI.RunMethod("set", Array(year,month,day))
   ' Sunrise & Sunset
   Log(joSRS.RunMethod("getOfficialSunriseForDate", Array(joCalI)))
   Log(joSRS.RunMethod("getOfficialSunsetForDate", Array(joCalI)))   
End Sub
 

Attachments

  • sunriseset.zip
    9.7 KB · Views: 374
Last edited:
Upvote 0
Top