Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about "Date" class

 
Wang Kaiyi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,all!
I am confused about "Date" class;
1. I don't know why there is another "Date" in package "java.sql" other than "java.util.Date".
2. And what's the difference between them?

3. In my eyes, the methods of "Date" such as "getDay" "getHours" is very useful, why it is deprecated?
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
java.sql.Date is a thin wrapper of a millisecond value, which enables JDBC to identify it as a SQL date. In the SQL specification there is no time component of a date value, so the hours, minutes, seconds and milliseconds must be zero. All the methods for getting and setting time e.g getHours are deprecated as they serve no purpose.
java.util.Date is a class that represents a specific moment in time, with millisecond precision. However it's methods for getting and and setting date and time were not particulary conducive to internationalization and the java.util.Calender class should be used to convert between a date and it's various time fields i.e(YEAR, MONTH e.tc). Also the java.util.DateFormat class should be used to format and parse date strings
best regards,
Nigel Browne
SCJP, SCJD
 
Wang Kaiyi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your response!But I have a new problem.
I want a String as such format "2004-02-25 12:05:09".
which class and methods can I use?
I tried to do this as following:
java.util.Date dDate = new java.util.Date(System.currentTimeMillis());
String sDate = dDate.dDate.toLocaleString();
then, I execute my program....
the result:
sDate is "2004-02-25 12:05:09" in Windows Environment.(JBuilder9)
sDate is "Feb 25, 2004 12:05:09 AM" in Linux Environment.
I am very confused about this.
What difference can OS make???

and my Linux Environment is as following:
[ken@test47 consumer]$ java -version
java version "1.3.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)
[ken@test47 consumer]$ uname -a
Linux test47 2.4.18-3 #1 Thu Apr 18 07:37:53 EDT 2002 i686 unknown
 
Zac Nahas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also a question about the Date? I want to find the time between to events so I first use GetDate and then do some processing. Then do another GetDate. How do I find the time between the two events ie How do I subtract time?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can say
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wang:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic