Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

one question about the Date Class!

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Hiep Nguyen
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of JDK are you using? When you compiled, did you notice any warning messages? (Never ignore warning messages.)
 
Marilyn Monickam
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm using jdk1.3
yes, it has these warning

so what is my mistake?
thank you so much !
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic