I have a problem with GregorianCalendar so if you please can help me out with it. First I'll give you my code:
private String changeClock(String day, String clock, int change) {
String time="";
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm");
try {
Date d=df.parse(day+" "+clock);
GregorianCalendar g=new GregorianCalendar();
g.setTime(d);
g.add(GregorianCalendar.HOUR_OF_DAY, change);
time=g.get(GregorianCalendar.YEAR)+"-"
+(g.get(GregorianCalendar.MONTH)+1)+"-"
+g.get(GregorianCalendar.DAY_OF_MONTH)+" "
+g.get(GregorianCalendar.HOUR_OF_DAY)+":"
+g.get(GregorianCalendar.MINUTE);
} catch (Exception e) {
e.printStackTrace();
}
return time;
}
Let me explain what is happening. I have a GUI with + and - button. When someone press + it add one hour, or if - is pressed then take one hour.
Now example, time is 23:00 and I press +, it is everything ok and it jumps to 00:00 of the next day. Problems are on 12:00. If it is 12:00 and I press + it goes to 1:00 and that goes on and on. It doesn't move to the next day even after 2x12 hours or 21465x12 hours. Moving backward is a little better if I can say so. When it is 00:00 and I press - it changes to yesterday 23:00 (also date changes). If I then press + it changes also one day forward (so to today in this case).
What have I done wrong or what more should I write to my code?
Thanks for your help guys.