![](/img/trans.png)
[英]Rest API response as XML format, calling from Spring Boot with Resttemplate
[英]Format Date on Server Response from REST API not working
我有一个从REST服务器获取数据的方法。 该方法以“2017-08-14T17:45:16.24Z”格式返回日期。 我还写了一个方法,按照“dd / MM / yyyy”的顺序格式化日期。 这很好用但是当我尝试从服务器格式化日期并将其设置为Edittext时它不起作用。 这显示我在服务器响应中格式化日期的方法不起作用。 格式化日期的方法如下:
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 "";
}
下面的方法从服务器获取日期并格式化日期并将日期设置为编辑文本。
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();
}
});
}
这是我从日期格式得到的例外
I/dateError:: Unparseable date: "1988-11-09T00:00:00Z"
您想要的格式是
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
最后的Z代表“零小时偏移”,也称为“祖鲁时间”(UTC)。
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
有了这个UTC
时区,转换时间将是20H
。
如果您不添加时区,您的预计小时数为17H
。
这是我从我的formatDate I / dateError :: Unparseable日期得到的错误:“1988-11-09T00:00:00Z”(偏移19处)
该日期时间字符串在第二个字符串上没有小数(并且没有小数点)。 偏移19是Z
所在的位置,小数点位于格式模式字符串中。 这会导致您的异常。
java.time
,现代Java日期和时间API,将轻松解决您的问题:
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的月份的第15行。这是因为当UTC时间为17:45时,它已经是澳大利亚的第二天了。 我想你想在用户的时区里度过这一天。 因此,如果碰巧不是澳大利亚/昆士兰州,请替换您想要的时区。 如果您确实想要UTC atZone
的日期,请使用此行而不是atZone
调用:
.atOffset(ZoneOffset.UTC)
这将确保您获得与字符串相同的日期。
作为一个细节,上面的代码也正确地解析你的0.24秒。 SimpleDateFormat
将24理解为24毫秒,所以你得到了0.024秒,略有不准确。 另一个细节,我声明你的方法是static
,没有必要,但我们也可以。
是的,您可以在Android上使用java.time
。 它至少需要Java 6 。
org.threeten.bp
的日期和时间类。 java.time
。 java.time
。 java.time
到Java 6和7(用于JSR-310的ThreeTen)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.