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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:51:55+00:00 2026-05-22T23:51:55+00:00

Using the calendar class to determine AM or PM times. Calendar c = Calendar.getInstance();

  • 0

Using the calendar class to determine AM or PM times.

            Calendar c = Calendar.getInstance(); 
            int seconds = c.get(Calendar.SECOND);
            int minutes = c.get(Calendar.MINUTE);
            int hours = c.get(Calendar.HOUR);
            int years = c.get(Calendar.YEAR);
            int months = 1 + c.get(Calendar.MONTH);
            int days = c.get(Calendar.DAY_OF_MONTH);
            int AM_orPM = c.get(Calendar.AM_PM);

            try{
                if (hours < 12)
                {
                    String PM = "";
                    if (AM_orPM == 1)
                    {
                        PM = "PM";
                    }
                    timestamp.setText("Refreshed on " + months + "-"
                    + days + "-" + years + " " +  hours + ":" + minutes + ":" + seconds + " " + PM);
                    timestamp.setTextSize(17f);
                    timestamp.setTextColor(Color.GREEN);
                }
                else if (hours > 12)
                {
                    String AM = "";
                    if (AM_orPM == 0)
                    {
                        AM = "AM";
                    }
                    hours = hours - 12;
                    timestamp.setText("Refreshed on " + years + "-"
                    + months + "-" + days + " " +  hours + ":" + minutes + ":" + seconds + AM);
                    timestamp.setTextSize(17f);
                    timestamp.setTextColor(Color.GREEN);
                }
            }
            catch (Exception e){} 

I want to set the time to AM or PM depending on the current time.
also for some reason the Calendar.MONTH value doesn’t give me the correct month. It’s off by one so thats why I had to add 1. Just wondering if thats normal?

int months = 1 + c.get(Calendar.MONTH);
  • 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-22T23:51:55+00:00Added an answer on May 22, 2026 at 11:51 pm

    It is normal. Because the index of the Calendar.MONTH starts from 0. So that why you need +1 to get the correct Month.

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

Sidebar

Related Questions

In Python, using calendar.timegm(), I get a 10 digit result for a unix timestamp.
I'm using the CodeIgniter calendar class to generate an availability calendar. I use a
I have the following class class X { private List<Calendar> calendars; // public int
I'm having a strange issue with abstract Calendar class using the GregorianCalendar method. For
In my project I am using com.toedter.calendar.JCalendar class. But I do not know how
I have a class that extends Calendar for supporting Jalali calendar. Currently I'm using
I’m building a availability calendar using the CodeIgniter’s calendar class. I’m generating checkboxes for
I am using Calendar Class in my android app to calulate day of year
I m Using a calendar.js file to implement calendar in my App. Actually this
How to create a calendar using XML

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.