Java 如何在android中获取当前时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39897338/
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-11 21:32:29  来源:igfitidea点击:

How to get current time stamp in android?

javaandroidapiandroid-studiotimestamp

提问by rahul sharma

I want to get current Timestamp in this YYYY-MM-DDTHHmmssZ.For instance; Expected value : 2016-01-01%2000:00:00. I have tried various methods,One of them is below :

我想在这个YYYY-MM-DDTHHmmssZ 中获取当前时间戳。例如;预期值:2016-01-01%2000:00:00。我尝试了各种方法,其中一种方法如下:

String timeStamp = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss'Z'").format(new Timestamp(System.currentTimeMillis()));

Still I am facing the error (didn't getting expected output which is 2016-01-01%2000:00:00) , Any help will be appreciate,thanks.

我仍然面临错误(没有得到预期的输出,即 2016-01-01%2000:00:00),任何帮助将不胜感激,谢谢。

回答by Swanand

just use this:

只需使用这个:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
String format = simpleDateFormat.format(new Date());
Log.d("MainActivity", "Current Timestamp: " + format);

just change the format in which you want current time stamp.

只需更改您想要当前时间戳的格式。

Thanks. :)

谢谢。:)

回答by Sanket Prabhu

Your question is ambiguous but according to my understanding:

你的问题模棱两可,但根据我的理解:

String timeStamp = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss'Z'")

You haven to set the timezone, like:

您必须设置时区,例如:

private static SimpleDateFormat df
    = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ssz")
 df.setTimeZone(TimeZone.getTimeZone("GMT"));

OR

或者

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));           
System.out.println(sdf.format(new Date())); //-prints-> 2015-01-22T03:23:26Z

If you want to learn more. please refer to:

如果你想了解更多。请参阅:

https://svn.apache.org/repos/asf/commons/dormant/feedparser/trunk/src/java/org/apache/commons/feedparser/tools/ISO8601DateParser.java

https://svn.apache.org/repos/asf/commons/dormant/feedparser/trunk/src/java/org/apache/commons/feedparser/tools/ISO8601DateParser.java