0

so I'm inserting the date of birth to an API, and the API returns the updated information, which i'm supposed to present on another page after handling the data in java backend.

Here is what gets returned in JSON:

"firstname": "John",
"middlename": "The",
  "lastname": "Doe",
  "displayName": "John The Doe",
  "dateOfBirth": [
    1994,
    3,
    26
  ]

so what I'm having trouble with, is picking out the 3 (year/month/day) in separate variables, because if theres no 0 in 03 ( mars for example ) i want to add the 0, same goes with day.

Here i'm getting the object:

@Override
public Object getDateOfBirth() {
    return get("dateOfBirth");
}

But i'm getting [1994,3,26] which obviously looks very bad displayed on a website.

How would you get the 3 "1994,3,26" in different variables?

Any help is greatly appreciated.

MattiasH
  • 423
  • 1
  • 7
  • 20
  • 1
    How about creating a date formatter method to return formatted date as like as your requirement ? – Md Ashfak Chowdhury Mar 23 '17 at 08:38
  • If they are always on the same positions, I would parse the date and format it the way I want it before displaying it to the client. – Andrei Olar Mar 23 '17 at 08:42
  • If you want date fields in 3 different variables, how about creating the 3 different variables and then extracting array values from them? – M. Prokhorov Mar 23 '17 at 08:48
  • yes @M.Prokhorov, how do i do this? that sounds exactly like what i wanna do – MattiasH Mar 23 '17 at 08:53
  • Depends on what array implementation is. If that's a simple java array, you will need to cast it and then extracting like this: `int[] dob = (int[]) getDateOfBirth(); int year = dob[0], month = dob[1], day = dob[2];` – M. Prokhorov Mar 23 '17 at 08:56
  • In addition to Shaon's comment: Display the date dependent on the user's (browser) Locale (dd.MM.yyyy vs MM.dd.yyyy)... – slowy Mar 23 '17 at 09:00
  • the array is in json @M.Prokhorov – MattiasH Mar 23 '17 at 09:09
  • Json as in "json string"? Then split and parse it yourself or use some library to do it. `DateTimeFormatter.ofPattern("'['yyyy,M,d']'")` or similar to it should be enough even. Later you can do `LocalDate.parse(parser, getDateOfBirth())`. – M. Prokhorov Mar 23 '17 at 09:12

3 Answers3

1

if you are using Java 8 or later, you can use LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate.of(year, month, day); //2015-12-22
LocalDate.of(Integer.parseInt(dateOfBirth[0]), Integer.parseInt(dateOfBirth[1]), Integer.parseInt(dateOfBirth[2]));
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
coenni
  • 437
  • 4
  • 14
  • FYI, for earlier than Java 8… Much of the java.time functionality is back-ported to Java 6 & 7 in [ThreeTen-Backport](http://www.threeten.org/threetenbp/) and further adapted to [Android](https://en.wikipedia.org/wiki/Android_(operating_system)) in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) (see [*How to use…*](http://stackoverflow.com/q/38922754/642706)). – Basil Bourque Mar 23 '17 at 22:05
0

take a look on this link This can help. I can recommend you to use the Gson

if you use maven add this dependency to the pom

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
</dependency>



public class MyJsonAsClass
{
    private String firstname;
    private String middlename;
    private String lastname;
    private String displayName;
    private List<String> dateOfBirth;

    public MyJsonAsClass()
    {

    }

    public static MyJsonAsClass fromString(String json) 
    {
       Gson gson = new Gson();
       MyJsonAsClass info = gson.fromJson(json, MyJsonAsClass.class);
       return info;
    }
    /*you need to add getters & setters */

    public List<String> getdateOfBirth()
    {
       for (String date : dateOfBirth) {
           if (date.length()==1)
               date='0'+date;
       }
       return dateOfBirth;
    }
Community
  • 1
  • 1
yogev levi
  • 369
  • 1
  • 4
  • 21
  • We did use Gson and had a lot of other problems, for various reasons we decided to not use Gson anywhere in our application – MattiasH Mar 23 '17 at 08:55
  • I use it all the time and it make my life very easy I don't know any problems caused by it to the application. Just out of curiosity What are the problems? – yogev levi Mar 23 '17 at 09:03
0

But i'm getting [1994,3,26] which obviously looks very bad displayed on a website.

How would you get the 3 "1994,3,26" in different variables?

You can transform the output of returned array object into custom format for display purpose. Assuming get("dateOfBirth") returns and Array object. You can do something similar ...

String result = Arrays.toString(dateOfBirthArrayObject).replaceAll("[\\[\\]]", "");
            
System.out.println(result);

Output: 1994, 3, 26

Community
  • 1
  • 1
fabfas
  • 2,200
  • 1
  • 21
  • 21