Convert UTC timestamp to local timestampAndroid get Current UTC timeGet date in current timezone in javaHow to convert a Drawable to a Bitmap?Converting pixels to dpCalendar getTimeInMillis() is timezone-dependent?Converting UTC timestamp to local device timestampandroid sqlite save and retrieve utc and convert to localHow to get another Calendar result from same instance?new Date() returns local time instead of UTCManually Set System Clock Screws Up UTC in Android and IOSHow to get 2 date objects holding the current time, one in GMT and the other in localConvert hour minute string time to utc and vice versa
Did the CIA blow up a Siberian pipeline in 1982?
What is the origin of Scooby-Doo's name?
What is the oldest commercial MS-DOS program that can run on modern versions of Windows without third-party software?
What are the pros and cons for the two possible "gear directions" when parking the car on a hill?
Story about a space war, and a human prisoner of war captured by alien enemy
How to make clear to people I don't want to answer their "Where are you from?" question?
Excluding a rectangular region from an image in FITS
What can I do with a research project that is my university’s intellectual property?
Why isn't my calculation that we should be able to see the sun well beyond the observable universe valid?
Prime sieve in Python
Intuition for the role of diffeomorphisms
Heavily limited premature compiler translates text into excecutable python code
Is it possible to get a mortgage with a custom duration in the US?
Constitutionality of U.S. Democratic Presidential Candidate's Supreme Court Suggestion
Why is it recommended to mix yogurt starter with a small amount of milk before adding to the entire batch?
Can Ogre clerics use Purify Food and Drink on humanoid characters?
Hit the Bulls Eye with T in the Center
Why is "Congress shall have power to enforce this article by appropriate legislation" necessary?
Is declining an undergraduate award which causes me discomfort appropriate?
What do they call philosophers in China?
Is there any proof that high saturation and contrast makes a picture more appealing in social media?
Count All Possible Unique Combinations of Letters in a Word
Why don't countries like Japan just print more money?
"Permanent resident of UK” for a British travel insurance in the US
Convert UTC timestamp to local timestamp
Android get Current UTC timeGet date in current timezone in javaHow to convert a Drawable to a Bitmap?Converting pixels to dpCalendar getTimeInMillis() is timezone-dependent?Converting UTC timestamp to local device timestampandroid sqlite save and retrieve utc and convert to localHow to get another Calendar result from same instance?new Date() returns local time instead of UTCManually Set System Clock Screws Up UTC in Android and IOSHow to get 2 date objects holding the current time, one in GMT and the other in localConvert hour minute string time to utc and vice versa
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to get the local time in millseconds from UTC milliseconds... I tried following code.. But it return same UTC time instead of local time.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(aLong);
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
calendar1.setTimeInMillis(calendar.getTimeInMillis());
return calendar1.getTimeInMillis();
Actually I don't want DateFormat String, I need milliseconds back... That's why I used Calendar instance.
android
add a comment |
I want to get the local time in millseconds from UTC milliseconds... I tried following code.. But it return same UTC time instead of local time.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(aLong);
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
calendar1.setTimeInMillis(calendar.getTimeInMillis());
return calendar1.getTimeInMillis();
Actually I don't want DateFormat String, I need milliseconds back... That's why I used Calendar instance.
android
stackoverflow.com/a/24808474/1042124
– Rahul Kumar
Mar 25 at 7:48
Possible duplicate of Android get Current UTC time
– ADM
Mar 25 at 7:50
@RahulKumar I have edited my question as I need milliseconds only back.
– Gunaseelan
Mar 25 at 8:02
add a comment |
I want to get the local time in millseconds from UTC milliseconds... I tried following code.. But it return same UTC time instead of local time.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(aLong);
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
calendar1.setTimeInMillis(calendar.getTimeInMillis());
return calendar1.getTimeInMillis();
Actually I don't want DateFormat String, I need milliseconds back... That's why I used Calendar instance.
android
I want to get the local time in millseconds from UTC milliseconds... I tried following code.. But it return same UTC time instead of local time.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(aLong);
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
calendar1.setTimeInMillis(calendar.getTimeInMillis());
return calendar1.getTimeInMillis();
Actually I don't want DateFormat String, I need milliseconds back... That's why I used Calendar instance.
android
android
edited Mar 25 at 8:01
Gunaseelan
asked Mar 25 at 7:36
GunaseelanGunaseelan
7,48294696
7,48294696
stackoverflow.com/a/24808474/1042124
– Rahul Kumar
Mar 25 at 7:48
Possible duplicate of Android get Current UTC time
– ADM
Mar 25 at 7:50
@RahulKumar I have edited my question as I need milliseconds only back.
– Gunaseelan
Mar 25 at 8:02
add a comment |
stackoverflow.com/a/24808474/1042124
– Rahul Kumar
Mar 25 at 7:48
Possible duplicate of Android get Current UTC time
– ADM
Mar 25 at 7:50
@RahulKumar I have edited my question as I need milliseconds only back.
– Gunaseelan
Mar 25 at 8:02
stackoverflow.com/a/24808474/1042124
– Rahul Kumar
Mar 25 at 7:48
stackoverflow.com/a/24808474/1042124
– Rahul Kumar
Mar 25 at 7:48
Possible duplicate of Android get Current UTC time
– ADM
Mar 25 at 7:50
Possible duplicate of Android get Current UTC time
– ADM
Mar 25 at 7:50
@RahulKumar I have edited my question as I need milliseconds only back.
– Gunaseelan
Mar 25 at 8:02
@RahulKumar I have edited my question as I need milliseconds only back.
– Gunaseelan
Mar 25 at 8:02
add a comment |
1 Answer
1
active
oldest
votes
The code you wrote creates two Calendars with the same timestamp in two different time zones, so of course the value of getTimeInMillis
will be the same. There are three components to a date - the time zone, the milliseconds since epoch, and the date fields - and when you set two of these, the third is derived from them. So what you should be setting in addition to the time zone, is the date fields, so that the milliseconds value can be derived.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
// 7 date fields: YEAR, MONTH, DAY_OF_MONTH,
// HOUR_OF_DAY, MINUTE, SECOND, and MILLISECOND
// they come from "import static java.util.Calendar.*;"
calendar1.set(calendar.get(YEAR),
calendar.get(MONTH),
calendar.get(DAY_OF_MONTH),
calendar.get(HOUR_OF_DAY),
calendar.get(MINUTE),
calendar.get(SECOND));
calendar1.set(MILLISECOND, calendar.get(MILLISECOND));
return calendar1.getTimeInMillis();
1553509009000
is my UTC time, When I convert it back using your answer I'm getting1553489209000
. Which is one hour extra.
– Gunaseelan
Mar 25 at 10:21
1553489209000 - 1553509009000
is not 1 hour extra, it's 5 and a half hours extra, which is correct if you live in India. Recheck your calculation.
– Leo Aso
Mar 25 at 12:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55333110%2fconvert-utc-timestamp-to-local-timestamp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The code you wrote creates two Calendars with the same timestamp in two different time zones, so of course the value of getTimeInMillis
will be the same. There are three components to a date - the time zone, the milliseconds since epoch, and the date fields - and when you set two of these, the third is derived from them. So what you should be setting in addition to the time zone, is the date fields, so that the milliseconds value can be derived.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
// 7 date fields: YEAR, MONTH, DAY_OF_MONTH,
// HOUR_OF_DAY, MINUTE, SECOND, and MILLISECOND
// they come from "import static java.util.Calendar.*;"
calendar1.set(calendar.get(YEAR),
calendar.get(MONTH),
calendar.get(DAY_OF_MONTH),
calendar.get(HOUR_OF_DAY),
calendar.get(MINUTE),
calendar.get(SECOND));
calendar1.set(MILLISECOND, calendar.get(MILLISECOND));
return calendar1.getTimeInMillis();
1553509009000
is my UTC time, When I convert it back using your answer I'm getting1553489209000
. Which is one hour extra.
– Gunaseelan
Mar 25 at 10:21
1553489209000 - 1553509009000
is not 1 hour extra, it's 5 and a half hours extra, which is correct if you live in India. Recheck your calculation.
– Leo Aso
Mar 25 at 12:00
add a comment |
The code you wrote creates two Calendars with the same timestamp in two different time zones, so of course the value of getTimeInMillis
will be the same. There are three components to a date - the time zone, the milliseconds since epoch, and the date fields - and when you set two of these, the third is derived from them. So what you should be setting in addition to the time zone, is the date fields, so that the milliseconds value can be derived.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
// 7 date fields: YEAR, MONTH, DAY_OF_MONTH,
// HOUR_OF_DAY, MINUTE, SECOND, and MILLISECOND
// they come from "import static java.util.Calendar.*;"
calendar1.set(calendar.get(YEAR),
calendar.get(MONTH),
calendar.get(DAY_OF_MONTH),
calendar.get(HOUR_OF_DAY),
calendar.get(MINUTE),
calendar.get(SECOND));
calendar1.set(MILLISECOND, calendar.get(MILLISECOND));
return calendar1.getTimeInMillis();
1553509009000
is my UTC time, When I convert it back using your answer I'm getting1553489209000
. Which is one hour extra.
– Gunaseelan
Mar 25 at 10:21
1553489209000 - 1553509009000
is not 1 hour extra, it's 5 and a half hours extra, which is correct if you live in India. Recheck your calculation.
– Leo Aso
Mar 25 at 12:00
add a comment |
The code you wrote creates two Calendars with the same timestamp in two different time zones, so of course the value of getTimeInMillis
will be the same. There are three components to a date - the time zone, the milliseconds since epoch, and the date fields - and when you set two of these, the third is derived from them. So what you should be setting in addition to the time zone, is the date fields, so that the milliseconds value can be derived.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
// 7 date fields: YEAR, MONTH, DAY_OF_MONTH,
// HOUR_OF_DAY, MINUTE, SECOND, and MILLISECOND
// they come from "import static java.util.Calendar.*;"
calendar1.set(calendar.get(YEAR),
calendar.get(MONTH),
calendar.get(DAY_OF_MONTH),
calendar.get(HOUR_OF_DAY),
calendar.get(MINUTE),
calendar.get(SECOND));
calendar1.set(MILLISECOND, calendar.get(MILLISECOND));
return calendar1.getTimeInMillis();
The code you wrote creates two Calendars with the same timestamp in two different time zones, so of course the value of getTimeInMillis
will be the same. There are three components to a date - the time zone, the milliseconds since epoch, and the date fields - and when you set two of these, the third is derived from them. So what you should be setting in addition to the time zone, is the date fields, so that the milliseconds value can be derived.
private long getLocalTimeFromUTC(long aLong)
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar calendar1 = Calendar.getInstance(TimeZone.getDefault());
// 7 date fields: YEAR, MONTH, DAY_OF_MONTH,
// HOUR_OF_DAY, MINUTE, SECOND, and MILLISECOND
// they come from "import static java.util.Calendar.*;"
calendar1.set(calendar.get(YEAR),
calendar.get(MONTH),
calendar.get(DAY_OF_MONTH),
calendar.get(HOUR_OF_DAY),
calendar.get(MINUTE),
calendar.get(SECOND));
calendar1.set(MILLISECOND, calendar.get(MILLISECOND));
return calendar1.getTimeInMillis();
answered Mar 25 at 9:28
Leo AsoLeo Aso
5,65311430
5,65311430
1553509009000
is my UTC time, When I convert it back using your answer I'm getting1553489209000
. Which is one hour extra.
– Gunaseelan
Mar 25 at 10:21
1553489209000 - 1553509009000
is not 1 hour extra, it's 5 and a half hours extra, which is correct if you live in India. Recheck your calculation.
– Leo Aso
Mar 25 at 12:00
add a comment |
1553509009000
is my UTC time, When I convert it back using your answer I'm getting1553489209000
. Which is one hour extra.
– Gunaseelan
Mar 25 at 10:21
1553489209000 - 1553509009000
is not 1 hour extra, it's 5 and a half hours extra, which is correct if you live in India. Recheck your calculation.
– Leo Aso
Mar 25 at 12:00
1553509009000
is my UTC time, When I convert it back using your answer I'm getting 1553489209000
. Which is one hour extra.– Gunaseelan
Mar 25 at 10:21
1553509009000
is my UTC time, When I convert it back using your answer I'm getting 1553489209000
. Which is one hour extra.– Gunaseelan
Mar 25 at 10:21
1553489209000 - 1553509009000
is not 1 hour extra, it's 5 and a half hours extra, which is correct if you live in India. Recheck your calculation.– Leo Aso
Mar 25 at 12:00
1553489209000 - 1553509009000
is not 1 hour extra, it's 5 and a half hours extra, which is correct if you live in India. Recheck your calculation.– Leo Aso
Mar 25 at 12:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55333110%2fconvert-utc-timestamp-to-local-timestamp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
stackoverflow.com/a/24808474/1042124
– Rahul Kumar
Mar 25 at 7:48
Possible duplicate of Android get Current UTC time
– ADM
Mar 25 at 7:50
@RahulKumar I have edited my question as I need milliseconds only back.
– Gunaseelan
Mar 25 at 8:02