Home > Software design >  Getting Date with Start time of the Day
Getting Date with Start time of the Day

Time:12-15

I am using below function to take the today's date :

fun getCurrentDateTime(dateFormat: String): String {
    val Datetime: String
    val c = Calendar.getInstance()
    val dateformat = SimpleDateFormat(dateFormat, Locale.getDefault())
    Datetime = dateformat.format(c.time)
    return Datetime
}

I have filter for today to sort fetch today's filtered data. But, With the above function I am filtering with the same values, Means start date for Today and end date for Today are both same.

I want it different. Means : Start Date should be 1639560609 (Wednesday, 15 December 2021 00:00:00 GMT 05:30) and End Date should be Current time (which I am getting with above function)

So, The Issue you got that I want the Today's start Date with start time of the day.

How ? Thanks.

CodePudding user response:

Use LocalDateTime to get current date and start of the day

val dateFormatter = DateTimeFormatter.ofPattern("EEEE, d MMMM yyyy HH:mm:ss")
val localDate = LocalDate.now()   // your current date time 
val startOfDay: LocalDateTime = localDate.atStartOfDay() // date time at start of the date
val timestamp = startOfDay.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() // start time to timestamp
Log.d("Date:", "start date $timestamp")
Log.d("Date:", "start date parsed ${startOfDay.format(dateFormatter)}")

Output:
Start Date Timestamp : 1639506600000
Parsed TimeStamp: Wednesday, 15 December 2021 00:00:00

  • Related