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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:38:06  来源:igfitidea点击:

How to get readable time from currentTimeMillis()

javatime

提问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 Calendarobject

将该值与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 Calendarobject

注意:c.getInstance()已经保存了当前的日期时间,所以第二行是多余的。我添加它以显示如何以毫秒为单位设置Calendar对象的时间

回答by Rahul Tripathi

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));

回答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 以可读格式表示当前时间