Home > Blockchain >  How to get day of the week of GMT using kotlin?
How to get day of the week of GMT using kotlin?

Time:11-07

For EXAMPLE, current UTC time is:

17:14:24 UTC Friday, 5 November 2021

I want to get the result "6" (Sunday = 1 => Friday = 6)

CodePudding user response:

Using kotlinx.datetime (which is multiplatform):

import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.isoDayNumber
import kotlinx.datetime.toLocalDateTime

public val DayOfWeek.dayNumberStartingFromSunday: Int
    get() = when (this) {
        DayOfWeek.SUNDAY -> 1
        else -> isoDayNumber   1
    }

fun main() {
//    val now: Instant = Clock.System.now()
    val now = Instant.parse("2021-11-05T17:14:24Z")
    val datetimeInUtc = now.toLocalDateTime(TimeZone.UTC)
    val dayNumberStartingFromSunday = datetimeInUtc.dayOfWeek.dayNumberStartingFromSunday
    println(dayNumberStartingFromSunday) // 6
}

CodePudding user response:

The first day of the week is Locale specific. Since you want the first day of the week to be Sunday, you can use Locale.US.

Demo:

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getDayOfWeekValue(LocalDateTime.of(2021, 11, 5, 17, 14, 24)));
    }

    static int getDayOfWeekValue(LocalDateTime input) {
        return Math.toIntExact(
                ChronoUnit.DAYS.between(
                        input.with(
                                TemporalAdjusters.previousOrSame(
                                        WeekFields.of(Locale.US)
                                            .getFirstDayOfWeek())), 
                        input.plusDays(1))); 
        // Note: One day has been added as ChronoUnit.DAYS.between excludes
        // the second parameter while calculating the number of days
    }
}

Output:

6

ONLINE DEMO

Note: Test this code with Locale.UK (for which the first day of the week is Monday) and you will get 5 as the output. As per your requirement, you can change the definition of the function like

static int getDayOfWeekValue(LocalDateTime input, Locale locale) {
    return Math.toIntExact(
            ChronoUnit.DAYS.between(
                    input.with(
                            TemporalAdjusters.previousOrSame(
                                    WeekFields.of(locale)
                                        .getFirstDayOfWeek())), 
                    input.plusDays(1))); 
    // Note: One day has been added as ChronoUnit.DAYS.between excludes
    // the second parameter while calculating the number of days
}

Learn more about the modern Date-Time API* from Trail: Date Time.


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8 APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time. Check this answer and this answer to learn how to use java.time API with JDBC.

  • Related