如何比较时间字符串的时间部分?

发布于 2024-12-07 14:06:11 字数 1766 浏览 1 评论 0原文

我的偏好中有一些代表开始时间和结束时间的字符串。

我编写了这个函数来确定当前时间是否在开始和结束时间之内。日期字符串的格式为“HH:mm”。该函数采用来自首选项的字符串。

我确信我缺少一些用于比较的代码,因为我的解析返回类似这样的内容:

Thu Sep 29 12:24:33 EDT 2011

但我需要的只是得到这个: 12:24

这是函数。你能帮我纠正编码吗?

谢谢。

确实, 埃马德

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2024-12-14 14:06:12

您错误地使用了 SimpleDateFormat。

字符串模式=“HH:mm”应该是输入日期字符串的格式。否则 SimpleDateFormat 如何知道哪个部分代表什么。

创建两个SimpleDateFormat,f1(具有输入字符串格式)和f2(具有输出字符串格式);

使用 f1.parse() 获取输入字符串的日期对象。

然后在此日期对象上使用 f2.format() 来获取输出字符串表示形式。

有关如何使用的详细信息,请参阅 SimpleDateFormat指定日期格式。

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文