1

I tried very had to parse the string to date by dateformatter, the format of the string is like this --- 2018-06-02T00:00:00+11:00 and I want to parse it to Date type.

so I wrote like this

    public Resident(String address, String dob, String email, String postcode, String firstname, String surname, String provider, String familynumber)
    this.address = address;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    try{
        dob =  dob + "T00:00:00+11:00";//the dob is a string of "2018-06-02"
        this.dob = sdf.parse(dob);
        sdf.format(this.dob);
    }
    catch (ParseException e) {
        e.printStackTrace();
    }

       ...

enter image description here

however,as shown above, it generates*-- Fri Jun 01 23:00:00 GMT+10:00 2018* to me, the sequence is totally wrong, I tried numbers of way to debug but in vain.
Can someone tell me what is gone wrong??
many thanks!

Damon
  • 7
  • 3
  • that is so weird. cracking – Damon May 01 '18 at 15:29
  • What output do you expect? – cse May 01 '18 at 15:33
  • @cse exactly i was going to ask the same question . – Adeel Turk May 01 '18 at 15:35
  • if you can clear your question a little bit more – Fahed Yasin May 01 '18 at 15:41
  • Assuming that is a date of birth I suggest you use `LocalDate` from `java.time`. It’s a date without time of day and without time zone, so frees you of trouble with those. And parses your string without an explicit formatter, saving you from still more trouble. To use `java.time` on not-quite-new Android you need [the ThreeTenABP library](https://github.com/JakeWharton/ThreeTenABP). PS In your code `sdf.format(this.dob);` seems superfluous when you’re not picking up its return value. – Ole V.V. May 02 '18 at 03:52
  • @cse I want it fomatted as "2018-06-02T00:00:00+11:00" , just the same as the original string – Damon May 02 '18 at 06:23
  • @OleV.V. thanks for your kindly reply, what I am doing next is to translate it into JSON and post it to server using RESTful method, so the data type and formatt should be one hundred percent match.. – Damon May 02 '18 at 08:19
  • @FahedYasin emm, to make it simpler, I want to translate a string "2018-06-02T00:00:00+11:00 " to date type by SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");, but the result is not what I want. – Damon May 02 '18 at 08:21
  • Were you expecting the `Date` object to get your format? Not going to happen. A `Date` cannot have a format, it’s just a point in time. – Ole V.V. May 02 '18 at 11:07
  • Assuming `dob` is a `LocalDate` try `dob.atStartOfDay(ZoneOffset.ofHours(11)).toString()`. It doesn’t print the seconds, which is probably OK. The format is ISO 8601, and in ISO 8601 the seconds are optional, so your RESTful server should be happy. If it isn’t, you will need a `DateTimeFormatter`. – Ole V.V. May 02 '18 at 11:13
  • In any case consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, 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. The classes I have mentioned come from there. – Ole V.V. May 02 '18 at 11:15

1 Answers1

1

There is nothing wrong. You have parsed the date successfully.

The Fri Jun 01 23:00:00 GMT+10:00 2018 in the picture is generated by calling this.dob.toString() by debugger.

In fact, when you debug the program, it will call the object's toString() implictly to display the current value. The toString method of a Date object is the format like : Fri Jun 01 23:00:00 GMT+10:00 2018, it has nothing to do with SimpleDateFormat, just like this:

enter image description here

xingbin
  • 27,410
  • 9
  • 53
  • 103
  • I wish it is like this, but when I test by translating it to JSON through Gson , it still generates Fri Jun 01 23:00:00 GMT+10:00 2018 to me, it is obviously not what I want – Damon May 02 '18 at 08:16
  • 1
    @Damon You’re asking more than one question. What goes wrong in parsing, how to format, how to translate to JSON through Gson. It will be easier for us if you ask one question at a time. Did you check [GSON - Date format](https://stackoverflow.com/questions/6873020/gson-date-format) yet? – Ole V.V. May 02 '18 at 11:20
  • @OleV.V. sorry, I thought it is just due to the date format issue. – Damon May 02 '18 at 11:25
  • 1
    @Damon In the picture you posted, the date has been successfully parsed. The next process has nothing to do with `SimpleDateFormatter`! – xingbin May 02 '18 at 12:36