Solved

Date function in JAVA

Posted on 2001-08-20
7
7,305 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

759 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

20 Experts available now in Live!

Get 1:1 Help Now