• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Visual J++, calculate days difference

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Elouise Kivineva
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Elouise Kivineva
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic