0

Is it possible to get a TimeZone ID from a certain TimeStamp ? If it is please explain by a simple code.

 private String getDate(long timeStamp) {DateFormat objFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    TimeZone timeZone = TimeZone.getTimeZone("GMT+4:30");
    //Instead of the Above code I want to get the TimeZone ID from my timeStamp objFormatter.setTimeZone(timeZone);
    Calendar objCalendar =
            Calendar.getInstance(timeZone);
    objCalendar.setTimeInMillis(timeStamp * 1000);
    String result = objFormatter.format(objCalendar.getTime());
    objCalendar.clear();
    return result;
}
MohsenFM
  • 129
  • 1
  • 13
  • Its duplicate question. It has been asked multiple times. FYI, check this answer : https://stackoverflow.com/a/1707934/631803 – Vikasdeep Singh Jul 31 '17 at 01:35
  • Not repeatable to my question. I have a `long timeStamp` and I want to get the TimeZone from it. In fact in no where there is even a mention to timeStamp therefore it is not a duplicate question. – MohsenFM Jul 31 '17 at 01:38
  • Then explain your question properly. Half information is always dangerous. Also provide the code snippet, what you have tried. – Vikasdeep Singh Jul 31 '17 at 01:40
  • Possible duplicate of [TimeZone ID's in Java](https://stackoverflow.com/questions/1707799/timezone-ids-in-java) – Vikasdeep Singh Jul 31 '17 at 01:42
  • @VicJordan you can mark it as a duplicate all you want. It is not ! As I said before I want to get a TimeZone ID from a `long timestamp` – MohsenFM Jul 31 '17 at 01:46
  • @VicJordan No, your linked Question is *not* a duplicate. It has nothing to do with a timestamp or count-from-epoch shown here. But there are likely many *other* duplicates. – Basil Bourque Jul 31 '17 at 02:41

2 Answers2

6

tl;dr

Impossible to derive offset/zone from a count-from-epoch-in-UTC. But you can adjust into a zone.

Instant.ofEpochSecond( yourCount )
       .atZone( ZoneId.of( "Pacific/Auckland" ) ) 

Avoid count-from-epoch

Firstly, avoid using a count-from-epoch number to track date-time values. Do you mean a count of whole seconds, milliseconds, microseconds, nanoseconds, or something else? Do you mean the Unix/Java epoch of 1970-01-01T00:00:00Z or one of the couple dozen other epochs in use by many computer systems?

Apparently you have whole seconds, and I'll assume the Unix/Java epoch.

Impossible to get zone from count-from-epoch

You cannot “ get a TimeZone ID from a certain TimeStamp”, that is impossible. Your count-from-epoch was made while accounting for a certain time zone, usually UTC. If must know that intended zone used in creating that count-from-epoch, it cannot be deduced.

Perhaps your goal is actually adjusting this count-from-epoch into a date-time for a particular region’s time zone. Read on.

java.time

Avoid the troublesome old date-time classes such as Date & Calendar now supplanted by the java.time classes.

Convert your count-from-epoch into a point on the timeline in UTC.

Instant instant = Instant.ofEpochSecond( yourCount ) ;

Assign your desired time zone.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Asia/Kabul" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

See this code run live at IdeOne.com.

Notice the 4.5 hour difference, changing from 02:40 to 07:10, appropriate for time in Kabul. This is the same moment, the same point on the time zone, but viewed through the lens of a different region’s wall-clock time.

input: 1500000000

instant: 2017-07-14T02:40:00Z

zdt: 2017-07-14T07:10+04:30[Asia/Kabul]

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you. This was very through and helpful answer to my question. Exactly what I needed. I am reading timestamp from a website and that website changes timestamp according to the location of the user who sent http request. I wanted to have that timestamp in my application and get the timeZone from it. Since my user may use a proxy to access that website, It would have been convenient if I could just read the TimeStamp and get the location from it but as you said it is not possible. – MohsenFM Jul 31 '17 at 03:05
  • I use http request to fetch a website into my app then I read the TimeStamp from the website but I have no way of telling what the TimeZone might be because the user might install a proxy app on his/her cellphone. – MohsenFM Jul 31 '17 at 03:11
  • The website calculates the TimeStamp based on the http request from the user. Since I am sending the http Request in my app, is there any way to acquire the current timeZone of the user ? Not from his/her cellphone but from the http Request. – MohsenFM Jul 31 '17 at 03:16
1

I would like to answer this question based on the definition of each terminology.

What is timestamp?

Timestamp or Unix Timestamp is the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,minus the number of leap seconds that have taken place since then. Wikipedia

Wath is Time Zone?

A time zone is a region of the earth where the same standard time is used. Each time zone is described by an identifier and usually has the format region/city (Asia/Tokyo) and an offset from Greenwich/UTC time. For example, the offset for Tokyo is +09:00. Time Zone Oracle Doc

Regarding to both definitions there is no way to get a region of the earth based on a number of seconds (time), it is imperative to know from what region of the earth the time comes from.

Daniel C.
  • 5,418
  • 3
  • 23
  • 26