Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Date problem

Thread view: 
Mr B - 12 Mar 2007 17:10 GMT
Hi all,

I've been trying in vein to work this out but i simply can't find a
way of doing it.  I'm trying to compare a date stored to the current
date, but I want to find out whether or not a date is 2 days older
than the date now.  in the example below i used a String to store the
Date for compairing but it doesn't matter what I use to store this
information.  I used the CompareTo method but found this only returned
1, 0 or -1.

public static void main(String[] args) throws java.text.ParseException
{

   SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
   java.util.Date now = new java.util.Date();
   String Date1 = "09-03-2007";
   String Date2 = "12-03-2007";

   java.util.Date trade = sdf.parse(Date1);

   System.out.println(trade.compareTo(now));
}

Is there any way of achieving what I need?

Thanks
Daniel
Knute Johnson - 12 Mar 2007 18:29 GMT
> Hi all,
>
[quoted text clipped - 23 lines]
> Thanks
> Daniel

There are the methods Date.before() and Date.after().  But that will
compare to the current time as well.  If it is Tuesday and you want to
know if you date is any time on Saturday you will need to use something
more sophisticated.  Take a look at the Calendar class.  Date has
limited functionality.

Signature

Knute Johnson
email s/nospam/knute/

~Glynne - 12 Mar 2007 18:48 GMT
> Hi all,
>
[quoted text clipped - 24 lines]
> Thanks
> Daniel

What you need is a method that calculates the Julian Day Number, like
so:

 public static int
 YMD_2_JDN( int Y, int M, int D )
 {
   // Converts calendar date components
   // to a Julian Day Number
   int  Jdn;

   // normalize wacky month values
   while( M > 12 ) {
       M -= 12;
       ++Y;
   }
   while( M < 1 ) {
       M += 12;
       --Y;
   }

   //...but wacky day values are okay
   Jdn= D - 32075 + 1461 *
            (Y + 4800 + (M - 14) / 12) /
            4 + 367 * (M - 2 - (M - 14) /
            12 * 12) / 12 - 3 * ((Y + 4900 +
            (M - 14) / 12) / 100) /4;

   return Jdn;
 }

Now you can simply calculate the JDN for each of your dates and
subtract.

~Glynne
ck - 12 Mar 2007 20:47 GMT
> > Hi all,
>
[quoted text clipped - 59 lines]
>
> ~Glynne

Is this simpler? I don't know if this is better.

public static long dayDifference(Date refDate){
 Date today = new Date();
 long diff = today.getTime()- refDate.getTime();
 // one day = 60*60*24*1000 milliseconds
 long inDays = diff/(60*60*24*1000);
 System.out.println(inDays + " days");
 return inDays;
}

You can invoke it like given below

public static void main(String[] args) {
// this is deprecated though
Date date = new Date("03/09/2007");
System.out.println(date);
dayDifference(date);
}

You can also create a date object from GregorianCalendar.

// year,month,day. Month starts from 0 (Jan = 0 Feb =1 mar =2 ...)
Calendar previousDay= new GregorianCalendar(2007,2,3);
// use dayDifference method mentioned above.
dayDifference(previousDay.getTime());

--
Ck
http://www.gfour.net
Jim Bailey - 12 Mar 2007 21:50 GMT
I'm pretty new to this but I've found that working with joda time
http://joda-time.sourceforge.net/
to be pretty easy when doing any kind of date/time math.
For working with database related data i've found using the timestamp class
to be most usefull.

jim

>> Hi all,
>>
[quoted text clipped - 59 lines]
>
> ~Glynne
Real Gagnon - 12 Mar 2007 22:35 GMT
"Mr B" <mrb_16@hotmail.com> wrote in news:1173715818.402000.178940
@j27g2000cwj.googlegroups.com:

> Is there any way of achieving what I need?

Use Calendar.before()/.after()/equals()

See http://www.rgagnon.com/javadetails/java-0104.html
for an example.

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Java, Javascript, VBScript or PowerBuilder snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.