I want to increment an hour to the current time and also get the date in UTC. I tried to use Calender class but that only increments the hour but doesn't give the Date in UTC timezone, on the other hand, SimpleDateFormat sets the timezone but doesn't increment an hour. How to do it?
public static void main(String[] args)
{
Date currentDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, 1);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
simpleDateFormat.format(calendar.get(Calendar.HOUR));
String dateOfDeletion = simpleDateFormat.format(new Date()).toString();
System.out.println(currentDate) ;
System.out.println(dateOfDeletion);
System.out.print(calendar.getTime());
}
Output:
Mon Jun 26 14:05:56 IST 2023
Mon Jun 26 08:35:56 UTC 2023
Mon Jun 26 15:05:56 IST 2023
I want the date to be in UTC with 1 hour incremented from the current time.