# Zeitspanne ausrechnen?



## Freak2k (29. April 2005)

Hallo,

ich habe 2 Zeiten als strings in der Form "17:45" vorliegen.
Diese befinden sich beide am selben Tag.
Wie errechne ich die vergangende Zeit in Minuten?

danke


----------



## OliverT (29. April 2005)

Hi,

versuchs mal so:


```
/*
 * Created on 29.04.2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package de;

import java.util.GregorianCalendar;

/**
 * @author OliverT
 */
public class DateConverter {

	private GregorianCalendar gTime1;
	private GregorianCalendar gTime2;
	
	public DateConverter(){

	}
	
	public void init(){
		
		//Konstruktor GregorianCalendar
		 //(int year, int month, int date, int hour, int minute, int second)
		
		gTime1 = new GregorianCalendar(2005,5,1,17,00,00);
		gTime2 = new GregorianCalendar(2005,5,1,17,45,00);
						
		
		long millisPerMinute = 1000 * 60;
		long millisPerHour = millisPerMinute * 60;

		long time2 = gTime2.getTimeInMillis();
		long time1 = gTime1.getTimeInMillis();
		long timeDiff = time2 - time1;
		long hours = timeDiff/millisPerHour;
		long minutes = (timeDiff%millisPerHour)/millisPerMinute;
		StringBuffer buf = new StringBuffer(" ");
		if (hours > 0)
		  buf.append(hours).append(" Std.");
		if (minutes > 0)
		  buf.append(", ").append(minutes).append(" Min.");
	     
		System.out.println(buf.toString());
		
	}
	
	public static void main(String[] args) {
		
		new DateConverter().init();
		
	}
}
```


----------



## Thomas Darimont (29. April 2005)

Hallo!

Schau mal hier:

```
/*
 * Created on 29.04.2005@09:52:25 by Darimont
 *
 * TODO Licence info
 */
package de.tutorials;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Darimont
 * 
 * TODO Explain me
 */
public class StringTimeDifference {

	public static void main(String[] args) throws Exception {
		String time0 = "17:45";
		String time1 = "19:15";
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
		Date d0 = sdf.parse(time0);
		Date d1 = sdf.parse(time1);

		GregorianCalendar gc0 = new GregorianCalendar();
		gc0.setTime(d0);

		GregorianCalendar gc1 = new GregorianCalendar();
		gc1.setTime(d1);

		gc1.add(Calendar.HOUR_OF_DAY, -gc0.get(Calendar.HOUR_OF_DAY));
		gc1.add(Calendar.MINUTE, -gc0.get(Calendar.MINUTE));

		System.out.println(gc1.get(Calendar.HOUR_OF_DAY) + ":"
				+ gc1.get(Calendar.MINUTE));
	}
}
```

Gruß Tom


----------



## Freak2k (29. April 2005)

Danke an euch!


----------

