R Suman Radhakrishnan
R Suman Radhakrishnan

Reputation: 103

How to set hours, minutes, seconds for date object in android

I have Date object like this -> Sat Jun 26 00:00:00 IST 2021 I want to set hours, minutes, seconds like this -> Sat Jun 26 23:59:59 IST 2021

In Android

Upvotes: 2

Views: 5443

Answers (3)

ZyGabriel
ZyGabriel

Reputation: 39

Or use a LocalDateTime from Java 8

// change this to anything you want - Year.of(<int>)
val currentYear = Year.now().value
//  change this to anything you want - YearMonth.of(<year in int>, <month int value>)
val currentMonth = YearMonth.now()

LocalDateTime.of(currentYear, currentMonth.month.value, 26, 23, 59, 59)

Upvotes: -2

Jayanth
Jayanth

Reputation: 6277

You can use the Calendar API in Java, Get a reference to calendar like below.

Calendar calendar = Calendar.getInstance();

You can use the below methods to set minute and hour

calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, seconds);

read more about Calendar Api Oracal Docs Calendar
Calandar Api example

Upvotes: 2

tomas
tomas

Reputation: 406

You can do that with Calendar.

val calendar = Calendar.getInstance()
calendar.time = Date() // Set your date object here
    
calendar.set(Calendar.HOUR_OF_DAY, 23)
calendar.set(Calendar.MINUTE, 59)
calendar.set(Calendar.SECOND, 59)
calendar.time // Your changed date object

Upvotes: 3

Related Questions