I get the current time with SimpleDateFormat and I want to return new time which is 6 hours and 30 minutes earlier. I tried to do it with Calendar but it is returning negative values for minutes if they are less than 30. So my question is if this can be done to return possitive number when neccesary to borrow from the hours or days. Here is the code. I use eclipse and the program is compiling and running.
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DifferenceInHours_04
{
public static void main(String[] args)
{
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz" );
System.out.println(sdf.format(date));
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE );
int second = cal.get(Calendar.SECOND);
System.out.println("Current time: " + hour + ":" + minute +":" + second);
int newHour = hour - 6;
int newMinute = minute - 30;
System.out.println("Modified time: " + newHour + ":" + newMinute + ":"+ second);
}
}
Simple do