Classe TestData

3 respostas
celosilveira
//You should create the following function:

//

//public String changeDate(String date, char op, long value);

//

//Where,

//date: An date as String in the format ?dd/MM/yyyy HH24:mi?;

//op: Can be only ?+? | ?-?;

//value: the value that should be incremented/decremented. It will be expressed in minutes.

//

//Restrictions:

//- you shall not work with non-native classes / libraries;

//- you shall not make use of neither Data nor Calendar classes;

//- if the op is not valid an exception must be thrown;

//- if the value is smaller than zero, you should ignore its signal;

//- if the result sum is bigger than max value to the field, you should increment its immediate bigger field;

//- ignore the fact that February have 28/29 days and always consider only 28 days;

//- ignore the daylight save time rules.

//

//Example:

//changeDate(?15/01/2007 13:22?, ?+?, 150672) = ?31/05/2007 04:34?

public class TestDate {

public static void main (String[]args) {
	
	TestDate d = new TestDate();
	
	String date = d.changeDate("29/01/2013 19:22", '+', 1440);
	System.out.println(date);
	
}

public String changeDate(String date, char op, long value) {
	String fullDate = null;
	int day = Integer.parseInt(date.substring(0,2));
	int month = Integer.parseInt(date.substring(3,5));
	int year = Integer.parseInt(date.substring(6,10));
	int hour = Integer.parseInt(date.substring(11,13));
	int minute = Integer.parseInt(date.substring(14,16));
	String auxValue = String.valueOf(value);
	if (auxValue.substring(0,1).equals("-")) {
		auxValue = auxValue.substring(1);
		value = Long.valueOf(auxValue);
	}
	
	try {
		if (op == '+') {
			if (value >= 60) {
				long auxHour = value / 60;
				long auxMinute = value % 60;
				hour+=auxHour;
				minute+=auxMinute;
				if (minute >= 60) {					
					hour++;
					minute-= 60; ;  
				} 
				if (hour >= 24) {
					int auxDays = hour / 24;
					int auxHourDay = hour % 24;
					hour=auxHourDay;
					day+= auxDays;
				}
				if (day > 60) {
					int auxMonth = day / 30;
					int auxDayMonth = day % 30;
					day = Integer.parseInt(date.substring(0,2));
					month+= auxMonth;
					day+=auxDayMonth;
					if (day > 30) {
						day-=30;
					}
				}
				if (day > 30 && day < 60) {
					day-=30;
					month++;
				}
			} else if (value <= 60) {
				minute+= value;
			}
		} else if (op == '-') {
			long auxHour = value / 60;
			long auxMinute = value % 60;
			hour-=auxHour;
			minute-=auxMinute;
			if (minute <= 0) {					
				hour--;
				minute+= 60; ;  
			}
			if (auxHour >= 24) {
				long auxDays = auxHour / 24;
				if (hour >= -24) {
					hour+=24;
				} else if (hour <= -24) {
					hour+=auxHour;
				}
				day-= auxDays;
			}
			
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	String dayMonth = day<10?"0"+String.valueOf(day):String.valueOf(day);
	String monthYear = month<10?"0"+String.valueOf(month):String.valueOf(month);
	String hourDay = hour<10?"0"+String.valueOf(hour):String.valueOf(hour);
	String minuteHour = minute<10?"0"+String.valueOf(minute):String.valueOf(minute);
	
	fullDate = dayMonth +"/"+ monthYear +"/"+ String.valueOf(year) +" "+ hourDay +":"+ minuteHour;
			
	return fullDate;
}

}

3 Respostas

pvrsouza

E dai? :smiley:

celosilveira

Claro esqueci da pergunta. Teria como fazer o cálculo de minutos de uma forma mais rápida?

pvrsouza

De uma olhada nisso aqui:

Calendar now = Calendar.getInstance(); System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE));

Olha a documentação aqui:
http://java.sun.com/javase/6/docs/api/java/util/Calendar.html

Abraços.

Criado 10 de janeiro de 2013
Ultima resposta 10 de jan. de 2013
Respostas 3
Participantes 2