格式化日期字符串以在 JAVA 中删除时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18843429/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:34:15  来源:igfitidea点击:

Format date string to remove time in JAVA

javastringdate

提问by dsi

Example, String value is:

示例,字符串值为:

15/08/2013 15:30 GMT+10:00 

I want to format above string to 15/08/2013(remove time part and only keep date) in Java.

我想15/08/2013在 Java 中将上面的字符串格式化为(删除时间部分并只保留日期)。

How can I do this format?

我怎样才能做到这种格式?

采纳答案by Suresh Atta

Remove time part and only keep date

删除时间部分,只保留日期

String dateString= "15/08/2013 15:30 GMT+10:00";
String result  = dateString.split(" ")[0];

That gives you 15/08/2013

那给你 15/08/2013

There is no need to formatter, I guess.

我想不需要格式化程序。

回答by vcetinick

Check out the DateFormat class, http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

查看 DateFormat 类, http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

You should be able to parse in the date using a parser and then write out your desired format in another pattern

您应该能够使用解析器解析日期,然后以另一种模式写出所需的格式

i.e. new SimpleDateFormat("dd/MM/yyyy hh:mm z") for inbound

即新的 SimpleDateFormat("dd/MM/yyyy hh:mm z") 用于入站

new SimpleDateFormat("dd/MM/yyyy") for your updated format

new SimpleDateFormat("dd/MM/yyyy") 为您更新的格式

回答by Lefteris Laskaridis

回答by Jesper

If this is nothing more than you have a string containing 15/08/2013 15:30 GMT+10:00and you just want the date part, which is the first 10 characters of the string, I'd just take the first 10 characters; no need to parse and format it as a date:

如果这只不过是一个包含字符串的字符串,15/08/2013 15:30 GMT+10:00而您只想要日期部分,即字符串的前 10 个字符,我只取前 10 个字符;无需解析并将其格式化为日期:

String input = "15/08/2013 15:30 GMT+10:00";
String result = input.substring(0, 10);

回答by Ran Adler

DateFormat formatter = new SimpleDateFormat(format);
Date date = (Date) formatter.parse(dateStr);

回答by MadProgrammer

2018

2018年

Since it's now 2018 and we have the date/time API in Java 8+ (and the ThreeTen Backport), you could do something like...

由于现在是 2018 年,而且我们在 Java 8+(和ThreeTen Backport)中有日期/时间 API ,你可以做类似的事情......

String text = "15/08/2013 15:30 GMT+10:00";
LocalDateTime ldt = LocalDateTime.parse(text, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm z", Locale.ENGLISH));
System.out.println(ldt);

// In case you just want to do some "date" manipulation, without the time component
LocalDate ld = ldt.toLocalDate();
// Will produce "2013-08-15"
//String format = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE);
String format = ldt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(format);

Original Answer

原答案

One approach would be to parse the Stringdate to a Dateobject and then simply format as per your requirements

一种方法是将String日期解析为Date对象,然后根据您的要求简单地格式化

String text = "15/08/2013 15:30 GMT+10:00";
SimpleDateFormat inFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm z");
Date date = inFormat.parse(text);
System.out.println(date);

SimpleDateFormat outFormat = new SimpleDateFormat("dd/MM/yyyy");
String formatted = outFormat.format(date);
System.out.println(formatted);

This has the nice benefit of preserving the date/time information in the Dateshould you need it for other things ;)

Date如果您需要它来做其他事情,这有一个很好的好处,可以保留日期/时间信息;)

See, SimpleDateFormatfor more details

请参阅,SimpleDateFormat了解更多详情