我有一个字符串“11/15/2013 08:00:00”,我想将它格式化为“11/15/2013”,正确的DateTimeFormatter模式是什么?

我尝试了很多,谷歌了一下,仍然无法找到正确的模式。

编辑:我正在寻找Joda-Time DateTimeFormatter,而不是Java的SimpleDateFormat..


当前回答

请试试这个

public void Method(Datetime time)
{
    time.toString("yyyy-MM-dd'T'HH:mm:ss"));
}

其他回答

更新:

你可以:创建一个常量:

private static final DateTimeFormatter DATE_FORMATTER_YYYY_MM_DD =
          DateTimeFormat.forPattern("yyyy-MM-dd"); // or whatever pattern that you need.

这个DateTimeFormat是从下面导入的(小心)

进口org.joda.time.format.DateTimeFormat; 进口org.joda.time.format.DateTimeFormatter;

解析日期:

DateTime.parse(dateTimeScheduled.toString(), DATE_FORMATTER_YYYY_MM_DD);

以前: DateTime.parse(“201711201515”,DateTimeFormat.forPattern(“yyyyMMddHHmm”)).toString(“yyyyMMdd”);

如果需要datetime:

DateTime.parse("201711201515", DateTimeFormat.forPattern("yyyyMMddHHmm")).withTimeAtStartOfDay();

我有个很蠢但很管用的办法。 如果你有字符串fullDate = "11/15/2013 08:00:00";

   String finalDate = fullDate.split(" ")[0];

这应该工作简单和快速。:)

这是

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();
}

简单的方法:

DateTime date = new DateTime();
System.out.println(date.toString(DateTimeFormat.forPattern("yyyy-mm-dd")));

注意,在JAVA SE 8中有一个新的JAVA。time (JSR-310)包。此替换Joda时间,建议Joda用户迁移。对于JAVA SE≥8的日期和时间格式,请参见下文。

Joda的时间

使用DateTimeFormat.forPattern(String)创建一个DateTimeFormatter

使用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));

标准Java≥8

Java 8引入了一个新的日期和时间库,使其更容易处理日期和时间。如果您想使用标准Java版本8或更高版本,您将使用DateTimeFormatter。由于在字符串中没有时区,所以可以使用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));

标准Java < 8

在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));