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 6594207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:46:51+00:00 2026-05-25T17:46:51+00:00

I am trying to get the time of a timestamp but I keep getting

  • 0

I am trying to get the time of a timestamp but I keep getting the wrong time when I use Calendar.HOUR and Calendar.MINUTE,no matter what the timestamp is it tells me the hour is 10 and the minute is 12.

now when I use the Calendar.getTime() it gives me the correct time so I dont understand? I just want to get the hour in 12hr format and the minute

here is how i go about doing it

public static String getRealTime(long time){

            Calendar cal = Calendar.getInstance();
    Log.d("Calendar",String.valueOf(time));
    cal.setTimeInMillis(time);
    Date timeS = cal.getTime();
    String sTime = timeS.toString(); // gives correct time in 24hr format
    int hr = cal.HOUR;               // gives me 10 no matter what the timestamp is
    int min = cal.MINUTE;            // gives me 12 no matter what the timestamp is
    String dMin = getDoubleDigit(min);
    int ampm = cal.AM_PM;
    String m = new String();
    if(ampm == 0){
        m = "AM";
    }else{
        m="PM";
    }
    String rtime = String.valueOf(hr)+":"+dMin+" "+m;
    return rtime;
} 

so say the timestamp is 1316626200000 cal.getTime() gives me Wed Sep 21 13:30:00 EDT 2011 which would be the correct time but cal.HOUR gives me 10 for the hour which clearly is not what it should be. Why is it doing that?

  • 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-05-25T17:46:51+00:00Added an answer on May 25, 2026 at 5:46 pm

    cal.HOUR and cal.MINUTE are static final Integers for use in Calendar method calls. You would use this code to get the correct result:

       int hr = cal.get(Calendar.HOUR);
       int min = cal.get(Calendar.MINUTE);
    

    Notice that I called the HOUR and MINUTE fields from Calendar and not your object cal. It is bad practice to call static members from an instantiated object.

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

Sidebar

Related Questions

What I'm trying to do is to use ajax to get time from another
I'm trying to get a unix timestamp with PHP but it doesn't seem to
I am trying to get the timestamp. This works. final Calendar cal = new
I am trying to get time with microseconds through glib which will work on
I'm trying to get the exact time and date when users select a button,
When I'm trying to get the idle time of the gnome screensaver in seconds,
I'm trying to get the unix time for date strings that are formatted like
When trying to get geolocation on iPhone the first time - I declined. Every
I am trying to get the difference between time which is in the form
I've spent too much time trying to get this to work on IE 7.

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.