【问题标题】:how do I calculate Prayer Times in kotlin or java?如何在 kotlin 或 java 中计算祈祷时间?
【发布时间】:2020-04-25 16:38:36
【问题描述】:

所以我正在使用这个类: http://praytimes.org/code/git/?a=viewblob&p=PrayTimes&h=093f77d6cc83b53fb12e9900803d5fa75dacd110&hb=HEAD&f=v1/java/PrayTime.java 为了获取祈祷时间,我想将它与我使用位置权限制作的纬度和经度一起使用,但是当我更改时区的值时,我不会给我准确的城市祈祷时间。 有没有办法让时区自动工作?所以如果摩洛哥的一个人使用我的应用程序,时区会自动更改为摩洛哥时区,所以祈祷时间会给出准确的时间?同样,如果一个人住在沙特、伊拉克、西班牙……等等

提前致谢。

**我的代码有问题 **

@SuppressLint("MissingPermission")
private fun getLastLocation() {
    if (checkPermission()) {
        if (isLocationEnabled()) {

            mFusedLocationClient.lastLocation.addOnCompleteListener(this) { task ->
                var location: Location? = task.result
                if (location == null) {
                    requestNewLocationData()
                } else {

                    val resultimeZone = TimezoneMapper.latLngToTimezoneString(
                        location.latitude,
                        location.longitude
                    )
                    findViewById<TextView>(R.id.cityName).text = resultimeZone
                    val latitude: Double = location.latitude
                    val longitude: Double = location.longitude
                    var timeZone = 3.00
                    val timeZoneId = timeZone.toString()
                    val prayTime = praytime()
                    prayTime.timeFormat = prayTime.time12 // getTime12 is protected
                    prayTime.calcMethod = prayTime.makkah // Makkah
                    prayTime.asrJuristic = prayTime.shafii // Shafii (standard)
                    prayTime.adjustHighLats = prayTime.angleBased
                    prayTime.timeZone = prayTime.timeZone
                    prayTime.computeMidDay(prayTime.dhuhrMinutes.toDouble())
                    val offsets = intArrayOf(
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0
                    ) // {Fajr,Sunrise,Dhuhr,Asr,Sunset,Maghrib,Isha}

                    prayTime.tune(offsets)
                    val cal = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId))
                    cal.time = Date()


                    val times: ArrayList<String> =
                        prayTime.getPrayerTimes(
                            cal,
                            latitude,
                            longitude,
                            timeZone
                        )
                    findViewById<TextView>(R.id.Fajr).text = times.get(0)
                    findViewById<TextView>(R.id.sabah).text = times.get(1)
                    findViewById<TextView>(R.id.dhuhr).text = times.get(2)
                    findViewById<TextView>(R.id.AzanTime).text = times.get(3)
                    findViewById<TextView>(R.id.maghreb).text = times.get(4)
                    findViewById<TextView>(R.id.Esha).text = times.get(6)

                }
            }
        } else {
            Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show()
            val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
            startActivity(intent)
        }
    } else {
        requestPermission()
    }

您可以将 Java 用于示例

【问题讨论】:

  • 我并不是说这是侮辱性的:以 UTC 格式返回祈祷时间(我知道,如此世俗),然后让用户的设备将其转换为当地时间。正是因此,廷巴克图的用户不必更改他们的时区以匹配您的时区。哪一个,如果他们无论如何都要改变他们的时区,我会假设他们会选择麦加并完全跳过你的程序。顺便说一句,麦加是 UTC+3 小时。
  • 哪个祷告时间?
  • 再一次,不要侮辱他们所有人!为什么一个祈祷与另一个不同? (我知道,糟糕的措辞)。
  • 你的意思是我应该改变祈祷时间课程还是什么?你也可以给我一个如何做的样本吗?
  • 仅供参考,CalendarDate 类存在严重缺陷。几年前,它们被 JSR 310 中定义的现代 java.time 类所取代。我强烈建议找到基于 java.time 的计算库,而不是使用的遗留类由您当前的图书馆提供。

标签: java android-studio kotlin timezone


【解决方案1】:

要获取时区,您可以轻松使用此库:https://raw.githubusercontent.com/drtimcooper/LatLongToTimezone/master/src/main/java/com/skedgo/converter/TimezoneMapper.java 之后使用 TimeZone.getTimeZone 也可用于计算祈祷时间,此站点的代码很有用 http://praytimes.org/code/

【讨论】:

  • 我理解你的问题,前几天我也问过类似的问题,好像你不能直接将经纬度转换为时区,但是使用我链接的库可以转换纬度和经度到时区的名称,例如“亚洲/德黑兰”,然后 TimeZone.getTimeZone 向您显示例如 GMT+04:30 @Abood
  • 你能给我举个例子吗?
  • 我在最后的评论@Abood 中给了你例子
【解决方案2】:

终于过了一段时间,我现在能够解决这个问题。

首先你需要这个库来获取时区,

TimeZoneMapper

然后你应该将这个库链接到你的时区变量,然后你应该像这样将时区减去 4:

 var TimeId = TimezoneMapper.latLngToTimezoneString(location.latitude, location.longitude)

                val gmt = TimeZone.getTimeZone(TimeId).getDisplayName(false, TimeZone.SHORT)

                val z1 = gmt.substring(4)
                val z = z1.replace(":", ".")
                val zo = z.toDouble()

就是这样

完整代码

                    var TimeId = TimezoneMapper.latLngToTimezoneString(location.latitude, location.longitude)

                    val gmt = TimeZone.getTimeZone(TimeId).getDisplayName(false, TimeZone.SHORT)

                    val z1 = gmt.substring(4)
                    val z = z1.replace(":", ".")
                    val zo = z.toDouble()
                    val latitude: Double = location.latitude
                    val longitude: Double = location.longitude
  val timeZoneId = TimeZone.getAvailableIDs()
                    val prayTime = praytime()
                    prayTime.timeFormat = prayTime.time12 // getTime12 is protected
                    prayTime.calcMethod = prayTime.makkah // Makkah
                    prayTime.asrJuristic = prayTime.shafii // Shafii (standard)
                    prayTime.adjustHighLats = prayTime.angleBased
                    // my codes
                    prayTime.timeZone = prayTime.timeZone1
                    prayTime.setFajrAngle(18.5)

                    prayTime.computeMidDay(prayTime.dhuhrMinutes.toDouble())
                    val offsets = intArrayOf(
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0
                    ) // {Fajr,Sunrise,Dhuhr,Asr,Sunset,Maghrib,Isha}

                    prayTime.tune(offsets)
                    val cal = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId.toString()))
                    val now = Date()
                    cal.time = now



                        val times: ArrayList<String> =
                            prayTime.getPrayerTimes(
                                cal, latitude, longitude, zo
                            )
                        findViewById<TextView>(R.id.Fajr).text = times.get(0)
                        findViewById<TextView>(R.id.sabah).text = times.get(1)
                        findViewById<TextView>(R.id.dhuhr).text = times.get(2)
                        findViewById<TextView>(R.id.AzanTime).text = times.get(3)
                        findViewById<TextView>(R.id.maghreb).text = times.get(4)
                        findViewById<TextView>(R.id.Esha).text = times.get(6)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-10-16
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 2021-05-10
  • 1970-01-01
  • 2012-09-14
相关资源
最近更新 更多