Solved

Date function in JAVA

Posted on 2001-08-20
7
7,307 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse java  build path 6 57
iterator/ListIterator approach 17 27
How to convert from xls to xlsx using java 7 39
pagenation logic how it is working in my code 1 30
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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