3

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?

tyczj
  • 71,600
  • 54
  • 194
  • 296

2 Answers2

13

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.

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
2

The great and almighty Android Reference page to the rescue!!! :D http://developer.android.com/reference/java/util/Calendar.html

So, here's the lowdown on why some of those things aren't returning the results you are expecting. First off, the Calendar.HOUR is not a reference to the current hour. First hint at that is the fact that it is in all caps, which by Java convention means that this is a constant (aka final) field. If you are developing in Eclipse it probably brought up a warning saying that you should probably reference the static variable with the class name Calendar instead of using the instance cal. Second hint: the reference page said so! ;)

Well, what should you do with the Calendar.HOUR then? That is a static constant so that you can use the cal.get() to find out. (see the reference page http://developer.android.com/reference/java/util/Calendar.html#get(int))

But! There is an easier way. The code that you might be looking for could be something like this:

public static String getRealTime(long time){
    return new SimpleDateFormat("HH:mm").format(new Date(time));
    //if you'd rather have the current time, just use new Date() without the time as a parameter
} 

Another user has asked for a sorta similar things and there are a few other implementations on this page Display the current time and date in an Android application

Community
  • 1
  • 1
Bob
  • 1,219
  • 1
  • 14
  • 28