【问题标题】:Convert hour interval to 24h interval将小时间隔转换为 24 小时间隔
【发布时间】:2021-02-16 22:57:57
【问题描述】:

我想将“9:00 am – 11:00 pm”转换为“9:00 - 23:00”,我该怎么做?到目前为止我尝试过的:

if(input.contains("am")) {  //This because I have a string with other kind of items too (not only these interval of hours but names, etc)

    DateFormat df = new SimpleDateFormat("h:mm a – h:mm a");
    DateFormat outputformat = new SimpleDateFormat("HH:mm - HH:mm");
    Date date = null;
    String output = null;
    date= df.parse(input);
    input = outputformat.format(date);
    System.out.println("Output: "+output);

}

但输出错误,在示例中是 23:00 - 23:00,因为它无法识别第一个小时和第二个小时

我也试过类似的东西:

     DateFormat df = new SimpleDateFormat("h:mm a" +" - "+ "h:mm a");
     DateFormat outputformat = new SimpleDateFormat("HH:mm"+" - "+ "HH:mm");

但是我得到了一个解析错误

【问题讨论】:

  • 从外观上看,输出应该是null(因为您打印的是output,它只定义为String output = null),所以请发布一些实际重现问题的内容。
  • 你传递给parse方法的string变量是什么!?
  • 对不起,这不是 date= df.parse(string);但是 date= df.parse(input);如果我在正确获得类型为“9:00 am – 11:00 pm”的字符串列表之后插入一个 println。 if 工作正常 我使用当前代码的输入和输出示例: I/System.out:输入 9:00 am – 11:00 pm I/System.out:输出:23:00 - 23:00 I/System .out:输入:上午 8:00 - 下午 5:00 I/System.out:输出:17:00 - 17:00 I/System.out:输入:上午 4:00 - 下午 12:00 I/系统。出: 输出: 12:00 - 12:00
  • 我建议你不要使用SimpleDateFormatDate。这些类设计不佳且早已过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time APILocalTimeDateTimeFormatter
  • 感谢您的澄清。请将其添加到问题本身而不是评论中,以便我们将所有内容集中在一个地方。

标签: java android string date converters


【解决方案1】:

试试这个:

String dateToParse = "9:00 am – 11:00 pm";
        String splitDate[] = dateToParse.split("–");

        DateFormat df = new SimpleDateFormat("h:mm a");
        DateFormat outputformat = new SimpleDateFormat("HH:mm");
        Date date1 = null, date2 = null;
        String output1 = null, output2 = null;
        try {
            date1 = df.parse(splitDate[0].trim());
            date2 = df.parse(splitDate[1].trim());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        output1 = outputformat.format(date1);
        output2 = outputformat.format(date2);

        System.out.println("Output: " + output1 + " – " + output2);

【讨论】:

  • 谢谢!它工作除了我有 12 点的情况: I/System.out:输入:上午 9:00 – 晚上 11:00 I/System.out:输出:09:00 – 23:00 I/System.out:输入:上午 9:00 – 晚上 11:00 I/System.out:输出:09:00 – 23:00 I/System.out:输入:上午 9:00 – 晚上 11:00 I/System.out:输出: 09:00 – 23:00 I/System.out:输入:上午 8:00 – 下午 5:00 I/System.out:输出:08:00 – 17:00 I/System.out:输入:4:00 am – 12:00 pm I/System.out:输出:04:00 – 12:00 正如您在最后两行中看到的,12 pm 被转换为 12 但它是错误的,它应该是 00:00。上午 12 点改为 12 点。
  • 中午 12:00 是中午,凌晨 12:00 是午夜。 4:00 am – 12:00 am(输出:04:00 – 00:00)和 4:00 am – 12:00 pm(输出:04:00 – 12:00)
【解决方案2】:

面向对象并使用 java.time

不要将区间从一种字符串格式转换为另一种。设计一个Interval 类来存储和操作您的时间间隔。我建议您包含一个接受字符串的便捷构造函数和一个产生所需格式的toString 方法。

我还建议您使用现代 Java 日期和时间 API java.time 来处理您的时间问题。

public class Interval {

    private static final String middlePart = " – ";
    private static final DateTimeFormatter amPmFormatter = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern("h:mm a")
            .toFormatter(Locale.ENGLISH);
    
    private LocalTime begin;
    private LocalTime end;
    
    public Interval(String intervalString) {
        String[] timeStrings = intervalString.split(middlePart);
        if (timeStrings.length != 2) {
            throw new IllegalArgumentException("Improper format");
        }
        begin = LocalTime.parse(timeStrings[0], amPmFormatter);
        end = LocalTime.parse(timeStrings[1], amPmFormatter);
    }
    
    @Override
    public String toString() {
        return "" + begin + middlePart + end;
    }
}

尝试一下:

        Interval testInterval = new Interval("9:00 am – 11:00 pm");
        System.out.println(testInterval);

输出是:

09:00 – 23:00

你的代码出了什么问题?

Date 是用于间隔的完全错误的类。该课程设计不良且早已过时。 date 代表一个时间点,而不是一天中的某个时间,当然也不是一天中两次的间隔。

SimpleDateFormat——一个臭名昭著的麻烦制造者——试图将你的字符串解析为你所在时区的 1970 年 1 月 1 日的时间。它应该反对,因为那天的时间不能同时是上午 9 点和晚上 11 点——你在自相矛盾。但是不,SimpleDateFormat 给了你一个这样的时刻,闭上嘴,假装一切都好。可能是为了表现得很好,但如果是这样,恕我直言,这是一个完全被误解的尝试。

链接

Oracle tutorial: Date Time 解释如何使用 java.time。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-09
    • 2013-10-14
    • 2013-08-27
    • 1970-01-01
    • 2012-12-01
    • 2019-02-17
    • 2015-08-14
    • 2017-04-20
    相关资源
    最近更新 更多