programing

Joda-Time DateTime을 mm/dd/yyyy로만 포맷하는 방법

coolbiz 2022. 7. 7. 23:39
반응형

Joda-Time DateTime을 mm/dd/yyyy로만 포맷하는 방법

난 '줄'이 있어11/15/2013 08:00:00"로 포맷하고 싶다.11/15/2013", 올바른 것은 무엇입니까?DateTimeFormatter패턴?

여러 번 검색해 봤지만 정확한 패턴을 찾을 수 없어요.

edit : Java의 Simple Date Format이 아닌 Joda-Time을 찾고 있습니다.

JAVA SE 8에서는 새로운 java.time(JSR-310) 패키지가 도입되었습니다.이것은 Joda 시간을 대체하며, Joda 사용자는 마이그레이션하는 것이 좋습니다.JAVA SE © 8가지 형식의 날짜와 시각에 대해서는, 이하를 참조해 주세요.

조다 시간

를 작성하다

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은 새로운 Date and Time 라이브러리를 도입하여 날짜와 시간을 쉽게 처리할 수 있도록 하였습니다.표준 Java 버전 8 이상을 사용하려면 DateTimeFormatter를 사용합니다.타임존이 없기 때문에Stringjava.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 이전 버전에서는 SimpleDateFormat 및 java.util을 사용합니다.날짜

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

Joda Time을 사용하고 있다면 이 방법이 효과적이라고 생각합니다.

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

여기서의 일부를 얻었어요

내게는 아주 멍청하지만 효과적인 선택권이 있다.String fullDate = "11/15/2013 08:00:00"가 있는 경우

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

그것은 쉽고 빠르게 작동될 것이다.:)

이거 한 번 입어보세요

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은 Import처: (주의사항)

org.joda.time.format을 가져옵니다.DateTimeFormat. org.joda.time.format을 Import합니다.Date Time Formatter;

날짜 구문 분석:

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

이전:
DateTime.parse("201711201515"), DateTimeFormat.forPattern("yyyMMdHHM").toString("yyyyMMdd");

날짜/시간을 원하는 경우:

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

이것은 동작합니다.

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

또 다른 방법은 다음과 같습니다.

String date = dateAndTime.substring(0, dateAndTime.indexOf(" "));

확실하지는 않지만, 이 방법이 더 빠를 수 있습니다..split()방법.

간단한 방법:

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

언급URL : https://stackoverflow.com/questions/20331163/how-to-format-joda-time-datetime-to-only-mm-dd-yyyy

반응형