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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:09:52+00:00 2026-05-25T20:09:52+00:00

I have in my preferences some strings that represent a start time and and

  • 0

I have in my preferences some strings that represent a start time and and ending time.

I wrote this function to determine if the current time is within the start and ending time. The format of the date strings is “HH:mm”. The function takes the strings that are from the preferences.

I’m sure I’m missing some code for the comparing because my parsing returns a something like this:

Thu Sep 29 12:24:33 EDT 2011

But all I need is to get this:
12:24

Here is the function. Can you help me correct the coding?

Thanks.

Truly,
Emad

public static boolean currentTimeIsWithinStartAndEnd(String startTime,
        String endTime) {

    String pattern = "HH:mm";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    boolean booleanValueToReturn = false;

    try {
        Date startTimeToCompare = sdf.parse(startTime);
        Date endTimeToCompare = sdf.parse(endTime);

        /*
         * These are for the current time in date format.
         */
        Date currentTime = new Date(System.currentTimeMillis());


        Log.w("Emad", "Current Time: " + currentTime + "   Start Time is: "
                + startTimeToCompare + "   End Time is : "
                + endTimeToCompare);



        /*
         * Check if current time is equal or greater than the start time.
         */
        if (currentTime.compareTo(startTimeToCompare) == 0
                || currentTime.compareTo(startTimeToCompare) == 1) {

             booleanValueToReturn = true;

            /*
             * Now check if the current time is equal or less than the end
             * time.
             */
            if (currentTime.compareTo(endTimeToCompare) == 0
                    || currentTime.compareTo(endTimeToCompare) == -1) {

                booleanValueToReturn = true;
            } else {
                 booleanValueToReturn = false;
            }
        } else {
             booleanValueToReturn = false;
        }

    } catch (java.text.ParseException e) {
    }

    return booleanValueToReturn;
  • 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-25T20:09:52+00:00Added an answer on May 25, 2026 at 8:09 pm

    You are using SimpleDateFormat Incorrectly.

    String pattern = “HH:mm” should be format in which your input Date String is. otherwise how is SimpleDateFormat going to know which portion represents what.

    Create two SimpleDateFormat, f1 (with Input String Format) and f2 ( with output String Format) ;

    Use f1.parse() to get Date object for Input String.

    Then use f2.format() on this Date Object to get Output String representation.

    Refer to SimpleDateFormat for details on how to specify date Format.

    public static boolean currentTimeIsWithinStartAndEnd(String startTime,
            String endTime) {
    
               // assuming input date string is of format MM/dd/yyyy. Change it according to your needs. 
    
                String inputPattern = "MM/dd/yyyy";
                String outputPattern = "HH:mm";
                SimpleDateFormat inputFormatter = new SimpleDateFormat(inputPattern);
                SimpleDateFormat outputFormatter = new SimpleDateFormat(outputPattern);
    
                Date startTimeToCompare = inputFormatter.parse(startTime);
                String dateInRequiredFormat = outputFormat.format(startTimeToCompare);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some shared preferences (latitude, longitude) that I want to access from a
I have some preferences in my activity and I would like to achieve that,
I have some questions about the registry. We have Preferences p = Preferences.userRoot(); If
I would like some of my preferences to have icons, like the Settings app.
I have some table: table ASK with idask table PREFERENCES with idpref , fk_idask
I have a problem finding references to this subject and would appreciate some help.
I have other pages within my app preferences which are stored as separate files
I have a table that contains some data given below. It uses a tree
I have some information stored as SharedPreferences. I need to access that information from
So, I have this nice little view that I've made, which basically shows two

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.