如何将Joda-Time DateTime格式化为mm/dd/yyyy?

[英]How to format Joda-Time DateTime to only mm/dd/yyyy?


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-Time DateTimeFormatter, not Java's SimpleDateFormat..

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

8 个解决方案

#1


301  

Joda time

Create a DateTimeFormatter using DateTimeFormat.forPattern(String)

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

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 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.LocalDateTime or a LocalDate, otherwise the time zoned varieties ZonedDateTime and ZonedDate could be used.

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

Standard Java < 8

Before Java 8, you would use the a SimpleDateFormat and 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));

#2


21  

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

#3


17  

DateTime date = DateTime.now().withTimeAtStartOfDay();
date.toString("HH:mm:ss")

#4


10  

I have a very dumb but working option. if you have the String fullDate = "11/15/2013 08:00:00";

我有一个非常愚蠢的工作选择。如果您有字符串fullDate = "11/15/2013 08:00:00";

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

That should work easy and fast. :)

那应该很容易而且很快。:)

#5


6  

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.

我从这里得到了一部分。

#6


1  

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()方法更快/使用更少的内存。

#7


1  

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

#8


0  

just want string:

只是希望字符串:

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

if want datetime:

如果希望datetime:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/12/02/bf2410f0855f46c823983a0e0905d1ef.html



 
  © 2014-2022 ITdaan.com 联系我们: