converting time stamp in SECONDS to a calendar date format.. problems

Hello. I'm trying to convert a long interger that represents the number of SECONDS passed since the 1970 date

the number was originally generated by php's mktime() function

one of the numbers I have for example is this : 1126933200

I haven't done the math but this is a date probably around september of 2005.. it's irrelevant anyhow.

I want to convert this to a date format I can use in java. I've tried to create a new date like so :

Date date = new Date(1126933200);

I've also tried it with the calendar method like so :

Calendar calendar = new GregorianCalendar();

calendar.setTimeInMillis(1126933200);

it was only then that I realized that both these methods require the number of MILLI seconds. So I tried doing a simple multiplication by 1000 on my original number and then I started getting a "integer number too large" error when trying to compile.

so i'm kind of at a loss here.. i figure I could make a function myself that would divide my number in days of 86400 seconds.. and then i'd have to keep in mind the bisectal (sp) and the months with more and less days etc.. but i'm sure there must be an easier way.

and i don't get why a method who's meant to receive a very long number is giving me an error when i try to give one to it.

your help is appreciated as always. thanks

[1379 byte] By [vesper8a] at [2007-10-3 5:39:38]
# 1
Because you're trying to do this with an int and not a long.Use a long. If you're stuck with that post your formatted code and we'll see what we can do.
cotton.ma at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 2

long d = 1126933200L;

Date date = new Date(d*1000);

System.out.println(date);

This gives

Sat Sep 17 06:00:00 BST 2005

Bloody hell! The longs have an 'L' suffix which is swallowed by the forum software!

By using double L the forum software displays just one.

Message was edited by:

sabre150

Message was edited by:

sabre150

sabre150a at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 3

thanks.. i guess my error was that i didn't know how to declare a long properly.. i was just doing it like :

long millis = 1126933200;

i figured out another way to make it work though, like this :

GregorianCalendar rightNow = new GregorianCalendar(1970,1,0);

rightNow.add(Calendar.SECOND, 1159156800 );

vesper8a at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 4
For a literal long you need the L at the end of the numberlong mylong = 123456789L;
cotton.ma at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 5

> i figured out another way to make it work though,

> like this :

>

> GregorianCalendar rightNow = new

> GregorianCalendar(1970,1,0);

>rightNow.add(Calendar.SECOND, 1159156800 );

Quite a cludge! Keep on producing cludges like this and your work colleges will learn to hate you!

sabre150a at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 6
The question remains... is it more or less kludgy thenLLStupid Sun.
cotton.ma at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 7

> > i figured out another way to make it work though,

> > like this :

> >

> > GregorianCalendar rightNow = new

> > GregorianCalendar(1970,1,0);

> >rightNow.add(Calendar.SECOND, 1159156800 );

>

> Quite a cludge! Keep on producing cludges like this

> and your work colleges will learn to hate you!

I disagree. It makes the intent of the code more obvious.

jschella at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 8

> Hello. I'm trying to convert a long interger that

> represents the number of SECONDS passed since the

> 1970 date

>

> the number was originally generated by php's mktime()

> function

>

> one of the numbers I have for example is this :

> 1126933200

>

> I haven't done the math but this is a date probably

> around september of 2005.. it's irrelevant anyhow.

>

> I want to convert this to a date format I can use in

> java. I've tried to create a new date like so :

>

> Date date = new Date(1126933200);

>

> I've also tried it with the calendar method like so

> :

>

> Calendar calendar = new GregorianCalendar();

> calendar.setTimeInMillis(1126933200);

>

> it was only then that I realized that both these

> methods require the number of MILLI seconds. So I

> tried doing a simple multiplication by 1000 on my

> original number and then I started getting a "integer

> number too large" error when trying to compile.

So you needed to add L to one of the numbers.

Note you also need to define what timezone that seconds number represents.

With Gregorian it is going to use the local timezone (the code here will anyways). The code using Date is using the UTC timezone.

jschella at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 9

How about import java.util.Date;

class dateConst {

public static void main(String[] argv) {

Date d = new Date(1126933200 * 1000);

System.out.println(d);

}

}

... or am I missing something here?

Keith.

corlettka at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...
# 10
> > ... or am I missing something here?Reply #2!
sabre150a at 2007-7-14 23:47:21 > top of Java-index,Java Essentials,Java Programming...