Quantcast

converting Time in Seconds to Date

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

converting Time in Seconds to Date

toddgeist
Hello,

I have the number of seconds since 1/1/1970 - 1275491109

I want to convert that to more readable date. Here is what I have come up with so far.

dateInt = 1275491109
t = dateInt.toLong()  // convert to Long
t = t.multiply(1000);  //convert seconds to milliseconds
def tz= TimeZone.'default'
t = t.minus(tz.rawOffset) // deal with the timeZone offset
new Date(t).toString()  //get a string back

-->Wed Jun 02 16:05:09 PDT 2010

So this seems to work, but I am pretty noobish, so I am wondering if I am missing anything.  Is there a better way to do this?

Thanks

Todd
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: converting Time in Seconds to Date

Chris Broadfoot
Just...

dateInt = 1275491109
new Date(dateInt * 1000)

read the docs for Java Date...


Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

You don't need to muck with TimeZone.

On Mon, Nov 29, 2010 at 1:51 AM, toddgeist <[hidden email]> wrote:

Hello,

I have the number of seconds since 1/1/1970 - 1275491109

I want to convert that to more readable date. Here is what I have come up
with so far.

dateInt = 1275491109
t = dateInt.toLong()  // convert to Long
t = t.multiply(1000);  //convert seconds to milliseconds
def tz= TimeZone.'default'
t = t.minus(tz.rawOffset) // deal with the timeZone offset
new Date(t).toString()  //get a string back

-->Wed Jun 02 16:05:09 PDT 2010

So this seems to work, but I am pretty noobish, so I am wondering if I am
missing anything.  Is there a better way to do this?

Thanks

Todd
--
View this message in context: http://groovy.329449.n5.nabble.com/converting-Time-in-Seconds-to-Date-tp3283395p3283395.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: converting Time in Seconds to Date

toddgeist
Thanks for responding :>)

but I either I am still missing something or you need to muck about with the TimeZone.

if run this in the groovyConsole

groovy> dateInt = 0
groovy> new Date(dateInt * 1000)
 
Result: Wed Dec 31 16:00:00 PST 1969

That is off by exactly the offset in my default timeZone.  I am in PST.

I am running groovy 1.7.5



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: converting Time in Seconds to Date

toddgeist
or heck even simpler...

groovy> new Date(0);
 
Result: Wed Dec 31 16:00:00 PST 1969
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: converting Time in Seconds to Date

tim yates-2
What about:

dateInt = 1275491109
use( groovy.time.TimeCategory ) {
  new Date() + dateInt.seconds
}

On 28 November 2010 16:14, toddgeist <[hidden email]> wrote:

or heck even simpler...

groovy> new Date(0);

Result: Wed Dec 31 16:00:00 PST 1969
--
View this message in context: http://groovy.329449.n5.nabble.com/converting-Time-in-Seconds-to-Date-tp3283395p3283468.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: converting Time in Seconds to Date

tim yates-2
Sorry:

dateInt = 1275491109
theDate = use( groovy.time.TimeCategory ) {
  new Date() + dateInt.seconds
}
println theDate

Tim

On 29 November 2010 08:03, Tim Yates <[hidden email]> wrote:
What about:

dateInt = 1275491109
use( groovy.time.TimeCategory ) {
  new Date() + dateInt.seconds

}

On 28 November 2010 16:14, toddgeist <[hidden email]> wrote:

or heck even simpler...

groovy> new Date(0);

Result: Wed Dec 31 16:00:00 PST 1969
--
View this message in context: http://groovy.329449.n5.nabble.com/converting-Time-in-Seconds-to-Date-tp3283395p3283468.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: converting Time in Seconds to Date

tim yates-2
For the record, I am an idiot:

dateInt = 1275491109
theDate = use( groovy.time.TimeCategory ) {
  new Date( 0 ) + dateInt.seconds
}
println theDate

Needed to call new Date() with 0 as the parameter (I think)

Tim

On 29 November 2010 08:04, Tim Yates <[hidden email]> wrote:
Sorry:

dateInt = 1275491109
theDate = use( groovy.time.TimeCategory ) {
  new Date() + dateInt.seconds
}
println theDate

Tim


On 29 November 2010 08:03, Tim Yates <[hidden email]> wrote:
What about:

dateInt = 1275491109
use( groovy.time.TimeCategory ) {
  new Date() + dateInt.seconds

}

On 28 November 2010 16:14, toddgeist <[hidden email]> wrote:

or heck even simpler...

groovy> new Date(0);

Result: Wed Dec 31 16:00:00 PST 1969
--
View this message in context: http://groovy.329449.n5.nabble.com/converting-Time-in-Seconds-to-Date-tp3283395p3283468.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





Loading...