Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:37:10+00:00 2026-06-05T17:37:10+00:00

I get the current time with SimpleDateFormat and I want to return new time

  • 0

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);

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:37:13+00:00Added an answer on June 5, 2026 at 5:37 pm

    Simple do

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -6);
        cal.add(Calendar.MINUTE,-30 );
        System.out.println("Time before 6 hour and 30 min from now is : "+ cal.getTime());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get current time (I need hours, minutes, seconds) crossplatform in c++?
I want to store the current date & time which can be get by
this function is used to get current time of my system, I want to
I am having a quite annoying problem. I want to get the current date/time
I want to get current time in my application. Then i want to add
I want to get the time difference between saved time and current time in
I am trying to get current time in specific time zones. I tried following
I need to get the current time in C# but when I create a
How might I get the current time in milliseconds in C? I am doing
how can I get the current time in hh:mm format? I need to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.