简体   繁体   中英

How to get a specific part of a string in java

I'm developing an Android app that gets the date from the internet. What I have is a complete date format like this one: 2020-06-13T16:21:15.239920+02:00 . I want to get the day of the month (which is 13 in this case).

Your date/time string complies with DateTimeFormatter.ISO_OFFSET_DATE_TIME . You can use java.time API to get the day of the month as shown below:

import java.time.OffsetDateTime;

public class Main {
    public static void main(String[] args) {
        String dateTimeStr = "2020-06-13T16:21:15.239920+02:00";
        OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr);
        int day = odt.getDayOfMonth();
        System.out.println(day);
    }
}

Output:

13

If you can not use Java SE 8 Date and Time , check ThreeTenABP and also How to use ThreeTenABP in Android Project .

If you are using at least API level 26, then you can useZonedDateTime class as your string uses the default format that is understood by that class.

ZonedDateTime zdt = ZonedDateTime.parse("2020-06-13T16:21:15.239920+02:00");
int d = zdt.getDayOfMonth();

Alternatively, if the format is constant, simply use method substring()

String dd = "2020-06-13T16:21:15.239920+02:00".substring(8, 10);

If the format is not constant, I would suggest either regular expression or combining ZonedDateTime with DateTimeFormatter

String str = "2020-06-13T16:21:15.239920+02:00";
String substr = "";

// prints the substring after index 7(8 includes) till index 9(10 excludes)
substr = str.substring(8, 10);

Its always a bad idea to split a string and extract data becuase the string will change to some other value once the date, month or any part of the date string changes and the string indexing will change

So use DateTimeFormatter

import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class SDF {

    public static void main(String[] args) {
        final TemporalAccessor parse = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSxxx")
                .parse("2020-06-13T16:21:15.239920+02:00");

        int dayOfMonth = MonthDay.from(parse).getDayOfMonth();
        System.out.println(dayOfMonth);
    }
}

There should be a method getDayOfMonth() available on this variable

private int parseDate(ZonedDateTime date) {
    int day = date.getDayOfMonth();
    return day;
}

You can use String split . Just take this whole format of date and put it into String and then create a new String Array String[] array= str.split("[-T]"); and then you can get result from array[2] .

String str = "2020-06-13T16:21:15.239920+02:00";
String[] array= str.split("[-T]");

System.out.println("OUTPUT: " + array[2]);

Output: 13

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM