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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:43:16+00:00 2026-06-18T14:43:16+00:00

I am trying to get a time down to the minute from a Calendar

  • 0

I am trying to get a time down to the minute from a Calendar object built from four inputs: occurrence in month (3rd), day (Thursday), hour (1), minute(30), and am/pm. Also, if the time is in the past the month should increase by one.

How do I set a a Calendar to this time?

This code seems to not set the day. The hour, minute, and am/pm are setting correctly though.

Calendar Setup:

long getTime() {
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, getDayOfWeekInMonth());
    calendar.set(Calendar.DAY_OF_WEEK, getDayOfWeek());
    calendar.set(Calendar.HOUR, getHour());
    calendar.set(Calendar.MINUTE, getMinute());
    calendar.set(Calendar.AM_PM, getAmPm());

    if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
        calendar.add(Calendar.MONTH, 1);
    }

    return calendar.getTimeInMillis();

}

int getDayOfWeekInMonth() {
    return weekOfMonthSpinner.getSelectedItemPosition() + 1;
}

int getDayOfWeek() {
    switch(dayOfWeekSpinner.getSelectedItemPosition()) {
    case 0:
        return Calendar.SUNDAY;
    case 1:
        return Calendar.MONDAY;
    case 2:
        return Calendar.TUESDAY;
    case 3:
        return Calendar.WEDNESDAY;
    case 4:
        return Calendar.THURSDAY;
    case 5:
        return Calendar.FRIDAY;
    case 6:
        return Calendar.SATURDAY;
    default:
        return 0;
    }
}

int getHour() {
    return Integer.parseInt((String) hourSpinner.getSelectedItem());
}

int getMinute() {
    return Integer.parseInt((String) minuteSpinner.getSelectedItem());
}

int getAmPm() {
    switch(amPmSpinner.getSelectedItemPosition()) {
    case 0:
        return Calendar.AM;
    case 1:
        return Calendar.PM;
    default:
        return 0;
    }
}

Spinner Setup:

<string-array name="week_of_month">
    <item>1st</item>
    <item>2nd</item>
    <item>3rd</item>
    <item>4th</item>
 </string-array>
<string-array name="day_of_week">
    <item>Sunday</item>
    <item>Monday</item>
    <item>Tuesday</item>
    <item>Wednesday</item>
    <item>Thursday</item>
    <item>Friday</item>
    <item>Saturday</item>
</string-array>
<string-array name="hour">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
</string-array>
<string-array name="minute">
    <item>00</item>
    <item>05</item>
    <item>10</item>
    <item>15</item>
    <item>20</item>
    <item>25</item>
    <item>30</item>
    <item>35</item>
    <item>40</item>
    <item>45</item>
    <item>50</item>
    <item>55</item>
</string-array>
<string-array name="am_pm">
    <item>am</item>
    <item>pm</item>
</string-array>

adapter = ArrayAdapter.createFromResource(getActivity(), R.array.week_of_month, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
weekOfMonthSpinner.setAdapter(adapter);

adapter = ArrayAdapter.createFromResource(getActivity(), R.array.day_of_week, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
dayOfWeekSpinner.setAdapter(adapter);

adapter = ArrayAdapter.createFromResource(getActivity(), R.array.hour, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
hourSpinner.setAdapter(adapter);

adapter = ArrayAdapter.createFromResource(getActivity(), R.array.minute, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
minuteSpinner.setAdapter(adapter);

adapter = ArrayAdapter.createFromResource(getActivity(), R.array.am_pm, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
amPmSpinner.setAdapter(adapter);
  • 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-18T14:43:18+00:00Added an answer on June 18, 2026 at 2:43 pm

    Usecalendar.set(Calendar.WEEK_OF_MONTH, <week of the month>); and calendar.set(Calendar.DAY_OF_WEEK, 4);.

    Example –

    Calendar calendar = Calendar.getInstance();
    System.out.println("Before - "+calendar.getTime());
    calendar.set(Calendar.WEEK_OF_MONTH, 3);
    calendar.set(Calendar.DAY_OF_WEEK, 4);
    System.out.println("After - "+calendar.getTime());
    

    Result –

    Before - Tue Feb 12 23:23:28 IST 2013
    After - Wed Feb 13 23:18:16 IST 2013
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a report down from the 20+ minute run time and
What I'm trying to do is to use ajax to get time from another
Well I'm having a hell of a time trying to get my CPU down
Trying to get page-load time down. I followed the third example outlined here to
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

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.