开发者

How do you compare the time portion of a time string?

开发者 https://www.devze.com 2023-04-09 06:35 出处:网络
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 da

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: " + curre开发者_StackOverflow社区ntTime + "   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;


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号