Android get Current UTC time
System.currentTimeMillis()
does give you the number of milliseconds since January 1, 1970 00:00:00 UTC. The reason you see local times might be because you convert a Date
instance to a string before using it. You can use DateFormat
s to convert Date
s to String
s in any timezone:
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());
Also see this related question.
How to get perfect UTC Time in java/android?
Try this code:
private String getCurrentDateTimeAccordingToUTC(String format) {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat.format(date);
}
String date = getCurrentDateTimeAccordingToUTC("dd-MM-yyyy hh:mm:ss a");
Log.e("date--","inUTC--:"+date);
How to get the current time in UTC, add some minutes to it and the convert it to a specified format in Kotlin
Use java.time
here, you can get the current time of a specific offset or even time zone and output it afterwards using the desired pattern:
import java.time.format.DateTimeFormatter
import java.time.ZoneOffset
import java.time.OffsetDateTime
fun main() {
val dateTime = getDateTimeFormatted(50, "HH:mm:ss")
println(dateTime)
}
fun getDateTimeFormatted(minutesToAdd: Long, pattern: String): String {
// get current time in UTC, no millis needed
val nowInUtc = OffsetDateTime.now(ZoneOffset.UTC)
// add some minutes to it
val someMinutesLater = nowInUtc.plusMinutes(minutesToAdd)
// return the result in the given pattern
return someMinutesLater.format(
DateTimeFormatter.ofPattern(pattern)
)
}
The output of an execution some seconds before posting this was:
09:43:00
If you are supporting older API versions than 26, you might find out Java 8 features are not directly available there.
You can use them anyway, just read the answers to this question, the most recent way is API Desugaring
How can I get the current date and time in UTC or GMT in Java?
java.util.Date
has no specific time zone, although its value is most commonly thought of in relation to UTC. What makes you think it's in local time?
To be precise: the value within a java.util.Date
is the number of milliseconds since the Unix epoch, which occurred at midnight January 1st 1970, UTC. The same epoch could also be described in other time zones, but the traditional description is in terms of UTC. As it's a number of milliseconds since a fixed epoch, the value within java.util.Date
is the same around the world at any particular instant, regardless of local time zone.
I suspect the problem is that you're displaying it via an instance of Calendar which uses the local timezone, or possibly using Date.toString()
which also uses the local timezone, or a SimpleDateFormat
instance, which, by default, also uses local timezone.
If this isn't the problem, please post some sample code.
I would, however, recommend that you use Joda-Time anyway, which offers a much clearer API.
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.
How get accurate UTC timestamp in android
On Linux platform, the system clock should be set to UTC. Whether it actually is, and whether it is accurate, is ultimately up to the user.
Calling System.currentTimeMillis()
will give the time in UTC since 1970-01-01T00:00:00Z
.
Is the result correct for international?
Yes. Provided that the clock is synced with a decent network time source and the user hasn't messed with it.
Maybe user can change device time and result be different?
Yes they can. There is not much you can do about it.
You could attempt to connect to a network time server, but the user could block that, or cause your game to connect to a fake time server. If they "own" the platform that your game runs on, you probably have no way to get guaranteed reliable time.
(The Wikipedia page on NTP talks about man-in-the-middle attacks. Unfortunately, standard NTP doesn't have a way to deal with that. There is a draft RFC for Network Time Security, but they have been working on it since 2015, and it still hadn't concluded at the time of writing.)
Related Topics
Android: Specify Two Different Images for Togglebutton Using Xml
Double Tap: Zoom on Android Mapview
Android: Cannot Perform This Operation Because the Connection Pool Has Been Closed
Android Studio Build Gradle Outofmemoryerror
Android Java.Net.Unknownhostexception: Host Is Unresolved (Strategy Question)
Loading Data Bigger Than the Memory Size in H2O
Execute Managebean Method from JavaScript Onload Event
Why We Can't Do List<Parent> Mylist = Arraylist<Child>();
Android: How to Hide Actionbar on Certain Activities
Xmpp with Java Asmack Library Supporting X-Facebook-Platform
How to Update Xml File from Another Xml File Dynamically
How to Make Edittext Not Focused When Creating Activity
Android Call a Method from Another Class
Android:Save a Bitmap to Bmp File Format