rahul sharma
rahul sharma

Reputation: 149

How to get current time stamp in android?

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 :

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.

Upvotes: 2

Views: 35922

Answers (2)

Sanket Prabhu
Sanket Prabhu

Reputation: 2232

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

Upvotes: 4

Swanand
Swanand

Reputation: 507

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. :)

Upvotes: 7

Related Questions