I have a method that gets data from REST server. The method returns the date in this format "2017-08-14T17:45:16.24Z". i also wrote a method that formats date in the order "dd/MM/yyyy". This works perfectly but when i try to format the date from the server and set the that to an Edittext it does not work. This shows shows that my method to format the date in the server response does not work. My method to format the date is 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 below that gets the date from the server and formats the date and sets the date to an 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 i get from the date format
I/dateError:: Unparseable date: "1988-11-09T00:00:00Z"