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

  • SEARCH
  • Home
  • 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 9113155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:54:09+00:00 2026-06-17T03:54:09+00:00

I have a problem with GregorianCalendar so if you please can help me out

  • 0

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 2×12 hours or 21465×12 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.

  • 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-17T03:54:10+00:00Added an answer on June 17, 2026 at 3:54 am

    Your date format is wrong…

    You’re using hh, which is a representation of the “Hour in am/pm (1-12)”, so a time of 1pm is been converted to 1am instead.

    You should be using HH which is a a representation of the “Hour in day (0-23)”.

    Either that, or you need supply a date/time format with the am/pm marker…

    Using either DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm"); or DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm a");

    Instead of relying on String date/time values, you should be passing in and back a Date object, leave the formatting for the display.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem. I have this code, and I can`t add session. This is
I have problem, can help me? I have text and I want to replace
I have problem on deleting component which is created on runtime. Please help me.
I have problem with passing variables through views. But, first some code // i
Hello I have a problem with GregorianCalendar. What is wrong in there? How outcome
Have problem with copy constructor and assignment operator. Have written code to multiply matricies:
I have problem deserialize string with JSON.Net on WP7. I have this JSON string:
I have problem with encoding data in Oracle database. I want to xor string
Have problem with this code var MAIN_LOCATION = http://www.bosscaffe.com/new/; $(#gallery_page).click(function() { $('#gallery_photos').show(); getPhotos(); return
I have problem converting String^ containing 8 bytes as characters (as ascii) to double.

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.