Date problem

M

Mr B

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
 
K

Knute Johnson

Mr said:
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

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.
 
G

~Glynne

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

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
 
C

ck

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);


Is there any way of achieving what I need?
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

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());
 
J

Jim Bailey

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

~Glynne said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top