Pessoal,
Fiz esse código mas não sei o que pode estar dando errado, pois o mesmo não imprimi a hora correta como deveria ser.
Alguém sabe me dizer o porque não esta funcionando?
/**
*
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?
* @author wagnercarvalho
*
*/
public class DateTnt {
private Integer day;
private Integer month;
private Integer year;
private Integer hour;
private Integer minute;
private Integer minutesDay;
/**
*
* @param date An date as String in the format ?dd/MM/yyyy HH24:mi?
* @param op Can be only ?+? | ?-?
* @param value the value that should be incremented/decremented. It will be expressed in minutes
* @return
*/
public String changeDate(String date, char op, long value) {
disassemblesDate(date);
sum(value);
System.out.println(day + "/" + month + "/" + year + " " + convertMinutesDayForHourAndMinute(this.minutesDay));
return "";
}
public void sum(long value) {
while (value != 0) {
if(minutesDay.equals(1440)) {
minutesDay = 0;
addOneDay();
}
else {
minutesDay++;
}
value--;
}
}
private void addOneDay() {
Month month = Month.getMonth(this.month);
if(this.day == month.getNumDay()) {
this.day = 1;
addOneMonth();
}
else {
this.day++;
}
}
private void addOneMonth() {
if(this.month == 12) {
this.month = 1;
this.year++;
}
else {
this.month++;
}
}
private void disassemblesDate(String dateHour) {
String[] dates = dateHour.split(" ");
if(dates != null) {
if(dates.length == 2) {
String[] date = dates[0].split("/");
this.day = Integer.valueOf(date[0]);
this.month = Integer.valueOf(date[1]);
this.year = Integer.valueOf(date[2]);
String[] hour = dates[1].split(":");
this.hour = Integer.valueOf(hour[0]);
this.minute = Integer.valueOf(hour[1]);
this.minutesDay = (this.hour * 60) + this.minute;
}
}
}
public String convertMinutesDayForHourAndMinute(long minutes) {
long minute = minutes % 60;
long hour = (minutes - minute) / 60;
String m = String.valueOf(minute);
if(minute < 10) {
m = "0" + minute;
}
return hour + ":" + m;
}
public static void main(String[] args) {
DateTnt date = new DateTnt();
date.changeDate("15/01/2007 13:22", '+', 150672);
}
}
Esta dando como resultado 30/4/2007 2:49, quando deveria ser 31/05/2007 04:34
O enum é:
public enum Month {
JANUARY(1, 31),
FEBRUARY(2, 28),
MARCH(3, 31),
APRIL(4, 30),
MAY(5, 31),
JUNE(6, 30),
JULY(7, 31),
AUGUST(8, 31),
SEPTEMBER(9, 30),
OCTOBER(10, 31),
NOVEMBER(11, 30),
DECEMBER(12, 31);
private int num;
private int numDay;
Month(int num, int numDay) {
this.num = num;
this.numDay = numDay;
}
public int getNum() {
return num;
}
public int getNumDay() {
return numDay;
}
public static Month getMonth(int num) {
Month[] values = Month.values();
for (Month item : values) {
if (item.getNum() == num) {
return item;
}
}
return null;
}
}
