-1

I receive the following date/time format through api call:

"AcceptedDate": "2020-09-28T11:47:37.217",
"Pickup1ArrivedDate": "2020-10-06T17:28:12.6",
"Pickup1LoadedDate": "2020-10-06T17:57:54.84",
"Pickup1DepartedDate": "2020-10-06T18:18:59.927"

Is there anyway to show the time in the format"11:47" after saving the response in java/android studio. Any help is appreciated. Thanks :-)

krish
  • 237
  • 4
  • 14

5 Answers5

1

You can use substring() to create a method to do so:

 public String stripRedundantDate(String date){
     return date.substring(11, 16);
 }

The following program:

public class Main{

     public static void main(String []args){
        String time = "2020-09-28T11:47:37.217";
        System.out.println(stripRedundantDate(time));
     }
     public static String stripRedundantDate(String date){
         return date.substring(11, 16);
     }
}

Produces the result:

11:47

If your date is not a fixed length

Use the following:

 public static String stripRedundantDate(String date){
     return date.substring(date.indexOf(':')-2, date.indexOf(':')+3);
 }
Spectric
  • 30,714
  • 6
  • 20
  • 43
0

I would convert it to a date object and then work with it that way

LocalDate date = LocalDate.parse(apiResult.get("AcceptedDate"));

Then I would format it to look how you want:

SimpleDateFormat format = new SimpleDateFormat("HH:mm");

String dateString = format.format( date  ); // this string will be the time you are looking for
Nathan Walsh
  • 358
  • 2
  • 4
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Oct 06 '20 at 15:16
  • Also from your first code line I get `java.time.format.DateTimeParseException: Text '2020-09-28T11:47:37.217' could not be parsed, unparsed text found at index 10`. You should use `LocalDateTIme` (not `LocalDate`) for parsing the string. – Ole V.V. Oct 06 '20 at 15:16
  • 1
    `SimpleDateFormat` doesn't know how to format a `LocalDate`. – Andreas Oct 06 '20 at 15:23
0

Kotlin

fun formatDate(date: String): String {
        val input = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH)
        val output = SimpleDateFormat("12:08 PM", Locale.ENGLISH)
        return output.format(input.parse(date)!!)
    }
Zeeshan Ayaz
  • 858
  • 6
  • 11
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Oct 06 '20 at 15:13
0

Just take the next 5 characters after the letter T

    String date = "2020-09-28T11:47:37.217";
    int from = date.indexOf("T")+1;
    int to = from+5;
    
    String timeOfInterest = date.substring(from, to);
    System.out.println( timeOfInterest );


    
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
0

java.time either through desugaring or through ThreeTenABP

I suggest you consider using java.time, the modern Java date and time API, for your date and time work.

    String acceptedDate = "2020-09-28T11:47:37.217";
    LocalDateTime dateTime = LocalDateTime.parse(acceptedDate);
    String timeString = dateTime.toLocalTime()
            .truncatedTo(ChronoUnit.MINUTES)
            .toString();
    System.out.println(timeString);

Output is:

11:47

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

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