[英]Java String split not working as expected using a colon (“:”)
I am trying to remove the seconds off of this string: 我正在尝试删除此字符串的秒数:
10/31/2009 9:46:16 AM
I'm attempting using the following function to do this: 我正在尝试使用以下功能来做到这一点:
public static String correctValue4(String str) {
String parts[] = str.split(":");
String fixedStr = parts[0] + ":" + parts[1];
return fixedStr;
}
I figured it would parse through it and get the 10/31/2009 9 and then add the 46 at the end. 我想它会解析它并得到10/31/2009 9,然后在末尾添加46。 However, I am getting a "java.lang.ArrayIndexOutOfBoundsException: 1" error upon running. 但是,运行时出现“ java.lang.ArrayIndexOutOfBoundsException:1”错误。 Obviously, it's not picking up the parts[1]. 显然,它不是在拾取零件[1]。
Any ideas? 有任何想法吗?
For extra karma: I'm needing to append the AM/PM back onto the end of the string. 对于额外的业障:我需要将AM / PM附加回字符串的末尾。 So, in the end it should look like this: 因此,最后应该看起来像这样:
10/31/2009 9:46 AM
Thanks, your help is greatly appreciated =) 谢谢,非常感谢您的帮助=)
EDIT: 编辑:
Sorry, I should have been more specific regarding the date. 抱歉,我应该更具体地说明日期。 What I'm doing is accepting a tab delimited text file into the application and then formatting it and outputting it into a new file. 我正在做的是将制表符分隔的文本文件接受到应用程序中,然后对其进行格式化并将其输出到新文件中。 All I was needing to do was drop the seconds from the date as per request and output it; 我要做的就是根据请求从日期中删除秒数并将其输出; I think I was just way over complicating it. 我认为我只是将其复杂化而已。
Tim's answer worked in this case! 蒂姆的答案在这种情况下有效!
Thank you, everyone, who offered suggestions! 谢谢大家提供的建议!
I would advise a different approach entirely: parse the value into the appropriate domain-specific type (I'd use LocalDateTime
from Joda Time ) and then reformat it in the format you want. 我会建议完全采用另一种方法:将值解析为适当的特定于域的类型(我将使用Joda Time的 LocalDateTime
),然后将其重新格式化为所需的格式。 This will perform validation at the same time, so you don't propagate bad data through your system just because it happens to have a colon in it. 这将同时执行验证,因此您不会仅仅因为系统中恰好有冒号就传播了不良数据。
Joda Time provides parse and format facilities (see DateTimeFormat
and DateTimeFormatter
), so it should be pretty simple. Joda Time提供了解析和格式化功能(请参见DateTimeFormat
和DateTimeFormatter
),因此它应该非常简单。
Why don't you use SimpleDateFormat ? 为什么不使用SimpleDateFormat ?
First parse the Date : 首先解析日期:
Date d = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a").format(str);
Then format the Date : 然后格式化日期:
new SimpleDateFormat("MM/dd/yyyy h:mm a").format(d);
如果后面的空格是空格,您是否不能删除冒号后跟两位数字?
String resultString = subjectString.replaceAll(":\\d{2}(?= )", "");
If the String is always of the same format (ending in 2 digits for the seconds, a space and them AM or PM), you can just extract the relevant parts with the substring
method available on each String
instance 如果字符串始终具有相同的格式(以2位数字表示秒,一个空格,它们是AM或PM),则可以使用每个String
实例上可用的substring
方法提取相关部分
String original = "10/31/2009 9:46:16 AM";
//-6 since we want to remove the : as well
String firstPart = original.substring( 0, original.length() - 6 );
String secondPart = original.substring( original.length() - 3, original.length() );
String result = firstPart + secondPart;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.