2

I am getting date in this format from API 2019-01-22T04:38:22Z, which I want it to be separated in date and time with following format 31/01/2019 and 23:59.

I am new to Java so not able to figure it out how and which class or method I should use to make it. I know little bit bout SimpleDateFormat

Any suggestion or idea would be appreciated.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • Consider not using the long outmoded and notoriously troublesome `SimpleDateFormat`, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 27 '19 at 22:34
  • Your original date-time string is in UTC. Do you want the date and time printed in UTC too? For many purposes you would probably prefer the user’s time zone. – Ole V.V. Jan 27 '19 at 22:37

2 Answers2

3

try this line of code to convert string to date and get date and time

code

  String dtStart = "2019-01-27T09:27:37Z";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    try {
        Date date = format.parse(dtStart);
        System.out.println(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");

        String date1 = sdf.format(date);
        String time= sdf1.format(date);
        Log.e("check_date_time",""+date1+"=="+time);
    } catch (ParseException e) {
        e.printStackTrace();
    }

output

2019/01/27 09:27

I hope its work for you

white hills
  • 237
  • 1
  • 8
  • welcome dear... :) – white hills Jan 27 '19 at 06:33
  • 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. Jan 27 '19 at 22:33
  • @OleV.V. Thanks for your input. I need to check how to use it. BTW, `SimpleDateForat` is not a good idea? If than why? – Code Lover Jan 28 '19 at 07:39
  • `SimpleDateFormat` is notorious for showing a lot of surprising behaviours including giving wrong results in a lot of situations where one would have expected to be notified of incorrect use. It’s what you’ve got built-in on Android under API level 26, so for simple tasks like this you may weigh the cons against the cons of an external dependency, but generally I recommend you stay away from it. My 0.02. – Ole V.V. Jan 28 '19 at 07:44
3

A more robust way of accomplishing this task is provided below with a code example.

    String initialStringDate = "2019-01-27T09:27:37Z";
    Locale us = new Locale("US");
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", us);
    try {
        Date date = format.parse(initialStringDate);
        String stringDate = new SimpleDateFormat("yyyy/MM/dd", us).format(date);
        String stringTime = new SimpleDateFormat("HH:mm", us).format(date);

        String finalDateTime = stringDate.concat(" ").concat(stringTime);

        Log.i("Date_and_Time", "" + finalDateTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }

Note: With the provided above code you will be able to even localize your date and time.

  • Thanks a lot. Yeah, this is better if want to localize the time which is important. – Code Lover Jan 27 '19 at 07:16
  • 1
    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. Jan 27 '19 at 22:33
  • 1
    The answer is not entirely correct. `Z` in the end of the string is an offset and must be parsed as such, or you get the wrong time (on the vast majority of JVMs). Also `new Locale("US")` doesn’t give you what you think (lemme guess, you intended `Locale.US`?) – Ole V.V. Jan 27 '19 at 22:41
  • @OleV.V. I think you are right but how can I fix this? – Code Lover Jan 28 '19 at 07:40
  • As I may have said, the good fix is using java.time, it has good support for parsing an offset that is given as `Z` when zero. See the linked questions and their answers. [Andreas has a good answer here](https://stackoverflow.com/a/52671099/5772882) showing both the modern and the old-fashioned solutions for Android. If below level 24, there’s no really correct solution using `SimpleDateFormat`; if you still insist on it, the hack is to set the formatter’s time zone to UTC and then parse `Z` as a literal as in this answer. – Ole V.V. Jan 28 '19 at 07:50