0

I want to get the two calender time difference

my calender is

 val calendar1 = Calendar.getInstance()
    calendar1[Calendar.HOUR+1] = hour.toString().toInt()
    calendar1[Calendar.MINUTE] = minute.toString().toInt()

this is giving in 12 hours format Wed Nov 17 06:30:31 GMT+05:30 2021

current calender is:-

 val calendar = Calendar.getInstance()
    calendar[Calendar.HOUR]=calendar.get(Calendar.HOUR)
    calendar[Calendar.MINUTE]=calendar.get(Calendar.MINUTE)

this is giving in 24 hours format Wed Nov 17 18:01:32 GMT+05:30 2021

how to get this calender in 12 hours format

thats why im having difficult in finding two calender difference

can anyone help?

Joffrey
  • 32,348
  • 6
  • 68
  • 100
IRON MAN
  • 191
  • 3
  • 18
  • What are you expecting this to do? `calendar[Calendar.HOUR]=calendar.get(Calendar.HOUR)` You are getting the hour from the calendar and setting the hour of the calendar to that same hour, so it's doing nothing. – Tenfour04 Nov 17 '21 at 13:46

1 Answers1

3

In first calender you are using

calendar1[Calendar.HOUR+1] -> which will be equivalent to calendar1[Calendar.HOUR_OF_DAY]

Here are the constants from Calendar class

public static final int HOUR = 10;
public static final int HOUR_OF_DAY = 11;

use calendar1[Calendar.HOUR] for 24 hour format .

Better way is to get millis from both calenders and find difference between 2 millis .

To get millis use calendar.time.time

for getting difference between millis see this answer

Manohar
  • 22,116
  • 9
  • 108
  • 144