0

I might have little confusion about Date in android/java. What I know is when calling new Date() it creates a Date instance with current UTC date and time, Right ? Because Date in java don't have any time zone thing, So if I call new Date().getTime() I will get a long value(time stamp) as UTC, not as local time, right ?

And to show date, we use 'DateFormat' and it has time zone info. So when I call DateFormat.getDateTimeInstance().format(new Date()) I will get a string with local time.

But how do I get long value(time stamp) of local time ?? I found this answer but is that the only way ? or something more simple ?

Thank you :)

Community
  • 1
  • 1
xmen
  • 1,947
  • 2
  • 25
  • 47

4 Answers4

1

So if I call new Date().getTime() I will get a long value(time stamp) as UTC, not as local time, right?

Well, it will give you the number of milliseconds since Jan 1st 1970 00:00:00 UTC, yes. It's not "in" UTC particularly; it's just a number of milliseconds since an arbitrary epoch.

But how do I get long value(time stamp) of local time?

You don't, basically. That turns out not to be a particularly useful concept. If you think about it, a timestamp is just an instant in time - it's independent of time zones. You can express the Unix epoch in any time zone; it just happens to normally be expressed in terms of UTC.

If you need the local date/time for a particular timestamp, you just need to remember the timestamp itself and the relevant time zone. If you give us more information about what you're trying to achieve, we may be able to help more.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

See Date.getTime javadocs: Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. There is no local timestamp. There can be only different textual date / time representations of it.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

You can try these to get local date.

String s=new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH).format(new Date());

or

Calendar c=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Ksharp
  • 102
  • 3
0

But how do I get long value(time stamp) of local time ??

A local date-time in a timezone is just a representation of the universal instant in that timezone. The new java.util.Date() gives us that universal instant i.e. it simply represents an instant on the timeline — a wrapper around the number of milliseconds since the UNIX epoch (January 1, 1970, 00:00:00 GMT). Since it does not hold any timezone information, its toString function applies the JVM's timezone to return a String in the format, EEE MMM dd HH:mm:ss zzz yyyy, derived from this milliseconds value. In other words, the same milliseconds will be represented as different date-times in different timezones. The vice versa: at any given moment, date-times in different timezones will give us the same number of epoch milliseconds.

java.time

The object corresponding to new java.util.Date() in java.time, the modern Date-Time API is Instant.now().

A demo of java.time, the modern Date-Time API:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        // Let's assume 2021-06-26T23:35:50 is the local date-time in India
        LocalDateTime ldtIndia = LocalDateTime.parse("2021-06-26T23:35:50");
        Instant instant = ldtIndia.atZone(ZoneId.of("Asia/Kolkata")).toInstant();
        long millis = instant.toEpochMilli();
        System.out.println(millis);
    }
}

Output:

1624730750000

ONLINE DEMO

You can convert Instant and java.util.Date to each other using

java.util.Date#from(Instant) and java.util.Date#toInstant().

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110