📝UTC 시간 Local Time으로 변경하기
리사이클러뷰에 데이터를 가져와 표시했는데 시간이 UTC로 되어있어 Local Time으로 변경해줬다
SimpleDateFormat sf;
SimpleDateFormat df;
// UTC > LocalTime
sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sf.setTimeZone(TimeZone.getTimeZone("UTC"));
df.setTimeZone(TimeZone.getDefault());
try {
Date date = sf.parse(posting.getCreatedAt());
holder.txtCreatedAt.setText(df.format(date));
} catch (ParseException e) {
}
사용한 전체 코드
public class PostingAdapter extends RecyclerView.Adapter<PostingAdapter.ViewHolder> {
Context context;
ArrayList<Posting> postingList;
SimpleDateFormat sf;
SimpleDateFormat df;
public PostingAdapter(Context context, ArrayList<Posting> postingList) {
this.context = context;
this.postingList = postingList;
// UTC > LocalTime
sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sf.setTimeZone(TimeZone.getTimeZone("UTC"));
df.setTimeZone(TimeZone.getDefault());
}
@NonNull
@Override
public PostingAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.posting_row, parent, false);
return new PostingAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostingAdapter.ViewHolder holder, int position) {
Posting posting = postingList.get(position);
holder.txtContent.setText(posting.getContent());
holder.txtEmail.setText(posting.getEmail());
try {
Date date = sf.parse(posting.getCreatedAt());
holder.txtCreatedAt.setText(df.format(date));
} catch (ParseException e) {
}
if (posting.getIsLike() == 1) {
holder.imgLike.setImageResource(R.drawable.ic_thumb_up_2);
} else {
holder.imgLike.setImageResource(R.drawable.ic_thumb_up_1);
}
Glide.with(context)
.load(posting.getImgUrl())
.placeholder(R.drawable.outline_photo_24)
.into(holder.imgPhoto);
}
@Override
public int getItemCount() {
return postingList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
TextView txtContent;
TextView txtEmail;
TextView txtCreatedAt;
ImageView imgLike;
ImageView imgPhoto;
public ViewHolder(@NonNull View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.cardView);
txtContent = itemView.findViewById(R.id.txtContent);
txtEmail = itemView.findViewById(R.id.txtEmail);
txtCreatedAt = itemView.findViewById(R.id.txtCreatedAt);
imgLike = itemView.findViewById(R.id.imgLike);
imgPhoto = itemView.findViewById(R.id.imgPhoto);
}
});
}
}
}
'Android Studio' 카테고리의 다른 글
[Android Studio] TabBar 만들기 (0) | 2023.02.15 |
---|---|
[Android Studio] 내가 만든 좋아요 API 기능 구현하기 (0) | 2023.02.14 |
[Android Studio] Retrofit으로 API호출할때 form-data 처리하기 (0) | 2023.02.14 |
[Android Studio] AlertDialog array로 사용하기 (0) | 2023.02.13 |
[Android Studio] 카메라, 앨범 사용하기 (0) | 2023.02.13 |
댓글