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));
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"
source
share