Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date function in JAVA

Posted on 2001-08-20
7
Medium Priority
?
7,323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Author Comment

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

Accepted Solution

by:
ahosang earned 100 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 100 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Assisted Solution

by:objects
objects earned 100 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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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