0

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).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Mohamed Hassan
  • 191
  • 2
  • 12

6 Answers6

2

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    OffsetDateTime.parse interprets the string in DateTimeFormatter.ISO_OFFSET_DATE_TIME by default. So, you can simplify for the OP's case by dropping specifying the format and the ZoneOffset and get the same result. – jmrah Jun 13 '20 at 15:11
  • @jrahhali - Thanks for the suggestion. I've updated my answer to incorporate it. – Arvind Kumar Avinash Jun 13 '20 at 15:57
2

If you are using at least API level 26, then you can use ZonedDateTime 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

Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    `ZonedDateTime` isn’t necessary. `OffsetDateTime` will do since the string contains an offset, +02:00, not a time zone like Asia/Jerusalem. Otherwise a good answer. – Ole V.V. Jun 13 '20 at 17:06
0
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);
Littm
  • 4,923
  • 4
  • 30
  • 38
Md Aman
  • 340
  • 3
  • 10
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – β.εηοιτ.βε Jun 13 '20 at 18:45
0

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);
    }
}
QuickSilver
  • 3,915
  • 2
  • 13
  • 29
0

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

private int parseDate(ZonedDateTime date) {
    int day = date.getDayOfMonth();
    return day;
}
Robert Głowacki
  • 292
  • 5
  • 22
  • The given input `2020-06-13T16:21:15.239920+02:00` is appropriate for `OffsetDateTime` class rather than `ZonedDateTime`. See the [Answer by Avinash](https://stackoverflow.com/a/62361576/642706). – Basil Bourque Jun 13 '20 at 20:57
0

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

Šimon Slabý
  • 533
  • 1
  • 4
  • 18
  • While the code in this Answer may work we have a purpose-built class for this: [`java.time.OffsetDateTime`](https://duckduckgo.com/?q=offsetdatetime+java+11&t=osx&ia=web). See the [Answer by Avinash](https://stackoverflow.com/a/62361576/642706). – Basil Bourque Jun 13 '20 at 20:55