Solved

Date function in JAVA

Posted on 2001-08-20
7
7,306 Views
Last Modified: 2008-03-04
the Data in SQL has the format (mm/dd/yy). I want to compare the SQL data with Current System Date in JAVA.
what's the query for that..??

Regards

0
Comment
Question by:rajeevtyagi
7 Comments
 

Author Comment

by:rajeevtyagi
ID: 6406717
do it please..
0
 
LVL 12

Accepted Solution

by:
ahosang earned 25 total points
ID: 6406937
Well,
java.util.Date d=new java.util.Date();
 /* this gives the system date in object form. Now you have choice of using deprecated Date methods or newer Calendar methods, or you could(depending on what you want to compare) just use Calendar to begin with. */
Calendar currentDate=Calendar.getInstance();
GregorianCalendar sqlData=new GregorianCalendar(yy,mm,dd);
if (currentDate.equals(sqlData)) {
  //do whatever
}
if (currentDate.before(sqlData)) {
  // do whatever
}
if (currentDate.after(sqlData)) {
 // do whatever
}

0
 
LVL 3

Assisted Solution

by:sghosh092199
sghosh092199 earned 25 total points
ID: 6407432
Hi!

I suppose, you have got the SQL Date as follows:

String sqlDate = rset.getString(x);

sqlDate has already the form of mm/dd/yy.

You have to convert the current date to the same form. You can use SimpleDateFormat for the same.

java.text.SimpleDateFormat formatter
               = new java.text.SimpleDateFormat ("MM'/'dd'/'yy");

String formattedDate = formatter.format(new java.util.Date());

Now, you can compare sqlDate and formattedDate.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 6408040
If your doing the comparison in Java:

Date d = rs.getDate(datecol);
Date now = new Date();
if (d.equals(now))
{
   System.out.println("Date in db matches current time");
}
else if (d.before(now))
{
   System.out.println("Date in db is before current time");
}
else
{
   System.out.println("Date in db is after current time");
}

0
 
LVL 6

Assisted Solution

by:kotan
kotan earned 25 total points
ID: 6408610
Or you can use compareTo().

// Copy from Objects :)
Date d = rs.getDate(dateCol);
Date now = new Date();

// use compareTo().
int res = d.compareTo(now);

if (res == 0)
     System.out.println("Date in db matches current time");
else if (res < 0)
     System.out.println("Date in db is before current time");
else if (res > 0)  
     System.out.println("Date in db is after current time");
0
 

Expert Comment

by:CleanupPing
ID: 9061828
rajeevtyagi:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 9332749
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between ahosang, sghosh, objects and kotan

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 103
firstswap challenge 20 68
recursion example 16 112
type mismatch (Object[] to double[] 4 13
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now