Android: How to convert from SystemClock.elapsedRealTime to human readable CharSeq?

How do you convert from SystemClock.elapsedRealTime()to human-readable CharSeq, which can be displayed in text form?

+4
source share
2 answers

TL; DR

Duration.ofMillis(                 // Represent a span of time on scale of hours-minutes-seconds, unattached to the timeline.
    SystemClock.elapsedRealTime()  // 441_000L as an example.
)
.toString()                        // Generate a String in standard ISO 8601 format.

PT7M21S

Avoid the time of day format

Displaying elapsed time in the time of day format (HH: MM: SS.SSS) is misleading and ambiguous, as it can easily be interpreted as the time of day.

ISO 8601 - Duration Format

The ISO 8601 standard defines reasonable string representations of various date and time values. These values ​​include elapsed time.

PnYnMnDTnHnMnS, P, T years-month-days --. :

  • PT30M=
  • PT8H30M=
  • P3Y6M4DT12H30M5S " , , , , ".

"" "" . . , "", P, "". Cest la vie.

java.time

java.time , :

/ ISO 8601, .

SystemClock.elapsedRealTime() - long, . , , Duration.

long millis = SystemClock.elapsedRealTime() ;
Duration d = Duration.ofMillis( millis ) ;

441000 .

Duration d = Duration.ofMillis( 441_000L ) ;

. 441_000L 21 .

String output = d.toString() ;  // Generate standard ISO 8601 string.

PT7M21S

, Duration.

Duration d = Duration.parse( "PT7M21S" ) ;

ISO 8601, . (00:07:21), , . , .

to…Part .

int minutesPart = d.toMinutesPart() ; // The whole-minutes portion.
int secondsPart = d.toSecondsPart() ; // The whole-seconds portion.

.

long millis = d.toMillis() ;  // Not the part, but the entire span of time in terms of milliseconds.

java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time . JDBC , JDBC 4.2 , , java.sql.*.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .


Joda

UPDATE: Joda-Time , java.time. .

Joda-Time , Interval, Duration Period, , Period, , , .. ISO 8601. Period ISO 8601. Joda-Time Android, java.util.Date/.Calendar.

Period long . , , SystemClock.elapsedRealTime() .

long machineUptimeMillis = SystemClock.elapsedRealtime() ;
Period machineUptimePeriod = new Period( machineUptimeMillis );
String output = machineUptimePeriod.toString(); // Ex: P3DT2H15M37.123S

, - , long .

long start = SystemClock.elapsedRealtime() ;
// … Perform some operation …
long stop = SystemClock.elapsedRealtime() ;
long elapsedMillis = stop - start ;
Period elapsedPeriod = new Period( elapsedMillis );

P / T .

String output = elapsedPeriod.toString().replace( "P" , "" ).replace( "T", " " ).toLowerCase() ; 

, 3y6m4d 12h30m5s.

-

Period, "5 2 ".

, , .

PeriodFormatter formatter = PeriodFormat.wordBased( Locale.CANADA_FRENCH ) ;
String output = formatter.print( elapsedPeriod ) ;

PeriodFormatterBuilder.

 PeriodFormatter yearsAndMonths = new PeriodFormatterBuilder()
     .printZeroAlways()
     .appendYears()
     .appendSuffix(" year", " years")
     .appendSeparator(" and ")
     .printZeroRarelyLast()
     .appendMonths()
     .appendSuffix(" month", " months")
     .toFormatter() ;
+6

. : hh: mm: ss.SSS Java?. :

private static String formatInterval(final long millis) {
    final long hr = TimeUnit.MILLISECONDS.toHours(millis);
    final long min = TimeUnit.MILLISECONDS.toMinutes(millis - TimeUnit.HOURS.toMillis(hr));
    final long sec = TimeUnit.MILLISECONDS.toSeconds(millis - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
    final long ms = TimeUnit.MILLISECONDS.toMillis(millis - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min) - TimeUnit.SECONDS.toMillis(sec));
    return String.format("%02d:%02d:%02d.%03d", hr, min, sec, ms);
}

, TextView:

long elapsedTime = SystemClock.elapsedRealTime();
textView.setText(elapsedTime);

TextView , .

+2

Source: https://habr.com/ru/post/1599178/


All Articles