aspose file tools*
The moose likes Beginning Java and the fly likes Visual J++, calculate days difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Visual J++, calculate days difference" Watch "Visual J++, calculate days difference" New topic
Author

Visual J++, calculate days difference

Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
I need to pull a date out of a database (so it could be retrieved as a Time, a Date, whatever) and calculate how many days between then & now. In VB we had DaysDiff method but it doesn't exist in J++. Any ideas? Visual J++ is restricted to Java 1.1 (yes, you can force it to use a newer version but that wouldn't be acceptable in this course).
Thanks in advance if someone can help.
Jon Huhtala
Greenhorn

Joined: Apr 10, 2002
Posts: 17
Here is a portion of an applet assignment I gave my students to help them calculate the number of days between two dates using J++. It should give you some ideas:
Create a class named MyCalendar that extends GregorianCalendar for the purpose of accessing the protected getTimeInMillis( ) method. The code for this class is as follows:
import java.util.*;
public class MyCalendar extends GregorianCalendar
{
public MyCalendar()
{
super();
}
public long toMillis()
{
return getTimeInMillis();
}
}
To calculate and display information about a particular date in the future or in the past (as far back as January 1, 1970), your applet must contain a text field in which the user may enter the target date as a String in English. You will need to convert this String to a MyCalendar object in order to perform date processing. For example, the following code could be used (assuming the reference of the text field object is targetDateField):
DateFormat localDate = DateFormat.getDateInstance(DateFormat.SHORT,Locale.US);
MyCalendar targetDate = new MyCalendar();
try
{
targetDate.setTime(localDate.parse(targetDateField.getText().trim()));
}
catch (ParseException err)
{
}
This permits the date string to be entered in MM/DD/YYYY format (such as "10/15/1978"). The try and catch are needed to satisfy the compiler.
Calculating the number of days between two dates can be challenging when they are in different years. To do so, you need to convert each date into milliseconds, subtract to obtain their difference in milliseconds, then divide the result by the number of milliseconds in one day ( 24*60*60*1000 ). Be sure to use the toMillis( ) method of MyCalendar.
Hope this helps...


- Jon Huhtala, SCPJ2<p> "Nothing worthwhile is ever easy..."
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
OH YES! Thank you, that works. Unfortunately it took me an hour wondering why it didn't work before I noticed you specified a date as mm/dd/yyyy NOT mm.dd.yyyy. Opening my eyes to that made all the difference.
Chris Pocock
Greenhorn

Joined: Mar 11, 2002
Posts: 13
Originally posted by Elouise Kivineva:
Visual J++ is restricted to Java 1.1

So...MS Visual J++ doesn't let you use the lastest version of Java?? Wouldn't that completely screw you?


<i>I'm currently reading 'How to program Java' by Deitel</i><br /><b>I am still learning, so be kind. I also know C++, by the way</b>
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
It is often frustrating. But J++ does let one create forms & manipulate database information (esp. from the machines own hard drive) VERY eaisily using microsoft's foundation classes (MFC). We're using it because the teacher is a VB expert ordered to teach Java but I think calling this java is really a streching. This is 50% MFC, 30% intro-level programming (the part that really doesn't depend on which language you use) and maybe 20% java if that much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Visual J++, calculate days difference
 
Similar Threads
date manipulation
birthdate
birthdate
difference in days
finding days of two dates in jsp page