Newsgroup: it.comp.java
Titolo: Come gestire le operazioni con le date?

In un progettino che sto realizzando mi serve effettuare delle sottrazioni
tra date :
per esempio quanti secondi(o minuti, è uguale) ci sono fra le 15.55 del
12/01/2000 e le 14.03 del 15/01/00.
Devo fare tutto a mano (che p...) oppure c'è qualcosa di già fatto?
ho visto le classi Date e Calendar ma non mi sembra ci sia una cosa del
genere.
scusate se ho chiesto una stupidata ma sono all'inizio e mi sto esercitando.
grazie x l'aiuto.
P.S. se non c'è nulla di già fatto, voi come fareste?
--
Ciao by Beppe
Solo chi è stupido non cambia mai idea
(Aldo, Così è la vita)

genere.
Devi usare sia Date che il tempo rappresentato in millisecondi:
Con DateFormat fa il parsing delle stringhe, ottenendo le date
Date d1 = ...data 15:55 12/01/2000...
Date d2 = ...data 14:03 15/01/2000...
long millis = d2.getTime() - d1.getTime();
su cui poi fai i calcoli.
A questo proposito scrissi qualche tempo fa una classe, TimeInterval,
che offre qualche servizio per rappresentare intervalli di tempo, 
che puo'essere facilmente aumentata per fornirti un intervallo espresso 
in minuti/giorni/settimane/etc (scritta, tra l'altro, proprio per il 
robot che invia la FAQ qui :). La granularita' e' - se non ricordo
male - a minuti.
Non sono sicuro di avere sull'hd di casa la versione piu' aggiornata, 
comunque ecco qui:
------------------------------------------------------------------------------
/************************************************************************************
* Copyright (C) 1998 Cristiano Sadun
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
************************************************************************************/
package com.deltax.util;
import java.io.*;
import java.util.*;
public class TimeInterval implements Serializable {
	private static final long _MINUTE	= 6000;
	private static final long _HOUR		= 60*_MINUTE;
	private static final long _DAY		= 24*_HOUR;
	private static final long _WEEK		= 7*_DAY;
	private static final long values [] = { _MINUTE, _HOUR, _DAY, _WEEK	};
 
	private static final int MINUTE_INDEX = 0;
	private static final int HOUR_INDEX   = 1;
	private static final int DAY_INDEX    = 2;
	private static final int WEEK_INDEX   = 3;
	private static final String desc [][] = { 
		{ "minute", "min", "m", "mm" },
		{ "hour", "hr","h","hh" },
		{ "day", "d", "dd" },
		{ "week", "wk", "ww" }
	};
 
	public static final long MINUTE	= values[0];
	public static final long HOUR		= values[1];
	public static final long DAY		= values[2];
	public static final long WEEK		= values[3];
 
	public static final String [] MINUTE_STR = desc[0];
	public static final String [] HOUR_STR	= desc[1];
	public static final String [] DAY_STR = desc[2];		
	public static final String [] WEEK_STR = desc[3];		
 
	public long weeks;
	public long days;
	public long hours;
	public long minutes;
 
	public TimeInterval(long weeks, long days, long  hours, long minutes) {
		this.weeks   =  weeks;  
		this.days    =  days;
		this.hours   =  hours;
		this.minutes =  minutes;
	}
 
	public TimeInterval() {
		this(0,0,0,0);
	}
	/**
	 * Creates a canonical interval, i.e. one in which the amount of time
	 * is distribuited to the possible maximum units. 
	 *
	 * (ie: 25hr are represented as 1day + 1 hr,etc)
	 *	
	 * Since the TimeInterval precision is on the minute - any additional 
	 * time slice will be lost (ie : 6000ms to 11999ms are just 1 minute)
	 *
	 */	
	public TimeInterval(long time)  {
		weeks = time / WEEK;
		days  = (time % WEEK) / DAY;
		hours = ((time % WEEK) % DAY) / HOUR;
		minutes = (((time % WEEK) % DAY) % HOUR) / MINUTE;		
	}
 
	public TimeInterval(String s) throws TimeIntervalFormatException {
		TimeInterval t = parse(s);
		weeks   =  t.weeks;  
		days    =  t.days;   
		hours   =  t.hours;  
		minutes =  t.minutes;
	}
 
	public static TimeInterval parse(String desc) throws TimeIntervalFormatException {
		StringTokenizer st = new StringTokenizer(desc," +,");
 
		TimeInterval ti = new TimeInterval();
 
		while (st.hasMoreElements()) {
			String s = st.nextToken();
 
			// Find coefficient based on desc, exception if not known
			addElementValue(ti, s);
		}
 
		return ti;
	}
 
	public long getTime() { return WEEK*weeks+DAY*days+HOUR*hours+MINUTE*minutes; } 
 
	public String toString() {
		return makeDescr();
	}
 
	public boolean shorter(TimeInterval ti) {
	   return getTime() < ti.getTime();
	}
 
	public boolean longer(TimeInterval ti) {
	   return getTime() > ti.getTime();
	}
 
	private String makeDescr() {
		StringBuffer sb = new StringBuffer();
		if (weeks!=0) {
			sb.append(weeks);
			sb.append(" week");
			if (weeks > 1) sb.append("s");
			if (days!=0 || hours!=0 || minutes!=0) sb.append(",");
		}
 
		if (days!=0) {
			sb.append(days);
			sb.append(" day");
			if (days > 1) sb.append("s");
			if (hours!=0 || minutes!=0) sb.append(",");
		}
 
		if (hours!=0) {
			sb.append(hours);
			sb.append(" hour");
			if (hours > 1) sb.append("s");
			if (minutes!=0) sb.append(",");
		}
 
		if (minutes!=0) {
			sb.append(minutes);
			sb.append(" minute");
			if (minutes > 1) sb.append("s");
		}
		return sb.toString();
	}
 
	private static long addElementValue(TimeInterval ti, String s) throws TimeIntervalFormatException {
		// Cycle thru the descriptions
		s.toLowerCase();
 
		for (int i=0;i<desc.length;i++) 
			for (int j=0;j<desc[i].length;j++) 
				if (s.endsWith(desc[i][j]) || s.endsWith(desc[i][j]+"s")) {
					s=s.substring(0,s.lastIndexOf(desc[i][j]));
 
					long val = values[i];
 
					switch(i) {
						case MINUTE_INDEX: ti.minutes+=values[i]; break;
						case HOUR_INDEX: ti.hours+=values[i]; break;
						case DAY_INDEX: ti.days+=values[i]; break;
						case WEEK_INDEX: ti.weeks+=values[i]; break;
					}
 
					// Get (integer) coefficient
					try {
						long f2=Long.parseLong(s)*val;
						return (long)f2;
					} catch (NumberFormatException e) {
						throw new TimeIntervalFormatException();
					}
				}
		throw new TimeIntervalFormatException();
	}
 
 
}
/************************************************************************************
* Copyright (C) 1998 Cristiano Sadun
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
************************************************************************************/
public class TimeIntervalFormatException extends Exception {
	public TimeIntervalFormatException (String s) {
		super("Format is like <x>WEEKS+<y>DAYS: <"+s+"> is wrong"); 
	}
 
	public TimeIntervalFormatException () {
		super("Format is like <x>MONTHS+<y>WEEKS+<z>DAYS");
	}
}
-- 
God is Real, unless declared Integer.
ObjectZone - 
http://space.tin.it/computer/csadun