【问题标题】:How to Format Date in android from String如何从字符串格式化android中的日期
【发布时间】:2019-11-16 01:50:50
【问题描述】:
日志打印
dateStr -> 2019-07-05 12:05:36
currentDate -> Fri Jul 05 00:00:00 GMT+02:00 2019
我需要 -> Fri 05 Jul 2019 并且还翻译成马其顿语言环境
String dateStr = obj.getString("sent_date");
Log.d("date", dateStr);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = null;
try {
currentDate = sdf.parse(dateStr);
Log.d("date", currentDate.toString());
【问题讨论】:
标签:
android
date
simpledateformat
localdate
【解决方案1】:
如果您想在 Macedonian 本地使用 SimpleDateFormat,您可以这样做:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("mk", "MK"));
或者您可以通过这种方式使用 DateFormat:
Date date = new Date(location.getTime());
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
myDate = df.parse(dateStr);
有关更多格式选项,您可以查看docs
【解决方案2】:
如果您正在寻找日期格式字符串,
SimpleDateFormat resultFormat = new SimpleDateFormat("E dd MMM yyyy", new Locale("mk", "MK"));
resultFormat.format(currentDate)
应该会给你预期的结果。
【解决方案3】:
试试这个代码:
@SuppressLint("SimpleDateFormat")
public static String formatTime(String dateFormat) {
String inputTimePattern = "EEE MMM dd HH:mm:ss zzzz yyyy";
String outputTimePattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputTimePattern, Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputTimePattern, new Locale("mk" ,""));
Date date;
try {
date = inputFormat.parse(dateFormat);
return outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return dateFormat;
}
Log.d(TAG, formatTime("Fri Jul 05 00:00:00 GMT+02:00 2019"));
Output -> 2019-07-05 00:00:00
【解决方案4】:
您可以使用此功能只需输入您想要的日期格式
private String returnData(String date) {
SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd");
localDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
localDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
String finalDate = localDateFormat.format(new Date());
return finalDate;
}