IT

Joda-Time DateTime을 mm / dd / yyyy로만 포맷하는 방법은 무엇입니까?

lottoking 2020. 5. 19. 08:27
반응형

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입니다 . 그렇지 않으면 시간대가 지정된 ZonedDateTimeZonedDate를 사용할 수 있습니다.

// 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 이전에는 SimpleDateFormatjava.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

반응형