Home > Software engineering >  Remove everything leaving first ten characters in a string
Remove everything leaving first ten characters in a string

Time:06-07

Below is my String and I want to remove everything leaving first 10 characters using Java method.

2022-05-31T07:17:17.292438

The value can be anything after first 10 characters which is a date and I want only the date to be returned in String format. Can someone help me?

CodePudding user response:

It's recommended that you use proper date and time handling.

LocalDateTime.parse("2022-05-31T07:17:17.292438").format(DateTimeFormatter.ISO_LOCAL_DATE);

yields 2022-05-31. And you get validation for free

CodePudding user response:

try this,

String ab = "2022-05-31T07:17:17.292438";
System.out.println(ab.substring(0, 10));
  •  Tags:  
  • java
  • Related