我正在使用改造。我从服务器获取数据(日期和时间)。但创建日期为(2016-01-22 15:22:37)
。我想只显示日期(2016-01-22)
。
user = StoreUtil.getInstance().selectFrom("users");
NetworkEngine.getInstance().getNotSending(user.getId(), new Callback<List<MyPending>>() {
@Override
public void success(List<MyPending> myPendings, Response response) {
txt_taker_name.setText(getResources().getString(R.string.str_taker_name)
+ ":" + " " + myPendings.get(0).getShippingInformation().getName());
txt_taker_address.setText(getResources().getString(R.string.str_taker_address)
+ ":" + " " + myPendings.get(0).getShippingInformation().getAddress());
txt_cargo_name.setText(getResources().getString(R.string.str_cargo_name)
+ ":" + " " + myPendings.get(0).getShippingTransactionDetail().get(0).getCargo().getName());
txt_cargo_qty.setText(getResources().getString(R.string.str_cargo_qty)
+ ":" + " " + myPendings.get(0).getShippingTransactionDetail().get(0).getCargo().getQuantity());
txt_total_weight.setText(getResources().getString(R.string.str_confirm_totalweight)
+ ":" + " " + myPendings.get(0).getTotalWeight());
txt_total_deliver_charges.setText(getResources().getString(R.string.str_confirm_totalcharges)
+ ":" + " " + myPendings.get(0).getTotalCost());
String date = new SimpleDateFormat("yyyy-MM-dd").format(
myPendings.get(0).getShippingTransactionDetail().get(0).getCargo().getCreatedAt());
txt_deliver_date.setText(getResources().getString(R.string.str_deliver_date) + ":" + " " + date);
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
dialog.dismissWithFailure();
}
});
答案 0 :(得分:2)
简单的方法就是分割日期
String dates = myPendings.get(0).getShippingTransactionDetail().get(0).getCargo().getCreatedAt();
if(!dates.equalsIgnoreCase("")){
String date[] = dates.split(" ");
Log.e("FINAL DATE-->","-->"+date[0]);
}
答案 1 :(得分:0)
我正在使用我用来将日期格式化为特定格式的代码。试试吧。
String mytime="Jan 17, 2012";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MMM dd, yyyy");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
答案 2 :(得分:0)
使用此方法转换日期
public String changedate(String pastdate) {
final String OLD_FORMAT = "yyyy-MM-dd hh:mm:ss";
final String NEW_FORMAT = "yyyy-MM-dd";
String newDateString = "";
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
try {
Date d = sdf.parse(pastdate);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
}catch (ParseException e)
{
}
return newDateString;
}