IT

자바 문자열을 Date 객체로 변환하는 방법

lottoking 2020. 6. 28. 18:00
반응형

자바 문자열을 Date 객체로 변환하는 방법 [중복]


이 질문에는 이미 답변이 있습니다.

나는 끈이있다

String startDate = "06/27/2007";

이제 Date 객체를 가져와야합니다. 내 DateObject는 startDate와 동일한 값이어야합니다.

나는 이렇게하고있다

DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);

그러나 출력 형식입니다

2007 년 1 월 27 일 00:06:00 PST.


기본적으로 날짜를 문자열 형식으로 날짜 개체로 효과적으로 변환했습니다. 이 시점에서 인쇄하면 표준 날짜 형식 출력이 표시됩니다. 그런 다음 형식을 지정하려면 지정된 형식 (이미 지정됨)의 날짜 개체로 다시 변환해야합니다.

String startDateString = "06/27/2007";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Date startDate;
try {
    startDate = df.parse(startDateString);
    String newDateString = df.format(startDate);
    System.out.println(newDateString);
} catch (ParseException e) {
    e.printStackTrace();
}

"mm"은 날짜의 "분"조각을 의미합니다. "개월"부분에는 "MM"을 사용하십시오.

따라서 코드를 다음과 같이 변경하십시오.

DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Date startDate = df.parse(startDateString);

편집 : DateFormat 객체에는 Date 객체가 아닌 날짜 형식 정의가 포함되어 있으며 형식과 관련없이 날짜 만 포함됩니다. 형식화에 대해 이야기 할 때 특정 형식으로 날짜의 문자열 표현을 작성하는 것에 대해 이야기합니다. 이 예를보십시오 :

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class DateTest {

        public static void main(String[] args) throws Exception {
            String startDateString = "06/27/2007";

            // This object can interpret strings representing dates in the format MM/dd/yyyy
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 

            // Convert from String to Date
            Date startDate = df.parse(startDateString);

            // Print the date, with the default formatting. 
            // Here, the important thing to note is that the parts of the date 
            // were correctly interpreted, such as day, month, year etc.
            System.out.println("Date, with the default formatting: " + startDate);

            // Once converted to a Date object, you can convert 
            // back to a String using any desired format.
            String startDateString1 = df.format(startDate);
            System.out.println("Date in format MM/dd/yyyy: " + startDateString1);

            // Converting to String again, using an alternative format
            DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy"); 
            String startDateString2 = df2.format(startDate);
            System.out.println("Date in format dd/MM/yyyy: " + startDateString2);
        }
    }

산출:

Date, with the default formatting: Wed Jun 27 00:00:00 BRT 2007
Date in format MM/dd/yyyy: 06/27/2007
Date in format dd/MM/yyyy: 27/06/2007

    try 
    {  
      String datestr="06/27/2007";
      DateFormat formatter; 
      Date date; 
      formatter = new SimpleDateFormat("MM/dd/yyyy");
      date = (Date)formatter.parse(datestr);  
    } 
    catch (Exception e)
    {}

월은 MM, 분은 mm입니다.


간결한 버전 :

String dateStr = "06/27/2007";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date)formatter.parse(dateStr);  

형식이 유효한 날짜인지 확인하려면 ParseException에 try / catch 블록을 추가하십시오.


var startDate = "06/27/2007";
startDate = new Date(startDate);

console.log(startDate);

참고 URL : https://stackoverflow.com/questions/6510724/how-to-convert-java-string-to-date-object

반응형