2

I'm using this extension-function for getting day of week:

private fun WeatherList.getDayOfWeek() =
        LocalDateTime.ofEpochSecond(dt.toLong(), 0, ZoneOffset.UTC).toLocalDate().dayOfWeek

dt - date time in my class in Unix

WeatherList is my class:

@Serializable
@Parcelize
data class WeatherList(
    val dt: Int,
    val main: Main,
    val weather: List<Weather>,
    val clouds: Clouds,
    val wind: Wind,
    val visibility: Int,
    val pop: Double,
    val sys: Sys,
    val dt_txt: String,
) : Parcelable

I was thinking that this should use Locale for output day of week but it's always English. So how to use locale language for day of week?

SoulReaver313
  • 365
  • 1
  • 11
  • I would use a `ZonedDateTime` here, but that's something different. If you want to get the name of the day of week in a different locale, you can extract and translate it like this: `...dayOfWeek.getDisplayName(TextStyle.FULL, Locale.FRENCH);` for example. – deHaar Sep 10 '21 at 08:49
  • 1
    @deHaar amazing. That's straigth what i was looking for. So i can use short day of week format by using TextStyle.SHORT and using my locale. Thanks! – SoulReaver313 Sep 10 '21 at 09:18
  • You're welcome... And by the way: A `LocalDateTime` already knows its day of week, so there's no need to call `toLocalDate()`. – deHaar Sep 10 '21 at 09:22
  • @deHaar it's the same with toLocalTime(), right? – SoulReaver313 Sep 10 '21 at 09:31
  • A `LocalDateTime` actually consists of a `LocalDate` and a `LocalTime`. An `OffsetDateTime` has a `ZoneOffset` in addition. I don't know if that answers your question, but it could... – deHaar Sep 10 '21 at 09:34
  • @deHaar i meant if we can remove toLocaleDate() in my situation then if I want to use toLocaleTime() I can just not use it either – SoulReaver313 Sep 10 '21 at 09:50
  • You can do `toLocalTime()` if you want to get the full `LocalTime` of a `LocalDateTime`, `OffsetDateTime` or `ZonedDateTime`, but if you want to get the hours of day you don't have to call `toLocalTime()` before, because a `LocalDateTime` has `getHour()` itself (in Java, maybe it's just `hour` in Kotlin). You can get the single units of time directly, you don't have to call anything in between. – deHaar Sep 10 '21 at 10:39
  • @deHaar can you help me to handle with this function? I need to make it in SimpleDateFormat because LocalDateTime is min 26 sdk – SoulReaver313 Sep 13 '21 at 06:10
  • 1
    You can use the [API Desugaring](https://developer.android.com/studio/write/java8-support) or import the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) if you want to use (most part of) `java.time` in Android 26 and above. – deHaar Sep 13 '21 at 06:36
  • @deHaar i need to use my extension on sdk 23 :( so i need it to be in SimpleDateFormat – SoulReaver313 Sep 13 '21 at 06:41
  • 1
    Seriously, @SoulReaver313, don't use a `SimpleDateFormat` and `Date`/`Calendar` anymore. Better import some library that enables `java.time` in lower API versions. But if you insist on using the old and outdated API, then have a look at questions like [this one](https://stackoverflow.com/questions/1661325/simpledateformat-and-locale-based-format-string). You will need to use the `E` abbreviation for day-of-week names. – deHaar Sep 13 '21 at 06:55
  • 1
    @deHaar thank you. I'm already figured it out but i will definitely check libraries that you adviced – SoulReaver313 Sep 13 '21 at 07:04

1 Answers1

2

Here's an example that takes a Locale and the epoch seconds as arguments and returns the name of the week as common in that specific Locale:

import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.TextStyle;
import java.util.Locale

fun WeatherList.getDayOfWeekName(locale: Locale, epochSecs: Int): String {
    return LocalDateTime.ofEpochSecond(epochSecs.toLong(), 0, ZoneOffset.UTC)
                        .dayOfWeek
                        .getDisplayName(TextStyle.FULL, locale);
}

You can of course include a fixed Locale and directly reference some variable holding the epoch seconds, but especially the last line gives you the name of the day of week in the given Locale's language and format.

Passing Locale.GERMAN and the epoch seconds 1631264387 would return Freitag (that's a German Friday).

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    Very good answer! **Comment for Java developers**: In Java, it would be `.getDayOfWeek()` instead of `.dayOfWeek` as mentioned in this answer for Kotlin. – Arvind Kumar Avinash Sep 15 '21 at 11:11