将字符从一点移到另一点
remove characters from one point to another
我有一个时间和日期字符串。
字符串总是看起来像这样(但带有实际日期。)
2015-01-06T06:36:12Z
我想删除日期(加上 T 和 Z)
要删除 T 和 Z,我可以使用正则表达式删除每个非数字字符,所以这不是问题。
我的问题是我不知道如何从字符 0
删除到字符 10
-> 2015-01-06T <- I want this removed.
我已经尝试了一些方法,但似乎无法找到一种方法来做到这一点。
使用 SmpleDateFormat ,还有其他库也可以处理时间 anddate.However 如果你有一些包含日期和时间信息的特定类型的字符串,那么你必须从你的代码。
See and example and here
开始的简单代码示例
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("hh:mm:ss");
System.out.println(sdf.format(date));
您可以使用子字符串:
System.out.println("2015-01-06T06:36:12Z".substring(11,19));
\T\.\w+_fn\Z
这个正则表达式给你06:36:12
。它删除了 T
、Z
和日期部分。
如果您只想删除 "from character 0 to character 10",那么您可以简单地使用 String
class 的 substring(int beginIndex)
函数。
String date = "2015-01-06T06:36:12Z"
String newString = date.substring(11);
// newString will be "06:36:12Z"
您必须将值 11 传递给 substring()
函数,因为您希望新字符串是给定的 日期,从第 11 个字符到结尾。
Java 8 引入了多种日期函数,在解析格式时可以找到一篇很棒的文章 here. One class that was introduced was the DateTimeFormatter SimpleDateFormatter
- DateTimeFormatter 是线程安全的。
所以,我可能不会使用答案中提到的 substring
方法。相反,我会使用 DateTimeFormatter
来解析字符串,然后以所需的格式输出它。这也提供了一些验证,即输入格式符合预期并且输出格式也有效。
示例:
@Test
public void test() throws IOException, ParseException {
// Setup the input formatter
final DateTimeFormatter inputFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
// Parse and validate the date
final LocalDateTime parsed =
LocalDateTime.parse("2015-01-06T06:36:12Z", inputFormatter);
// Setup the output formatter
final DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// Format the date to the desired format
String formatted = outputFormatter.format(parsed);
// Verify the contents (part of test only)
Assert.assertEquals("06:36:12", formatted);
}
Java 8 中的新日期和时间功能在很大程度上受到 Joda-Time and this SO-question 的启发,对于那些好奇它们之间有什么区别的人来说是很好的读物。
怎么样:
String input ="2015-01-06T06:36:12Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf2.format(sdf.parse(input)));
我有一个时间和日期字符串。 字符串总是看起来像这样(但带有实际日期。)
2015-01-06T06:36:12Z
我想删除日期(加上 T 和 Z) 要删除 T 和 Z,我可以使用正则表达式删除每个非数字字符,所以这不是问题。
我的问题是我不知道如何从字符 0
删除到字符 10
-> 2015-01-06T <- I want this removed.
我已经尝试了一些方法,但似乎无法找到一种方法来做到这一点。
使用 SmpleDateFormat ,还有其他库也可以处理时间 anddate.However 如果你有一些包含日期和时间信息的特定类型的字符串,那么你必须从你的代码。 See and example and here
开始的简单代码示例
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("hh:mm:ss");
System.out.println(sdf.format(date));
您可以使用子字符串:
System.out.println("2015-01-06T06:36:12Z".substring(11,19));
\T\.\w+_fn\Z
这个正则表达式给你06:36:12
。它删除了 T
、Z
和日期部分。
如果您只想删除 "from character 0 to character 10",那么您可以简单地使用 String
class 的 substring(int beginIndex)
函数。
String date = "2015-01-06T06:36:12Z"
String newString = date.substring(11);
// newString will be "06:36:12Z"
您必须将值 11 传递给 substring()
函数,因为您希望新字符串是给定的 日期,从第 11 个字符到结尾。
Java 8 引入了多种日期函数,在解析格式时可以找到一篇很棒的文章 here. One class that was introduced was the DateTimeFormatter SimpleDateFormatter
- DateTimeFormatter 是线程安全的。
所以,我可能不会使用答案中提到的 substring
方法。相反,我会使用 DateTimeFormatter
来解析字符串,然后以所需的格式输出它。这也提供了一些验证,即输入格式符合预期并且输出格式也有效。
示例:
@Test
public void test() throws IOException, ParseException {
// Setup the input formatter
final DateTimeFormatter inputFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
// Parse and validate the date
final LocalDateTime parsed =
LocalDateTime.parse("2015-01-06T06:36:12Z", inputFormatter);
// Setup the output formatter
final DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// Format the date to the desired format
String formatted = outputFormatter.format(parsed);
// Verify the contents (part of test only)
Assert.assertEquals("06:36:12", formatted);
}
Java 8 中的新日期和时间功能在很大程度上受到 Joda-Time and this SO-question 的启发,对于那些好奇它们之间有什么区别的人来说是很好的读物。
怎么样:
String input ="2015-01-06T06:36:12Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf2.format(sdf.parse(input)));