0

I have a string that is time and date. The string always looks like this (but with real dates ofc.)

2015-01-06T06:36:12Z

I want to remove the date (plus the T and Z) To remove the T and Z I can just use regex to remove every non-numberic character so that's not a problem.

My problem is that I have no idea how to remove from character 0 to character 10 -> 2015-01-06T <- I want this removed. I've tried some ways but can't seem to find a way to do this.

Squidly
  • 2,707
  • 19
  • 43
Addemod C
  • 23
  • 6
  • 2
    [`String#substring`](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int-) – Maroun Jan 06 '15 at 10:21
  • 1
    I would definitely go the parsing route. That provides validation at the same time, which is useful IMO. – Jon Skeet Jan 06 '15 at 10:23

6 Answers6

2

Java 8 introduced a variety of date functions and when it comes to parsing an formatting a great article can be found here. One class that was introduced was the DateTimeFormatter which has one big upside compared to e.g. SimpleDateFormatter - DateTimeFormatter it is thread-safe.

So, I would probably not use the substring-approach that was mentioned in an answer. Instead I would use the DateTimeFormatter to parse the string and then output it in the required format. This also offers some validation that the input format is as expected and that the output format is valid as well.

Example:

@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);
}

The new date and time features in Java 8 are heavily inspired by Joda-Time and this SO-question is good reading for those who are curious what the differences are.

Community
  • 1
  • 1
wassgren
  • 18,651
  • 6
  • 63
  • 77
0

Use SmpleDateFormat ,there are other libraries too to work with time anddate.However if you are having some specific types of strings containing date and time information,then you have to manuaaly parse them from your code. See and example and here

Simple code example to start of with

Date date = new Date(); 
SimpleDateFormat sdf; 
sdf = new SimpleDateFormat("hh:mm:ss"); 
System.out.println(sdf.format(date)); 
Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
0

You could use substring:

System.out.println("2015-01-06T06:36:12Z".substring(11,19));
panagdu
  • 2,133
  • 1
  • 21
  • 36
0

\T\.\w+_fn\Z This regular expression give you 06:36:12. It with remove T, Z and date part.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
0

If you just want to remove "from character 0 to character 10", then you could simply use the String class's substring(int beginIndex) function.

String date = "2015-01-06T06:36:12Z"
String newString = date.substring(11);
// newString will be "06:36:12Z"

You have to pass value 11 to the substring() function, because you want the new string to be the given date, from 11th character to end of it.

Ali Lotfi
  • 856
  • 3
  • 18
  • 40
0

What about :

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)));
Michaël
  • 3,679
  • 7
  • 39
  • 64