Format date on server Reply from REST API does not work

I have a method that receives data from a REST server. The method returns a date in this format "2017-08-14T17: 45: 16.24Z". I also wrote a method that formats the date in the order "dd / MM / yyyy". This works fine, but when I try to format the date from the server and set it to Edittext, it will not work. This shows that my date formatting method in the server response does not work. My date formatting method is given below:

private String formatDate(String dateString) {
    try {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS" );
        Date d = sd.parse(dateString);
        sd = new SimpleDateFormat("dd/MM/yyyy");
        return sd.format(d);
    } catch (ParseException e) {
    }
    return "";
}

This method is below, which receives the date from the server and formats the date and sets the date in the edit text.

public void getProfile() {

    Retrofit retrofit = RetrofitClient.getClient(authUser.getToken());
    APIService mAPIService = retrofit.create(APIService.class);

    mAPIService.getProfile("Bearer " + authUser.getToken()).enqueue(new Callback<Profile>() {
        @Override
        public void onResponse(Response<Profile> response, Retrofit retrofit) {
            if(response.isSuccess()) {
                try {
                    String loginSuccess = response.body().getSuccess();
                    if (loginSuccess.equals("true")) {
                        id_name.setText(response.body().getData().getName());
                        id_email.setText(response.body().getData().getEmail());
                        phone_input_layout.setText(response.body().getData().getPhoneNumber());
                        id_gender.setText(response.body().getData().getGender());
                        String dateOfBirth = response.body().getData().getDateOfBirth();
                        id_date_of_birth.setText(formatDate(dateOfBirth));
                        //updateLabel(dateOfBirth);
                        id_residential_address.setText(response.body().getData().getResidentialAddress());
                        if (response.body().getData().getEmploymentStatus().equals("Student")) {
                            id_nss_number.setVisibility(View.VISIBLE);
                            maximum_layout.setVisibility(View.INVISIBLE);
                            extended_layout.setVisibility(View.INVISIBLE);
                        } else if (response.body().getData().getEmploymentStatus().equals("Employed")) {
                            maximum_layout.setVisibility(View.VISIBLE);
                            extended_layout.setVisibility(View.INVISIBLE);
                            id_nss_number.setVisibility(View.INVISIBLE);
                            id_type.setText(response.body().getData().getIdType());
                            id_number.setText(response.body().getData().getIdNumber());
                            id_expiry_date.setText(response.body().getData().getIdExpiryDate());
                        }


                    } else {
                        String message = response.body().getMessage();
                        Log.e("getProfileError", message);
                        Toast.makeText(UserProfileActivity.this, message, Toast.LENGTH_LONG).show();
                    }
                }catch (Exception e){
                    Toast.makeText(getApplicationContext(), "Some fields are empty", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }

            }


        }

        @Override
        public void onFailure(Throwable throwable) {
            Log.e("getProfileError", throwable.getMessage());
            Toast.makeText(UserProfileActivity.this, "Unable to Login, Please Try Again", Toast.LENGTH_LONG).show();
        }
    });
}

this is the exception that I get from the date format

I/dateError:: Unparseable date: "1988-11-09T00:00:00Z"
+4
source share
2 answers

yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 

Z " ", " Zulu" (UTC).

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));

UTC 20H.

, 17H.

0

, Date/DateError:: Unparseable date: "1988-11-09T00: 00: 00Z" ( 19)

( ). 19 - Z, . .

java.time, API Java, :

private static final DateTimeFormatter dateFormatter
        = DateTimeFormatter.ofPattern("dd/MM/yyyy");

private static String formatDate(String dateString) {
    return Instant.parse(dateString)
            .atZone(ZoneId.of("Australia/Queensland"))
            .format(dateFormatter);
}

:

    System.out.println(formatDate("2017-08-14T17:45:16.24Z"));
    System.out.println(formatDate("1988-11-09T00:00:00Z"));

15/08/2017
09/11/1988

, , 14. , , UTC 17:45, , , , . , , , /. UTC, atZone:

            .atOffset(ZoneOffset.UTC) 

, , .

0.24 . SimpleDateFormat 24 24 , 0,024 , . , static, , .

: java.time Android?

, java.time Android. Java 6.

  • Java 8 Android- API .
  • Java 6 7 ThreeTen Backport, backport (ThreeTen JSR 310, . ).
  • Android Android ThreeTen Backport. ThreeTenABP. , org.threeten.bp .

0

Source: https://habr.com/ru/post/1693689/


All Articles