Java 如何从 currentTimeMillis() 获取可读时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18933712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get readable time from currentTimeMillis()
提问by user2802221
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)
所以下面有一种简单的方法可以从当前时间毫秒获取读数,但是如何从毫秒值获取可读时间?我会只是一堆被 60 除以增量的模数吗?(例外是 100 毫秒到一秒)
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());
}
}
回答by BackSlash
Use that value with a Calendar
object
将该值与Calendar
对象一起使用
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
注意:c.getInstance()
已经保存了当前的日期时间,所以第二行是多余的。我添加它以显示如何以毫秒为单位设置Calendar
对象的时间
回答by Rahul Tripathi
回答by Ahmed Adel Ismail
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
bothe 以可读格式表示当前时间