Solved

Date function in JAVA

Posted on 2001-08-20
7
7,315 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

626 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