Joda-Time DateTime을 mm / dd / yyyy로만 포맷하는 방법은 무엇입니까?
문자열 " 11/15/2013 08:00:00
" 이 있는데 " "로 형식을 지정하고 싶습니다 11/15/2013
. 올바른 DateTimeFormatter
패턴은 무엇입니까?
나는 많은 시도를했지만 여전히 올바른 패턴을 찾을 수 없습니다.
편집 : Java의 SimpleDateFormat이 아닌 Joda-Time을 찾고 있습니다 DateTimeFormatter
.
조다 시간
DateTimeFormatter
사용하여 만들기DateTimeFormat.forPattern(String)
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를 사용하십시오 . 에 시간대 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));
표준 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));
다른 답변이 완전히 받아 들일지라도 여기에 이것을 추가하고 있습니다. JodaTime에는 DateTimeFormat에 사전 구문 분석기가 내장되어 있습니다.
dateTime.toString(DateTimeFormat.longDate());
다음은 대부분의 옵션이 해당 형식으로 인쇄 된 것입니다.
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
DateTime date = DateTime.now().withTimeAtStartOfDay();
date.toString("HH:mm:ss")
I think this will work, if you are using 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.
I have a very dumb but working option. if you have the String fullDate = "11/15/2013 08:00:00";
String finalDate = fullDate.split(" ")[0];
That should work easy and fast. :)
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();
}
just want string:
DateTime.parse("201711201515",DateTimeFormat.forPattern("yyyyMMddHHmm")).toString("yyyyMMdd");
if want datetime:
DateTime.parse("201711201515", DateTimeFormat.forPattern("yyyyMMddHHmm")).withTimeAtStartOfDay();
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.
참고URL : https://stackoverflow.com/questions/20331163/how-to-format-joda-time-datetime-to-only-mm-dd-yyyy
'IT' 카테고리의 다른 글
스크립트가 소스인지 감지하는 방법 (0) | 2020.05.19 |
---|---|
의사 요소에서 SVG를 내용으로 사용하는 방법이 있습니까 : before 또는 : after (0) | 2020.05.19 |
SSMS 2008의“상위 200 개 행 편집”에서 SQL을 변경하는 방법 (0) | 2020.05.19 |
스위프트 : switch 문의 테스트 클래스 유형 (0) | 2020.05.19 |
git bash의 명령 줄에서 Python이 작동하지 않습니다. (0) | 2020.05.19 |