Java 如何在android中将Date转换为特定格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35939337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Date to a particular format in android?
提问by Rahul Verma
"Mar 10, 2016 6:30:00 PM" This is my date and I want to convert this into "10 Mar 2016". Can I use SimpleDateFormat in android. I am not getting the exact pattern to convert it. Please help and thanks in advance
“2016 年 3 月 10 日下午 6:30:00”这是我的日期,我想将其转换为“2016 年 3 月 10 日”。我可以在android中使用SimpleDateFormat吗?我没有得到转换它的确切模式。请帮助并提前致谢
String date="Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf=new SimpleDateFormat("Some Pattern for above date");
Date newDate=spf.format(date);
spf= new SimpleDateFormat("dd MMM yyyy");
String date = spf.format(newDate);
Will this steps work? If yes, can someone please give me a pattern of that format? Thanks in advance.
这个步骤会起作用吗?如果是的话,有人可以给我这种格式的模式吗?提前致谢。
采纳答案by user987339
This is modified code that you should use:
这是您应该使用的修改后的代码:
String date="Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf=new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa");
Date newDate=spf.parse(date);
spf= new SimpleDateFormat("dd MMM yyyy");
date = spf.format(newDate);
System.out.println(date);
Use hh
for hours in order to get correct time.
使用hh
数小时以获得正确的时间。
Java 8 and later
Java 8 及更高版本
Java 8 introduced new classes for time manipulation, so use following code in such cases:
Java 8 引入了用于时间操作的新类,因此在这种情况下使用以下代码:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a");
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy");
System.out.println(dateTime.format(formatter2));
Use h
for hour format, since in this case hour has only one digit.
使用h
的小时格式,因为在这种情况下,每小时只有一个数字。
回答by user2004685
You should parse()
the String into Date and then format it into the desired format. You can use MMM dd, yyyy HH:mm:ss a
format to parse the given String.
您应该parse()
将字符串转换为日期,然后将其格式化为所需的格式。您可以使用MMM dd, yyyy HH:mm:ss a
格式来解析给定的字符串。
Here is the code snippet:
这是代码片段:
public static void main (String[] args) throws Exception
{
String date = "Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
Date newDate = spf.parse(date);
spf = new SimpleDateFormat("dd MMM yyyy");
String newDateString = spf.format(newDate);
System.out.println(newDateString);
}
Output:
输出:
10 Mar 2016
回答by Rahul Sharma
I have created a method for this problem. We simply need to pass Current date format, required date formatand Date String.
我已经为这个问题创建了一个方法。我们只需要传递当前日期格式、所需日期格式和日期字符串。
private String changeDateFormat(String currentFormat,String requiredFormat,String dateString){
String result="";
if (Strings.isNullOrEmpty(dateString)){
return result;
}
SimpleDateFormat formatterOld = new SimpleDateFormat(currentFormat, Locale.getDefault());
SimpleDateFormat formatterNew = new SimpleDateFormat(requiredFormat, Locale.getDefault());
Date date=null;
try {
date = formatterOld.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
result = formatterNew.format(date);
}
return result;
}
This method will return Date String in format you require. In your case method call will be:
此方法将以您需要的格式返回日期字符串。在您的情况下,方法调用将是:
String date = changeDateFormat("MMM dd, yyyy hh:mm:ss a","dd MMM yyyy","Mar 10, 2016 6:30:00 PM");
回答by Ole V.V.
For the sake of completeness, here is the modern version. This is for anyone reading this who either uses Java 8 or later or is happy with a (good and futureproof) external library.
为了完整起见,这里是现代版本。这适用于任何使用 Java 8 或更高版本或对(良好且面向未来的)外部库感到满意的人阅读本文。
String date = "Mar 10, 2016 6:30:00 PM";
DateTimeFormatter parseFormatter
= DateTimeFormatter.ofPattern("MMM d, uuuu h:mm:ss a", Locale.ENGLISH);
DateTimeFormatter newFormatter
= DateTimeFormatter.ofPattern("d MMM uuuu", Locale.ENGLISH);
date = LocalDateTime.parse(date, parseFormatter).format(newFormatter);
System.out.println(date);
This prints the desired
这将打印所需的
10 Mar 2016
Please note the use of explicit locale for both DateTimeFormatter
objects. “Mar” and “PM” both are in English, so neither the parsing nor the formatting will work unless some English-speaking locale is used. By giving it explicitly we are making the code robust enough to behave as expected also on computers and JVMs with other default locales.
请注意对两个DateTimeFormatter
对象都使用显式语言环境。“Mar”和“PM”都是英文的,所以除非使用一些英语语言环境,否则解析和格式化都不起作用。通过明确给出它,我们使代码足够健壮,可以在具有其他默认语言环境的计算机和 JVM 上按预期运行。
To use the above on Android, use ThreeTenABP, please see How to use ThreeTenABP in Android Project. On other Java 6 and 7 use ThreeTen Backport.
要在 Android 上使用上述内容,请使用 ThreeTenABP,请参阅如何在 Android 项目中使用 ThreeTenABP。在其他 Java 6 和 7 上使用ThreeTen Backport。
回答by Syed Danish Haider
conversion from string to date and date to string
从字符串到日期和日期到字符串的转换
String deliveryDate="2018-09-04";
SimpleDateFormat dateFormatprev = new SimpleDateFormat("yyyy-MM-dd");
Date d = dateFormatprev.parse(deliveryDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE dd MMM yyyy");
String changedDate = dateFormat.format(d);
回答by Ketan Ramani
public static String formatDate(String fromFormat, String toFormat, String dateToFormat) {
SimpleDateFormat inFormat = new SimpleDateFormat(fromFormat);
Date date = null;
try {
date = inFormat.parse(dateToFormat);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outFormat = new SimpleDateFormat(toFormat);
return outFormat.format(date);
}
Use:formatDate("dd-MM-yyyy", "EEEE, dd MMMM yyyy","26-07-2019");
使用:formatDate("dd-MM-yyyy", "EEEE, dd MMMM yyyy","26-07-2019");
Result:Friday, 26 July 2019
结果:2019 年 7 月 26 日,星期五
回答by Dhirendra Gautam
You need to use SimpleDateFormat class to do the needful for you
你需要使用 SimpleDateFormat 类来为你做需要的
String date = "Your input date"
DateFormat originalFormat = new SimpleDateFormat("<Your Input format here>", Locale.US)
DateFormat targetFormat = new SimpleDateFormat("<Your desired format here>", Locale.US)
Date Fdate = originalFormat.parse(date)
formattedDate = targetFormat.format(Fdate)