8

I am trying to convert System.currentTimeMillis to current time format of (hh:mm:ss). So far this is what I've tried and it's not working right.

    Long currentTime = System.currentTimeMillis();

    int hours;
    int minutes;
    int seconds;

    String getSecToStr = currentTime.toString();
    String getTimeStr = getSecToStr.substring(8,13);

    seconds = Integer.parseInt(getTimeStr);

    minutes = seconds / 60;
    seconds -= minutes * 60;

    hours = minutes / 60;
    minutes -= hours * 60;

    String myResult = Integer.toString(hours) + ":" + Integer.toString(minutes) + ":" + Integer.toString(seconds);

    System.out.println("Current Time Is: " + myResult);

Any ideas? Much appreciated!

cryptic_coder
  • 181
  • 1
  • 2
  • 10
  • How is it not working? How does it differ from your expectation? – Lajos Arpad May 06 '19 at 14:38
  • System.currentTimeMillis pulls a 13 figure number. I believe those numbers include current date and time. The first 8 numbers I believe is the date and the last 5 is the time. When I use String.substring to assign number characters 8 to 13 as my seconds the end result is the following... 27:13:12 or 5:5:4 That's not what I want. I want the actual time. – cryptic_coder May 06 '19 at 14:46
  • It's not like that. Take a look [here](https://docs.oracle.com/javase/7/docs/api/java/lang/System.html) – D Ie May 06 '19 at 14:54
  • 1
    Possible duplicate of [How to convert Milliseconds to "X mins, x seconds" in Java?](https://stackoverflow.com/q/625433/90527), [Java System.currentTimeMillis() in Human Readable Format UTC](https://stackoverflow.com/q/41791292/90527) – outis May 07 '19 at 05:34

3 Answers3

11

There are some object that you can use to make this more easy, like SimpleDateFormat and Date.

First prepare the time in mills:

Long currentTime = System.currentTimeMillis();

Choose your desier format with SimpleDateFormat:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");

Create your date object:

Date date = new Date(currentTime);

Apply that format into your date object:

String time = simpleDateFormat.format(date);

Log it:

Log.d(TAG, "onCreate: " + time);

Result:

17:05:73
MohammadL
  • 2,398
  • 1
  • 19
  • 36
  • 2
    use `hh:mm:ss` or `HH:mm:ss` instead of `HH:MM:SS`. MM is for month and SS is part of millisecond. hh is for 12-hour format and HH is for 24-hour format. 05 is may and you can't have 73 seconds in a minute – Kilarn123 May 06 '19 at 14:49
  • 1
    These terrible date-time classes were supplanted years ago by the modern *java.time* classes with the adoption of JSR 310. Suggesting their use in 2019 is poor advice. – Basil Bourque May 06 '19 at 15:37
  • This Answer ignores the crucial issue of time zone. – Basil Bourque May 06 '19 at 15:57
6

tl;dr

No need for System.currentTimeMillis();. Use java.time classes.

LocalTime.now( 
    ZoneId.of( "America/Montreal" )
)
.truncatedTo( 
    ChronoUnit.SECONDS
)
.toString()

12:34:56

java.time

The modern approach uses the java.time classes. Never use the terrible Date and Calendar classes.

To get the current time-of-day requires a time zone. For any given moment, the time-of-day (and the date) vary around the globe by zone.

ZoneId z = ZoneId.of( "Australia/Sydney" ) ;

Capture the current time of day as seen in the wall-clock time used by the people of that particular region, that time zone. Obtain a LocalTime object.

LocalTime lt = LocalTime.now( z ) ;

If you want UTC rather than a particular zone, pass the ZoneOffset.UTC constant.

Apparently you want to track the time-of-day to the whole second. So let’s lop off the fractional second.

LocalTime lt = LocalTime.now( z ).truncatedTo( ChronoUnit.SECONDS ) ;

Generate text in standard ISO 8601 format by calling toString.

String output = lt.toString() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

enter image description here

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Hi Basil, a great answer but this approach requires API level 26 which a lot of devices still using less than API 26. I suppose you highlight your answer for API 26 or Higher. – Ally May 17 '20 at 07:29
  • @Ali See links added to new section at bottom of the Answer for *ThreeTenABP*. – Basil Bourque May 17 '20 at 13:59
4

The following is equivalent to the date format HH:mm:ss

String.format("%1$TH:%1$TM:%1$TS", System.currentTimeMillis())
Palamino
  • 791
  • 3
  • 10