Date constructor for long.

S

Sameer

import java.util.*;
import java.text.*;
public class DateDemo1 {
public static void main(String args[]) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.set(2006,7,1);
Date date = cal.getTime();
long longTime = date.getTime();
System.out.println("Long representation of date:"+longTime);
Date temp= new Date(longTime);
Date temp1= new Date(1154417769674);
}
}


Please study the code given above.
If the longTime variable is passed as it is programmatically to Date,
then there is no compilation error.
Date temp= new Date(longTime);

But, if i note it down and pass it like this
Date temp1= new Date(1154417769674);
The error is
DateDemo1.java:13: integer number too large: 1154417769674
Date temp1= new Date(1154417769674);

What may be the problem?
 
M

Manish Pandit

You are supposed to put an L at the end to denote a Long.

Date temp1= new Date(1154417769674L);

-cheers,
Manish
 
B

Bhanu

Sameer said:
import java.util.*;
import java.text.*;
public class DateDemo1 {
public static void main(String args[]) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.set(2006,7,1);
Date date = cal.getTime();
long longTime = date.getTime();
System.out.println("Long representation of date:"+longTime);
Date temp= new Date(longTime);
Date temp1= new Date(1154417769674);
}
}


Please study the code given above.
If the longTime variable is passed as it is programmatically to Date,
then there is no compilation error.
Date temp= new Date(longTime);

But, if i note it down and pass it like this
Date temp1= new Date(1154417769674);
The error is
DateDemo1.java:13: integer number too large: 1154417769674
Date temp1= new Date(1154417769674);

What may be the problem?

The problem is you are using a numeral literal which is by default
treated as integer. U need to append a L after the nubmer(not sure may
be before it). So that it is converted to long.
 
B

Bhanu

Sameer said:
import java.util.*;
import java.text.*;
public class DateDemo1 {
public static void main(String args[]) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.set(2006,7,1);
Date date = cal.getTime();
long longTime = date.getTime();
System.out.println("Long representation of date:"+longTime);
Date temp= new Date(longTime);
Date temp1= new Date(1154417769674);
}
}


Please study the code given above.
If the longTime variable is passed as it is programmatically to Date,
then there is no compilation error.
Date temp= new Date(longTime);

But, if i note it down and pass it like this
Date temp1= new Date(1154417769674);
The error is
DateDemo1.java:13: integer number too large: 1154417769674
Date temp1= new Date(1154417769674);

What may be the problem?

The problem is you are using a numeral literal which is by default
treated as integer. U need to append a L after the nubmer(not sure may
be before it). So that it is converted to long.

I mean pass it like

new Date(1154417769674L);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem about Date 3
Date different 32
Date/Calendar confusion 0
Date/Calendar confusion 8
Date format issue 3
Confused about Date 15
[LONG] java.net.URI encoding weirdness 18
Timezones and versions of Java 25

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top