Java 从一个点到另一个点删除字符
我有一个字符串是时间和日期。 字符串看起来总是这样(但实际日期为ofc) 2015-01-06T06:36:12Z 我想删除日期(加上T和Z) 要删除T和Z,我可以使用正则表达式删除每个非数字字符,所以这不是问题 我的问题是,我不知道如何从characterJava 从一个点到另一个点删除字符,java,string,Java,String,我有一个字符串是时间和日期。 字符串看起来总是这样(但实际日期为ofc) 2015-01-06T06:36:12Z 我想删除日期(加上T和Z) 要删除T和Z,我可以使用正则表达式删除每个非数字字符,所以这不是问题 我的问题是,我不知道如何从character0删除到character10->2015-01-06T使用,还有其他库可以处理时间和日期。但是,如果您有一些特定类型的字符串包含日期和时间信息,那么您必须手动从代码中解析它们。 及 开始时使用的简单代码示例 Date date = new
0
删除到character10
->2015-01-06T使用,还有其他库可以处理时间和日期。但是,如果您有一些特定类型的字符串包含日期和时间信息,那么您必须手动从代码中解析它们。
及
开始时使用的简单代码示例
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("hh:mm:ss");
System.out.println(sdf.format(date));
使用时,还有其他库可以处理时间和日期。但是,如果您有一些特定类型的字符串包含日期和时间信息,则必须手动从代码中解析它们。
及
开始时使用的简单代码示例
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("hh:mm:ss");
System.out.println(sdf.format(date));
使用时,还有其他库可以处理时间和日期。但是,如果您有一些特定类型的字符串包含日期和时间信息,则必须手动从代码中解析它们。
及
开始时使用的简单代码示例
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("hh:mm:ss");
System.out.println(sdf.format(date));
使用时,还有其他库可以处理时间和日期。但是,如果您有一些特定类型的字符串包含日期和时间信息,则必须手动从代码中解析它们。
及
开始时使用的简单代码示例
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));
您可以使用子字符串:
System.out.println("2015-01-06T06:36:12Z".substring(11,19));
您可以使用子字符串:
System.out.println("2015-01-06T06:36:12Z".substring(11,19));
您可以使用子字符串:
System.out.println("2015-01-06T06:36:12Z".substring(11,19));
\T\。\w+\u fn\Z
此正则表达式为您提供06:36:12
。它带有removeT
、Z
和日期部分。\T\.\w+\u fn\Z
这个正则表达式为您提供了06:36:12
。它带有removeT
、Z
和日期部分。\T\.\w+\u fn\Z
这个正则表达式为您提供了06:36:12
。它带有removeT
、Z
和日期部分。\T\.\w+\u fn\Z
这个正则表达式为您提供了06:36:12
。它使用removeT
、Z
和日期部分进行删除。如果您只想删除“从字符0到字符10”,那么您可以简单地使用String
类的子字符串(int beginIndex)
函数
String date = "2015-01-06T06:36:12Z"
String newString = date.substring(11);
// newString will be "06:36:12Z"
您必须将值11传递给substring()
函数,因为您希望新字符串是给定的日期,从第11个字符到它的结尾。如果您只想删除“从字符0到字符10”,那么您可以简单地使用string
类的子字符串(int beginIndex)
函数
String date = "2015-01-06T06:36:12Z"
String newString = date.substring(11);
// newString will be "06:36:12Z"
您必须将值11传递给substring()
函数,因为您希望新字符串是给定的日期,从第11个字符到它的结尾。如果您只想删除“从字符0到字符10”,那么您可以简单地使用string
类的子字符串(int beginIndex)
函数
String date = "2015-01-06T06:36:12Z"
String newString = date.substring(11);
// newString will be "06:36:12Z"
您必须将值11传递给substring()
函数,因为您希望新字符串是给定的日期,从第11个字符到它的结尾。如果您只想删除“从字符0到字符10”,那么您可以简单地使用string
类的子字符串(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引入了各种日期函数,在解析格式时,可以找到一篇很棒的文章。引入的一个类是,与例如SimpleDateFormatter
-DateTimeFormatter相比,它有一个很大的优势,它是线程安全的
因此,我可能不会使用回答中提到的子字符串
-方法。相反,我将使用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中新的日期和时间功能深受启发,对于那些想知道它们之间的区别的人来说,这是一本不错的读物。Java 8引入了各种日期函数,在解析格式时,可以找到一篇很棒的文章。引入的一个类是,与例如SimpleDateFormatter
-DateTimeFormatter相比,它有一个很大的优势,它是线程安全的
因此,我可能不会使用回答中提到的子字符串
-方法。相反,我将使用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中新的日期和时间功能深受启发,对于那些想知道它们之间的区别的人来说,这是一本不错的读物。Java 8引入了各种日期函数,在解析格式时,可以找到一篇很棒的文章。引入的一个类是,与例如SimpleDateFormatter
-DateTimeFormatter相比,它有一个很大的优势,它是线程安全的
因此,我可能不会使用回答中提到的子字符串
-方法。相反,我将使用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);
}
Java8中新的日期和时间特性深受启发,对于那些想知道两者之间有什么不同的人来说,这是一本很好的读物