Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get readable time from currentTimeMillis() [duplicate]

Tags:

java

time

So below there's a simple way to get a readout from current time millis, but how do I get from my millisecond value to a readable time? Would I be just a stack of modulus that are dividing incrementally by 60? (exception being 100 milliseconds to a second)

Would I be right in thinking/doing that?

Thanks in advance for any help you can give

  public class DisplayTime {
    public static void main(String[] args) {
                System.out.print("Current time in milliseconds = ");
                System.out.println(System.currentTimeMillis());


        }
    }
like image 987
user2802221 Avatar asked Sep 21 '13 14:09

user2802221


3 Answers

You may try like this:-

    long yourmilliseconds = 1119193190;
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");

    Date resultdate = new Date(yourmilliseconds);
    System.out.println(sdf.format(resultdate));
like image 107
Rahul Tripathi Avatar answered Sep 19 '22 10:09

Rahul Tripathi


Use that value with a Calendar object

Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());

String date = c.get(Calendar.YEAR)+"-"+c.get(Calendar.MONTH)+"-"+c.get(Calendar.DAY_OF_MONTH);
String time = c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND);

System.out.println(date + " " + time);

Output:

2013-8-21 16:27:31

Note: c.getInstance() already holds the current datetime, so the second line is redundant. I added it to show how to set the time in millis to the Calendar object

like image 25
BackSlash Avatar answered Sep 20 '22 10:09

BackSlash


System.out.println(new java.util.Date()); 

is the same as

System.out.println(new java.util.Date(System.currentTimeMillis()));

bothe represent the current time in a readable format

like image 21
Ahmed Adel Ismail Avatar answered Sep 22 '22 10:09

Ahmed Adel Ismail