Home > Mobile >  how to break a string into two parts in java
how to break a string into two parts in java

Time:12-21

Let's assume a string consists of two parts, day and date.

String arr[] = { "Tuesday 8/11/22", "Monday 15/3/21", "Friday 20/5/21" };

How can I only print out the latter part (date part) on console?

CodePudding user response:

As long as the day and date will stay in that format you can use the split() function:

class DateSplit {
    public static void main(String[] args) {
        String a = "Tuesday 8/11/22";
        String[] result = a.split(" ");
        System.out.println(result[1]);
    }
}

where the delimiter splitting the string is the whitespace character.

CodePudding user response:

for(String str : arr) {
  System.out.println(str.split(" ")[1])
}

CodePudding user response:

public class Main {
            public static void main(String[] args) {
                String arr[] = { "Tuesday 8/11/22", "Monday 15/3/21", "Friday 20/5/21" };
            
            for(int count = 0; count < arr.length; count  ) {
                String[] s = arr[count].split(" ");
                System.out.println(s[1]);
            }
        }
    }

CodePudding user response:

I recommend you use the date-time API instead of performing string manipulation of the elements. The string manipulation will not give you all the benefits that you can get by the specialized date-time API.

Bonus

You get your dates validated free of cost e.g. one of your dates, Friday 20/5/21 is incorrect. This date was Thursday and the java.time API can perform this check for you automatically.

Demo:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        String arr[] = { "Tuesday 8/11/22", "Monday 15/3/21", "Friday 20/5/21" };
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEEE d/M/uu", Locale.ENGLISH);
        for (String s : arr) {
            LocalDate date = LocalDate.parse(s, parser);

            // Now you can get individual units from date in a variety of ways
            System.out.println(date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
            System.out.println(date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
            System.out.println(date.getYear());

            // You can also format it in the desired ways
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/uu", Locale.ENGLISH);
            String formatted = date.format(formatter);
            System.out.println(formatted);
        }
    }
}

Output:

Tuesday
Tue
2022
8/11/22
Monday
Mon
2021
15/3/21
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Friday 20/5/21' could not be parsed: Conflict found: Field DayOfWeek 4 differs from DayOfWeek 5 derived from 2021-05-20
        at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023)

Learn more about the modern Date-Time API from Trail: Date Time.

  • Related