Home > Enterprise >  How to get difference between two calendar times
How to get difference between two calendar times

Time:11-18

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?

CodePudding user response:

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

  • Related