0

Possible Duplicate:
How to transform currentTimeMillis to a readable date format?

This is my code:

long currentTime = System.currentTimeMillis();
final String appTime1 = Long.toString(currentTime);

it display the time as 1353929203337

Please i ask how can change the first format of time to this format 10:25:24 ?

Thank you.

Community
  • 1
  • 1
user1735757
  • 185
  • 1
  • 2
  • 8

4 Answers4

4
Date date = new Date(currentTime); // if U really have long
Strint result = new SimpleDateFormat("HH:mm:ss").format(date.getTime());
Romczyk
  • 361
  • 3
  • 12
  • Sure, but user1735757 have started with long, I wasn't sure what exactly he needs. My approach just converts to String whaat he had as incoming parameter. – Romczyk Nov 26 '12 at 12:14
  • 2
    @GanGnaMStYleOverFlowErroR The `Date` constructor that takes a `long` is not deprecated; see the API documentation. – Jesper Nov 26 '12 at 12:19
  • @Jesper oops, my bad. +1, guess i was just confused :P – PermGenError Nov 26 '12 at 12:21
  • @Romczyk: please i ask if its possible to have also millisecondes and nanosecondes here new SimpleDateFormat("HH:mm:ss") ? Thank you for answer – user1735757 Nov 28 '12 at 13:34
  • Milliseconds yes: ("HH:mm:ss:SSS") http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html But I am not sure, how it's best can be done to get nanoseconds printed: you can try ("HH:mm:ss:SSSSSS") out – Romczyk Nov 28 '12 at 15:28
  • And yes, as other users have stated, using Calendar would be a better approach – Romczyk Nov 28 '12 at 15:35
0

That's right, it's showing the time in milliseconds dating back from 1st January 1970. Use SimpleDateFormat to format the date.

Try this code:

public class DateChk {


    public static void main(String[] args){

        DateChk chk = new DateChk();
        String s = chk.getCaptureDate();

        System.out.println(s);

    }

    public String getCaptureDate() {


        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String dateTime = sdf.format(System.currentTimeMillis()); // Your Answer
        return dateTime;
    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

It depends for what you need it for:
For simple debugging output the simplest is:

 final String appTime1 = new Date(currentTime).toString();

For more serius time calculations you better use Calendar.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
-1
System.currentTimeMillis() 

will get you the number of milliseconds since some date in the 70's.

If you want the current time you need to use a Calendar class and retrieve the time out of there using SimpleDateFormat.

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( sdf.format(cal.getTime()) );
pengibot
  • 1,492
  • 3
  • 15
  • 35