Android Get Current Timestamp

Android Get Current timestamp?

The solution is :

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();

How to get current time and date in Android

You could use:

import java.util.Calendar;
import java.util.Date;

Date currentTime = Calendar.getInstance().getTime();

There are plenty of constants in Calendar for everything you need.

Check the Calendar class documentation.

best way to get timestamp in long in android?

Hi I hope this will help you

//Getting the current date
Date date = new Date();
//This method returns the time in millis
long timeMilli = date.getTime();
System.out.println("Time in milliseconds using Date class: " + timeMilli);

//creating Calendar instance
Calendar calendar = Calendar.getInstance();
//Returns current time in millis
long timeMilli2 = calendar.getTimeInMillis();
System.out.println("Time in milliseconds using Calendar: " + timeMilli2);

//Java 8 - toEpochMilli() method of ZonedDateTime
System.out.println("Getting time in milliseconds in Java 8: " +
ZonedDateTime.now().toInstant().toEpochMilli());

And output fo these options will be

Time in milliseconds using Date class: 1508484583259

Time in milliseconds using Calendar: 1508484583267

Getting time in milliseconds in Java 8: 1508484583331

if we convert those long values to the date format then all three will be the same and it will be

Input   1508484583259
Input (formatted) 1,508,484,583,259
Date (Etc/UTC) Friday, October 20, 2017 7:29:43 AM UTC
Date (GMT) Friday, October 20, 2017 7:29:43 AM GMT
Date (short/short format) 10/20/17 7:29 AM

Over here I posted only one option result but all three will be the same or you can also check it by your own on online long to date convertor.

How to get current timestamp in Android without it updating like a clock

Here you go:

public String getTimestamp() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
return sdf.format(new Date());
}

Change the format("yyyy-MM-dd'T'HH:mm:ss") and get your date in that particular format!

Which one is the best way to get timestamp in android in long format?

System.currentTimeMillis() is obviously the most efficient since it does not even create an object, but new Date() is really just a thin wrapper about a long, so it is not far behind. Calendar, on the other hand, is relatively slow and very complex, since it has to deal with the considerable complexity and all the oddities that are inherent to dates and times (leap years, daylight savings, timezones, etc.).

It's generally a good idea to deal only with long timestamps or Date objects within your application, and only use Calendar when you actually need to perform date/time calculations or to format dates for displaying them to the user. If you have to do a lot of this, using Joda Time is probably a good idea, for the cleaner interface and better performance.

for full discussion please check the link

How do I get the current time as a TimeStamp in Kotlin?

Kotlin doesn't have any time handling classes of its own, so you just use Java's java.time. For an ISO-8601 timestamp (which is the preferred format):

DateTimeFormatter.ISO_INSTANT.format(Instant.now())

That will return 2018-04-16T17:00:08.746Z. For your format, or if you need a different timezone, you can specify those:

DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS")
.withZone(ZoneOffset.UTC)
.format(Instant.now())

See the java.time.format.DateTimeFormatter JavaDoc for details on how to specify a format string.

The java.time classes are bundled with Android 26 and later, and with Java 8 and later. Most of the java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android (<26) in ThreeTenABP. See How to use ThreeTenABP….

Update 2020/07

The development of ThreeTenABP is winding down. With Gradle plugin 4.0 and higher, you can directly use java 8 APIs without requiring a minimum API level for your app.

For more information see Java 8+ API desugaring support (Android Gradle Plugin 4.0.0+)

Android get current datetime and search in Firestore

The problem is in the query you send to the database:

Query query = doc1.whereEqualTo("userId", user.getStudentID())
.whereEqualTo("timeStamp", "18/5/2022");

This code compares the date/Timestamp that you store in the database with a string value of "18/5/2022". While this string value may read like a data to you, it doesn't to the database, so it returns no documents, since no documents have a field called timeStamp with a string value of "18/5/2022".


If you want to filter on a date/timestamp, you will usually need to have two conditions in the query: the start timestamp and the end timestamp of the range you want to get back.

So if you want to return an entire day, you'll want to start at the timestamp of the start of the day, and end with the timestamp at the end of the day (or alternatively, end just before the timestamp at the start of the next day).

So in code that could be something like:

// Create the start date from your literal values (month #4 is May)
Date startAt = new GregorianCalendar(2022, 4, 18).getTime();
// Create the end date by adding one day worth of milliseconds
Date endBefore = new Date(Date.getTime() + 24*60*60*1000);

Query query = doc1.whereEqualTo("userId", user.getStudentID())
.whereEqualToOrGreaterThan("timeStamp", startAt);
.whereLessThan("timeStamp", endBefore);


Related Topics



Leave a reply



Submit