File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes one question about the Date Class! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "one question about the Date Class!" Watch "one question about the Date Class!" New topic
Author

one question about the Date Class!

Hiep Nguyen
Ranch Hand

Joined: Oct 26, 2001
Posts: 46
i new an instance of Data clase, and pass for it a long number. when i print it, it print : "January 1, 1970, 00:00:00 GMT". i want to print the date the the long number contain. but i don't know how to get it
can you help me?
thank you!
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hiep,
The Date constructor that takes a long

'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.'
So if you construct a date object with this constructor and if you want to get the same long printed you should be using the method getTime() on the object created.
Hope this helps
Marilyn
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Can you give us some code that demonstrates what you're doing? It sounds like the long value that you're using to set the Date, is 0. What happens if you try using a different value for the long?
Take a look at this code to see how it works:


"I'm not back." - Bill Harding, Twister
Hiep Nguyen
Ranch Hand

Joined: Oct 26, 2001
Posts: 46
this is the code that i test

anh this is the result when i run the code
[result]
Jan 12, 1970 5:12:01 AM
11 Jan 1970 22:12:01 GMT
Mon Jan 12 05:12:01 GMT+07:00 1970
[/result]
thank you for your replies!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
What version of JDK are you using? When you compiled, did you notice any warning messages? (Never ignore warning messages.)
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hiep,
So did you try adding this statement to get the long value?
System.out.println(new java.util.Date(943921260).getTime());
Marilyn
Hiep Nguyen
Ranch Hand

Joined: Oct 26, 2001
Posts: 46
i'm using jdk1.3
yes, it has these warning

so what is my mistake?
thank you so much !
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Oops, false alarm - I saw that there were warnings, and without looking further I assumed that the deprecated methods were the source of your trouble. Turns out that's not really the case. You should still avoid deprecated methods though. If we follow the instructions:
Note: Recompile with -deprecation for details
That means compile like this:

(plus any other compiler flags you wish to use). This gives you additional information:

Basically this tells us exactly which methods are considered incorrect to use. Typically you can look in the API for these methods, but in this case there's not a lot of information there. The short answer is, don't use these methods; they'll confuse us. Use toString(), or use a DateFormat object instead.
Anyway, that's probably just a distraction I think from your original question. You originally said:
i new an instance of Data clase, and pass for it a long number. when i print it, it print : "January 1, 1970, 00:00:00 GMT"
Now in your last code shown, that's no longer true - you are getting a different time, Mon Jan 12 05:12:01 GMT+07:00 1970. (Also expressed in several other formats; all equivalent.) Looks like a perfectly good result to me. So, does this mean that your original question has been answered to your satisfaction? Were you expecting some other result?
Hiep Nguyen
Ranch Hand

Joined: Oct 26, 2001
Posts: 46
thank you!
but these are not the right result as you can see, if you pass any value it allways has results look like that. i also use DateFormat class. however, i have these same results.
this is my code

import java.util.Date;
import java.text.DateFormat;
import java.util.Locale;
public class Test {
public static void main (String args[]) {
Date date = new Date(943921260L);
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT,Locale.ENGLISH);

System.out.println(format.format(date));
}
}

and this is the result

1/12/70

the right result mus has year 1999
thank you one more !
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
but these are not the right result as you can see
No, actually I don't see what's wrong with these results.
When you pass in a long value of 943921260, that represents a date/time exactly 943921260 milliseconds after 12:00 A.M. GMT on Jan 1, 1970. That's 943921.260 seconds, or 15732 minutes, or 262 hourse, or 10.9 days. Which takes you to late Jan 11 using GMT, which is early Jan 12 in some time zones. That's what you're getting.
I guess the question is, where did the number 943921260 come from, and what sort of date were you expecting to get from it?
 
wood burning stoves
 
subject: one question about the Date Class!