Java 如何将 Joda-Time DateTime 格式化为只有 mm/dd/yyyy?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20331163/
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 format Joda-Time DateTime to only mm/dd/yyyy?
提问by iCodeLikeImDrunk
I have a string "11/15/2013 08:00:00
", I want to format it to "11/15/2013
", what is the correct DateTimeFormatter
pattern?
我有一个字符串“ 11/15/2013 08:00:00
”,我想将它格式化为“ 11/15/2013
”,正确的DateTimeFormatter
模式是什么?
I've tried many and googled and still unable to find the correct pattern.
我已经尝试了很多并用谷歌搜索,但仍然无法找到正确的模式。
edit: I am looking for Joda-TimeDateTimeFormatter
, not Java's SimpleDateFormat..
编辑:我正在寻找Joda-TimeDateTimeFormatter
,而不是 Java 的 SimpleDateFormat ..
采纳答案by DeltaLima
Joda time
乔达时间
Create a DateTimeFormatter
using DateTimeFormat.forPattern(String)
创建一个DateTimeFormatter
使用DateTimeFormat.forPattern(String)
Using Joda time you would do it like this:
使用 Joda 时间,您可以这样做:
String dateTime = "11/15/2013 08:00:00";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy");
// Printing the date
System.out.println(dtfOut.print(jodatime));
Standard Java ≥ 8
标准 Java ≥ 8
Java 8 introduced a new Date and Time library, making it easier to deal with dates and times. If you want to use standard Java version 8 or beyond, you would use a DateTimeFormatter. Since you don't have a time zone in your String
, a java.time.LocalDateTimeor a LocalDate, otherwise the time zoned varieties ZonedDateTimeand ZonedDatecould be used.
Java 8 引入了一个新的日期和时间库,可以更轻松地处理日期和时间。如果要使用标准 Java 版本 8 或更高版本,则应使用DateTimeFormatter。由于您的String
、java.time.LocalDateTime或LocalDate中没有时区,否则可以使用时区变体ZonedDateTime和ZonedDate。
// Format for input
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
LocalDate date = LocalDate.parse(dateTime, inputFormat);
// Format for output
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
// Printing the date
System.out.println(date.format(outputFormat));
Standard Java < 8
标准 Java < 8
Before Java 8, you would use the a SimpleDateFormatand java.util.Date
在 Java 8 之前,您将使用SimpleDateFormat和java.util.Date
String dateTime = "11/15/2013 08:00:00";
// Format for input
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Parsing the date
Date date7 = dateParser.parse(dateTime);
// Format for output
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
// Printing the date
System.out.println(dateFormatter.format(date7));
回答by Clad Clad
I have a very dumb but working option. if you have the String fullDate = "11/15/2013 08:00:00";
我有一个非常愚蠢但可行的选择。如果你有 String fullDate = "11/15/2013 08:00:00";
String finalDate = fullDate.split(" ")[0];
That should work easy and fast. :)
这应该可以轻松快速地工作。:)
回答by The Guy with The Hat
Another way of doing that is:
另一种方法是:
String date = dateAndTime.substring(0, dateAndTime.indexOf(" "));
I'm not exactly certain, but I think this might be faster/use less memory than using the .split()
method.
我不确定,但我认为这可能比使用该.split()
方法更快/使用更少的内存。
回答by The Guy with The Hat
I think this will work, if you are using JodaTime:
如果您使用 JodaTime,我认为这会起作用:
String strDateTime = "11/15/2013 08:00:00";
DateTime dateTime = DateTime.parse(strDateTime);
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/YYYY");
String strDateOnly = fmt.print(dateTime);
I got part of this from here.
我从这里得到了一部分。
回答by Eddie
This works
这有效
String x = "22/06/2012";
String y = "25/10/2014";
String datestart = x;
String datestop = y;
//DateTimeFormatter format = DateTimeFormat.forPattern("dd/mm/yyyy");
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(datestart);
d2 = format.parse(datestop);
DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
//Period
period = new Period (dt1,dt2);
//calculate days
int days = Days.daysBetween(dt1, dt2).getDays();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
回答by sebastian krüger
DateTime date = DateTime.now().withTimeAtStartOfDay();
date.toString("HH:mm:ss")
回答by Chad Bingham
I am adding this here even though the other answers are completely acceptable. JodaTime has parsers pre built in DateTimeFormat:
即使其他答案是完全可以接受的,我还是在这里添加了这个。JodaTime 在 DateTimeFormat 中预先构建了解析器:
dateTime.toString(DateTimeFormat.longDate());
This is most of the options printed out with their format:
这是打印出的大多数选项及其格式:
shortDate: 11/3/16
shortDateTime: 11/3/16 4:25 AM
mediumDate: Nov 3, 2016
mediumDateTime: Nov 3, 2016 4:25:35 AM
longDate: November 3, 2016
longDateTime: November 3, 2016 4:25:35 AM MDT
fullDate: Thursday, November 3, 2016
fullDateTime: Thursday, November 3, 2016 4:25:35 AM Mountain Daylight Time
回答by Hymany.cheng
UPDATED:
更新:
You can: create a constant:
您可以: 创建一个常量:
private static final DateTimeFormatter DATE_FORMATTER_YYYY_MM_DD =
DateTimeFormat.forPattern("yyyy-MM-dd"); // or whatever pattern that you need.
This DateTimeFormat is importing from: (be careful with that)
这个 DateTimeFormat 是从以下导入的:(小心)
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter;
导入 org.joda.time.format.DateTimeFormat; 导入 org.joda.time.format.DateTimeFormatter;
Parse the Date with:
解析日期:
DateTime.parse(dateTimeScheduled.toString(), DATE_FORMATTER_YYYY_MM_DD);
Before:
DateTime.parse("201711201515",DateTimeFormat.forPattern("yyyyMMddHHmm")).toString("yyyyMMdd");
之前:
DateTime.parse("201711201515",DateTimeFormat.forPattern("yyyyMMddHHmm")).toString("yyyyMMdd");
if want datetime:
如果想要日期时间:
DateTime.parse("201711201515", DateTimeFormat.forPattern("yyyyMMddHHmm")).withTimeAtStartOfDay();