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

- 81,772
- 15
- 137
- 161

- 191
- 2
- 12
-
You can use String functions to get date based upon character positions or you can use data/time formatter available in java to get the date – Nitin Bisht Jun 13 '20 at 14:57
-
@NitinBisht but how to get it based upon character positions (I'm beginner) – Mohamed Hassan Jun 13 '20 at 15:00
-
If you only want it for this format you can use YOUR_STRING.substring(8,10); – Wowo Ot Jun 13 '20 at 15:01
-
@MohamedHassan check `substring` method – Nitin Bisht Jun 13 '20 at 15:06
-
@Everyone Please don’t use string functions. Use date-time functions like the top-voted answers say. – Ole V.V. Jun 13 '20 at 17:08
-
If you’ve got two strings `2020-06-13T23:59:01.239920-12:00` and `2020-06-13T00:00:15.239920+14:00`, do you want 13 in both cases? There are almost 50 hours, more than 2 days, between the two points in time. – Ole V.V. Jun 13 '20 at 17:24
6 Answers
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.

- 303,325
- 100
- 852
- 1,154

- 71,965
- 6
- 74
- 110
-
1OffsetDateTime.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
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

- 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
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);
-
1While 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
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);
}
}

- 3,915
- 2
- 13
- 29
There should be a method getDayOfMonth()
available on this variable
private int parseDate(ZonedDateTime date) {
int day = date.getDayOfMonth();
return day;
}

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

- 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