Convert from Unix Timestamp to date in Groovy
I have a date in unix timestamp and I want to convert it to human readable...
def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"
This gives me..
clean date 49064-02-13
where I was expecting "2017-02-03".
What am I doing wrong?
I even casted the timestamp to a long explicitly as suggested in this answer.
date groovy
add a comment |
I have a date in unix timestamp and I want to convert it to human readable...
def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"
This gives me..
clean date 49064-02-13
where I was expecting "2017-02-03".
What am I doing wrong?
I even casted the timestamp to a long explicitly as suggested in this answer.
date groovy
Try this answer stackoverflow.com/questions/47403542/…
– IamVickyAV
Nov 21 '18 at 6:41
add a comment |
I have a date in unix timestamp and I want to convert it to human readable...
def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"
This gives me..
clean date 49064-02-13
where I was expecting "2017-02-03".
What am I doing wrong?
I even casted the timestamp to a long explicitly as suggested in this answer.
date groovy
I have a date in unix timestamp and I want to convert it to human readable...
def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"
This gives me..
clean date 49064-02-13
where I was expecting "2017-02-03".
What am I doing wrong?
I even casted the timestamp to a long explicitly as suggested in this answer.
date groovy
date groovy
asked Nov 21 '18 at 1:15
AbuMariamAbuMariam
1,04062340
1,04062340
Try this answer stackoverflow.com/questions/47403542/…
– IamVickyAV
Nov 21 '18 at 6:41
add a comment |
Try this answer stackoverflow.com/questions/47403542/…
– IamVickyAV
Nov 21 '18 at 6:41
Try this answer stackoverflow.com/questions/47403542/…
– IamVickyAV
Nov 21 '18 at 6:41
Try this answer stackoverflow.com/questions/47403542/…
– IamVickyAV
Nov 21 '18 at 6:41
add a comment |
2 Answers
2
active
oldest
votes
Don't multiply by 1000 - new Date(long) is in millisecs not microsecs
add a comment |
There is default way to convert Unix time stamp to Java date:
import java.time.Instant
import java.util.Date
def unixTimeStamp = 1486146877214
Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))
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%2f53403959%2fconvert-from-unix-timestamp-to-date-in-groovy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't multiply by 1000 - new Date(long) is in millisecs not microsecs
add a comment |
Don't multiply by 1000 - new Date(long) is in millisecs not microsecs
add a comment |
Don't multiply by 1000 - new Date(long) is in millisecs not microsecs
Don't multiply by 1000 - new Date(long) is in millisecs not microsecs
edited Nov 21 '18 at 22:22
answered Nov 21 '18 at 5:01
Michael RutherfurdMichael Rutherfurd
10.1k42235
10.1k42235
add a comment |
add a comment |
There is default way to convert Unix time stamp to Java date:
import java.time.Instant
import java.util.Date
def unixTimeStamp = 1486146877214
Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))
add a comment |
There is default way to convert Unix time stamp to Java date:
import java.time.Instant
import java.util.Date
def unixTimeStamp = 1486146877214
Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))
add a comment |
There is default way to convert Unix time stamp to Java date:
import java.time.Instant
import java.util.Date
def unixTimeStamp = 1486146877214
Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))
There is default way to convert Unix time stamp to Java date:
import java.time.Instant
import java.util.Date
def unixTimeStamp = 1486146877214
Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))
answered Nov 21 '18 at 7:10
Evgeny SmirnovEvgeny Smirnov
2,0361517
2,0361517
add a comment |
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%2f53403959%2fconvert-from-unix-timestamp-to-date-in-groovy%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
Try this answer stackoverflow.com/questions/47403542/…
– IamVickyAV
Nov 21 '18 at 6:41