1

As i am new in programming i need some help with this. If i have a String for example like this following string. Here you see date and time included in one single String.

String date_time = "2018-03-18T01:39:45+01:00";

Is there anyway to only pull the date out of the String.

"2018-03-18"

The case is that i have a full string with date and time, but i need to extract date for itself, and time for itself, so i can use it seperately.

EDIT

Sorry, but i think the recent heat has taken on me. i forgot to mention that the Strings are stored in an arraylst

List<String> allDate_time = new ArrayList<String>();

JSONObject date_timeArray = json2.getJSONObject( 0 );

String date_time = date_timeArray.getString( "date_time" );

allDate_time.add( date_time );

dateView = (TextView) findViewById( R.id.dateTX );

dateView.setText( allDate_time.get( 0 ));
Buster3650
  • 470
  • 2
  • 8
  • 19
  • 2
    Possible duplicate of [Extract time from date String](https://stackoverflow.com/questions/3504986/extract-time-from-date-string) – Eselfar May 31 '18 at 16:27
  • Parse your string into an `OffsetDateTime` as explained [here](https://stackoverflow.com/a/50384778/5772882) (and in many other places). Then use `OffsetDateTime.toLocalDate()` and `OffsetDateTime.toLocalTime()`. For the time (and really the date too), do you just want the time in the string, or do you want the time in some specific time zone (like the user’s time zone, for example)? – Ole V.V. Jun 01 '18 at 07:03

4 Answers4

0

You can retrieve the substring from the start till you find the character 'T'. There are other date/time apis as well, but the below code should suffice your need

String date_time = "2018-03-18T01:39:45+01:00";
    String date=date_time.substring(0,date_time.indexOf("T"));
    String time=date_time.substring(date_time.indexOf("T")+1,date_time.length());

EDIT: For the arraylist you can do as below. Loop over the arraylist and retrieve the date and time.

for (String dateTime: allDate_time){
    String date=date_time.substring(0,date_time.indexOf("T"));
    String time=date_time.substring(date_time.indexOf("T")+1,date_time.length());
}
Psypher
  • 10,717
  • 12
  • 59
  • 83
0

You need to parse the Date. Example:-

Function For Converting String To Date:-

   private Date toDate(String date)
    {
      Date date;
      SimpleDateFormat format = new SimpleDateFormat("format of your date",Locale.Default);
       try {
           date = format.parse(date);
           }
           catch (ParseException e) {
           e.printStackTrace();
           date = null;
      }
     return date;
    }

Function Implementation:-

String date = "2018-03-18T01:39:45+01:00";//You Can Use The Function Everytime You Want To Convert String To Date.
Date date = toDate(date);//You Can Take Out Date And Time Differently From Date.
String time = new SimpleDateFormat("H:mm").format(date);//You Can Change The Format You Wanted.
Shashank Mishra
  • 542
  • 4
  • 12
0

Try these solutions

Solution 1:-

String date_time=allDate_time.get( 0 );

then extract the date from date_time string

String date=date_time.substring(0,date_time.indexOf("T"));

Solution 2:-

String date_time=allDate_time.get( 0 );
String date=date_time.substring(0,10);
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

java.time

    List<String> allDate_time = new ArrayList<>();
    String date_time = "2018-03-18T01:39:45+01:00";
    allDate_time.add( date_time );

    for (String dateTimeString : allDate_time) {
        OffsetDateTime odt = OffsetDateTime.parse(dateTimeString);
        LocalDate date = odt.toLocalDate();
        LocalTime timeOfDay = odt.toLocalTime();
        System.out.println("Date: " + date + ". Time: " + timeOfDay + '.');
    }

Output:

Date: 2018-03-18. Time: 01:39:45.

Since the OffsetDateTime class of java.time, the modern Java date and time API, parses your date-time strings without any explicit formatter, it would almost be a pity not to exploit this. The explanation why it’s so easy is that your string is in ISO 8601 format, and the classes of java.time parse this format as their default.

If you you’re not sure whether the offset in the string (here +01:00) agrees with your user’s time zone, convert to that time zone before extracting date and time. For example:

        ZonedDateTime zdt = odt.atZoneSameInstant(ZoneId.of("America/Argentina/Cordoba"));
        LocalDate date = zdt.toLocalDate();
        LocalTime timeOfDay = zdt.toLocalTime();

With this change the output is:

Date: 2018-03-17. Time: 21:39:45.

My recommendations:

  • Handle date and time as date and time objects using classes from the standard library. In the long run this is stabler and easier than relying on string manipulation and gives code that is much easier to read and maintain.
  • Rather than the old classes Date and SimpleDateFormat used in one other answer, use the modern ones in java.time. Date and SimpleDateFormat are legacy, and SimpleDateFormat in particular is notoriously troublesome.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161