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());
}
}
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));
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With