Subtracting dates

S

Stevens

Dear all,

How can I subtract two dates formatted like '01/04/03 12:02' and '01/04/03
11:38' and find their difference in minutes.

Cheers!
 
E

Eugene Rosenzweig

auto87829 said:
Dear all,

How can I subtract two dates formatted like '01/04/03 12:02' and '01/04/03
11:38' and find their difference in minutes.

Cheers!

Here is some code. Nothing that couldn't be gleaned from Java API docs.

cheers,
Eugene.

------------

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class DateSubstract {

public static void main( String[] args )
{
new DateSubstract();
}

Date readDate(BufferedReader in)
{
SimpleDateFormat df = new SimpleDateFormat();
Date d = null;
while ( d == null ) {
String line = null;
while ( line == null )
{
System.out.println("Enter date: ");
try {
line = in.readLine();
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
}
try {
d = df.parse(line);
} catch (ParseException pe) {
System.out.println("Exception caught:\n"+pe);
}
}
return d;
}

public DateSubstract()
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
Date d1 = readDate(in);
Date d2 = readDate(in);

System.out.println("difference between dates in minutes: "
+ ( d2.getTime() - d1.getTime() ) / 60000 );
}
}
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top