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。
301
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));
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));
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));
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
17
DateTime date = DateTime.now().withTimeAtStartOfDay();
date.toString("HH:mm:ss")
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. :)
那應該很容易而且很快。:)
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.
我從這里得到了一部分。
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()方法更快/使用更少的內存。
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();
}
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。