android 项目中的日期格式
Date Formatting in android project
我正在制作新闻 Android 应用程序。
我使用 JSON 解析从 NewaApi 获取的所有数据。
我还以 'YYYY-MM-DD' 格式从 API 收集日期信息。
我想将格式转换为 DD-MM-YYYY。
这是我的适配器代码 class.
public class NewsAdapter extends ArrayAdapter<NewsData> {
public NewsAdapter(Context context, List<NewsData> news) {
super(context, 0, news);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.news_list, parent, false);
}
NewsData currentNews = getItem(position);
TextView headlineView = listItemView.findViewById(R.id.headline);
headlineView.setText(currentNews.getmHeadline());
String originalTime = currentNews.getmDate_time();
String date;
if (originalTime.contains("T")) {
String[] parts = originalTime.split("T");
date = parts[0];
} else {
date = getContext().getString(R.string.not_avilalble);
}
TextView dateView = listItemView.findViewById(R.id.date);
dateView.setText(date);
String imageUri=currentNews.getmImageUrl();
ImageView newsImage = listItemView.findViewById(R.id.news_image);
Picasso.with(getContext()).load(imageUri).into(newsImage);
return listItemView;
}
}
我还添加了格式在 JSON 中的图像。
如果是模式yyyy-MM-dd
,你可以解析为LocalDate
;
如果它在模式 yyyy-MM-dd'T'HH:mm:ss'Z'
中,您可以将其解析为 OffsetDateTime
,然后截断为 LocalDate
。
示例代码:
public static String convert(String originalTime) {
LocalDate localDate;
if (originalTime.contains("T")) {
localDate = OffsetDateTime.parse(originalTime).toLocalDate();
} else {
localDate = LocalDate.parse(originalTime);
}
return localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}
测试用例:
public static void main(String args[]) throws Exception {
System.out.println(convert("2000-11-10")); // 10-11-2000
System.out.println(convert("2000-11-10T00:00:01Z")); // 10-11-2000
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = sdf.parse(originalTime);
String newDate = new SimpleDateFormat("dd-MM-yyyy").format(date);
在 utils class 中使用这个方法,然后在任何你想要的地方调用这个方法,如 Utils.getDate("your date here").
public static String getDate(String ourDate) {
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
try {
d = input.parse("2018-02-02T06:54:57.744Z");
} catch (ParseException e) {
e.printStackTrace();
}
String formatted = output.format(d);
Log.i("DATE", "" + formatted);
return formatted;
}
我正在制作新闻 Android 应用程序。 我使用 JSON 解析从 NewaApi 获取的所有数据。 我还以 'YYYY-MM-DD' 格式从 API 收集日期信息。 我想将格式转换为 DD-MM-YYYY。 这是我的适配器代码 class.
public class NewsAdapter extends ArrayAdapter<NewsData> {
public NewsAdapter(Context context, List<NewsData> news) {
super(context, 0, news);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.news_list, parent, false);
}
NewsData currentNews = getItem(position);
TextView headlineView = listItemView.findViewById(R.id.headline);
headlineView.setText(currentNews.getmHeadline());
String originalTime = currentNews.getmDate_time();
String date;
if (originalTime.contains("T")) {
String[] parts = originalTime.split("T");
date = parts[0];
} else {
date = getContext().getString(R.string.not_avilalble);
}
TextView dateView = listItemView.findViewById(R.id.date);
dateView.setText(date);
String imageUri=currentNews.getmImageUrl();
ImageView newsImage = listItemView.findViewById(R.id.news_image);
Picasso.with(getContext()).load(imageUri).into(newsImage);
return listItemView;
}
}
我还添加了格式在 JSON 中的图像。
如果是模式yyyy-MM-dd
,你可以解析为LocalDate
;
如果它在模式 yyyy-MM-dd'T'HH:mm:ss'Z'
中,您可以将其解析为 OffsetDateTime
,然后截断为 LocalDate
。
示例代码:
public static String convert(String originalTime) {
LocalDate localDate;
if (originalTime.contains("T")) {
localDate = OffsetDateTime.parse(originalTime).toLocalDate();
} else {
localDate = LocalDate.parse(originalTime);
}
return localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}
测试用例:
public static void main(String args[]) throws Exception {
System.out.println(convert("2000-11-10")); // 10-11-2000
System.out.println(convert("2000-11-10T00:00:01Z")); // 10-11-2000
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = sdf.parse(originalTime);
String newDate = new SimpleDateFormat("dd-MM-yyyy").format(date);
在 utils class 中使用这个方法,然后在任何你想要的地方调用这个方法,如 Utils.getDate("your date here").
public static String getDate(String ourDate) {
SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
try {
d = input.parse("2018-02-02T06:54:57.744Z");
} catch (ParseException e) {
e.printStackTrace();
}
String formatted = output.format(d);
Log.i("DATE", "" + formatted);
return formatted;
}