Is there something like TimeSpan in android development?

后端 未结 6 498
盖世英雄少女心
盖世英雄少女心 2021-01-12 05:29

I need to know if there is something like a timespan in android development?

in C# there is something like and I like to use it in two ways:

  1. generate a
6条回答
  •  伪装坚强ぢ
    2021-01-12 06:03

    Unfortunately, there's no TimeSpan like class yet natively available in Java, but you can achieve this with few lines of code.

    Calendar startDate = getStartDate();
    Calendar endDate = getEndDate();
    
    long totalMillis = endDate.getTimeInMillis() - startDate.getTimeInMillis();
    int seconds = (int) (totalMillis / 1000) % 60;
    int minutes =  ((int)(totalMillis / 1000) / 60) % 60;
    int hours = (int)(totalMillis / 1000) / 3600;
    

提交回复
热议问题