【发布时间】:2017-12-07 18:49:34
【问题描述】:
我知道有很多类似的问题,到目前为止,我尝试过但没有完全回答我的问题的一些解决方案是:this、this、this、this、@ 987654325@、this、this、this、this、this 等等。
我正在制作一个应用程序,我从服务器获取用户的帖子并按日期对其进行排序,并显示它在帖子中创建的日期和时间。现在我有一个可行的解决方案,我正在将来自服务器的字符串以yyyy-MM-dd HH:mm:ss 格式转换为我的自定义日期和时间格式。
问题是:我不想对格式进行硬编码,而是想以用户手机的格式显示它,并且分别为日期和时间。我的意思是手机的本地格式 - am-pm/24 小时格式,日期格式相同 - dd-MM-YY、MM-dd-YYYY 或 dd-MM 等。
到目前为止,我已经浏览了很多解决方案,其中大部分都采用硬编码格式。我尽量避免,但我认为另一个问题是我想将日期和时间与日期时间字符串分开。另一个问题是日期和时间的转换不是分开的。
简而言之,我想将2017-07-04 12:00:00 格式化为04 Jul 2017 和12:00 PM,或者07/04/17 和12:00。 IE。 将日期和时间分成两个字符串,并以本地化电话格式显示。
我的适配器:
public class UserPostsAdapter extends RecyclerView.Adapter<UserPostsAdapter.PostsViewHolder> {
private List<UserPosts> postItems;
private Context context;
private static final String TAG = UserPosts.class.getSimpleName();
class PostsViewHolder extends RecyclerView.ViewHolder {
TextView userPostsTitle, userPostsDate, userPostsTime, userPost, textViewStars, textViewComments;
ImageView imageViewDisplayComments, imageViewReply, imageViewCommentStar;
String pCcontactId, postId;
PostsViewHolder(View itemView) {
super(itemView);
userPostsTitle = (TextView) itemView.findViewById(R.id.userPostsTitle);
userPostsDate = (TextView) itemView.findViewById(R.id.userPostsDate);
userPostsTime = (TextView) itemView.findViewById(R.id.userPostsTime);
userPost = (TextView) itemView.findViewById(R.id.userPost);
textViewStars = (TextView) itemView.findViewById(R.id.textViewStars);
textViewComments = (TextView) itemView.findViewById(R.id.textViewComments);
imageViewCommentStar = (ImageView) itemView.findViewById(R.id.imageViewCommentStar);
imageViewDisplayComments = (ImageView) itemView.findViewById(R.id.imageViewDisplayComments);
imageViewReply = (ImageView) itemView.findViewById(R.id.imageViewReply);
}
}
public UserPostsAdapter(List<UserPosts> postsList, Context context) {
this.postItems = postsList;
this.context = context;
}
@Override
public UserPostsAdapter.PostsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_user_post, parent, false);
return new UserPostsAdapter.PostsViewHolder(itemView);
}
@Override
public void onBindViewHolder(final UserPostsAdapter.PostsViewHolder holder, int position) {
final UserPosts post = postItems.get(position);
SQLiteHandler db = new SQLiteHandler(context.getApplicationContext());
HashMap<String, String> user = db.getUserDetails();
final String currentUserId = user.get("uid");
if (post.getTitle() != null && !post.getTitle().isEmpty()) {
holder.userPostsTitle.setText(post.getTitle());
} else {
holder.userPostsTitle.setVisibility(View.GONE);
}
holder.userPostsDate.setText(convertDate(postItems.get(position).getCreatedDate()));
holder.userPostsTime.setText(convertTime(postItems.get(position).getCreatedTime()));
holder.userPost.setText(post.getPostedText());
if (Integer.parseInt(post.getLikesNumber()) > 0) {
holder.textViewStars.setText(post.getLikesNumber());
}
holder.textViewComments.setText(post.getCommentsNumber());
holder.pCcontactId = post.getUserId();
final String postId = holder.postId = post.getId();
holder.imageViewDisplayComments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
***TRANSFORMING DATE AND TIME SEPARATELY TO CUSTOM FORMAT***
-----------------------------------------------------------------------------
private String convertDate(String time) {
SimpleDateFormat dateFormatReceived = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
SimpleDateFormat dateFormatConverted = new SimpleDateFormat("dd MMM yyyy", Locale.getDefault());
java.util.Locale.getDefault());
SimpleDateFormat(datePattern);
DateFormat.getTimeInstance(DateFormat.SHORT).parse(mTimeDisplay.getText().toString());
java.util.Date date = null;
try {
date = dateFormatReceived.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormatConverted.format(date);
}
private String convertTime(String time) {
SimpleDateFormat timeFormatReceived = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
SimpleDateFormat timeFormatConverted = new SimpleDateFormat("HH:mm", Locale.getDefault());
java.util.Date date = null;
try {
date = timeFormatReceived.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return timeFormatConverted.format(date);
}
-----------------------------------------------------------------------------
@Override
public int getItemCount() {
return postItems.size();
}
}
【问题讨论】:
-
@UsmanRana 谢谢你的回答乌斯曼。我确实尝试过,但它没有回答我的问题。我要做的是从服务器获取此字符串:
yyyy-MM-dd HH:mm:ss,将其切割成两个字符串:yyyy-MM-dd和HH:mm:ss,并将它们分别格式化为本地格式。 -
让我逐步解释: 1)您可以了解设备的默认日期格式。 2) 您必须知道服务器发送给您的日期格式。 3)创建一个日历实例并将日期时间设置为来自服务器。 4) 然后您可以按以下方式格式化日期和时间(SimpleDateFormat 的输入是您在步骤 1 中确定的格式): String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime ()); String formattedTime = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
-
@UsmanRana 感谢您的解决方案,并在投票前阅读我的问题 :) 你能回答这个问题,所以我可以接受你的回答 :) 再次感谢您的帮助
-
感谢您在询问之前搜索高低。我认为您没有找到重复项的原因之一是您确实在一个问题中至少提出了两个问题:(1)如何解析来自服务器的日期时间字符串(2)如何使用格式化日期和时间设备的格式设置。如果可以的话,最好发布两个单独的问题。
标签: java android date-format datetime-format time-format