웹 사이트를 등록하면 종종 이름, 연령, 생년월일 등과 같은 개인 정보를 작성해야합니다. 페이지의 생년월일 값이 배경으로 전달되면 문자열이며 데이터베이스에 입금하면 날짜 유형이 필요합니다. 반대로 페이지에 표시되면 데이터베이스에서 생년월일을 얻어야합니다. 현재이 유형은 날짜 유형이며 날짜 유형을 문자열로 변환하여 페이지에 표시해야합니다. Java API는 날짜와 문자열을 서로에게 전송하는 클래스 데이트 foramt를 제공합니다. DateForAmt는 추상 클래스이므로 일반적으로 하위 클래스 SimpleDateFormat로 사용됩니다. SimpledateFormat에는 4 개의 생성자가 있으며 두 번째 생성자는 가장 자주 사용됩니다.
생성자의 패턴은 시간 패턴입니다. 특정 패턴은 무엇입니까? API는 다음과 같이 설명됩니다
1. 문자열 날짜 (형식)
package com.test.dateformat; import java.text.simpledateformat; import java.util.date; import org.junit.test; public class date2string {@test public void test () {date date = new 날짜 (); simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd"); System.out.println (sdf.format (date)); sdf = 새로운 simpledateformat ( "yyyy-mm-dd hh : mm : ss"); System.out.println (sdf.format (date)); sdf = 새로운 simpledateformat ( "yyyyy 년 mm 달 dd 날짜 hh : mm : ss"); System.out.println (sdf.format (date)); }} 2016-10-24
2016-10-24 21:59:06
2016 년 10 월 24 일 21:59:06
2. 현재까지 문자열 (구문 분석)
package com.test.dateformat; import java.text.parseexception; import java.text.simpledateformat; import org.junit.test; public class string2date {@test public void test () parseexception {string string = "2016-10-24 21:59:06"; simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); System.out.println (sdf.parse (String)); }} 월 24 일 월 24 일 21:59:06 CST 2016
현재까지 문자열을 사용하면 주어진 패턴이 주어진 문자열 형식과 일치해야합니다. 그렇지 않으면 java.text.parseexception이 던져집니다. 예를 들어 다음이 잘못되었습니다. 문자열은 시간, 분, 두 번째 시간을주지 않습니다. 물론 SimpledateFormat은 얇은 공기에서 시간, 분 및 두 번째 값을 구문 분석 할 수 없습니다.
package com.test.dateformat; import java.text.parseexception; import java.text.simpledateformat; import org.junit.test; public class string2date {@test public void test () parseexception {string string = "2016-10-24"; simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); System.out.println (sdf.parse (String)); }}그러나 주어진 패턴은 문자열보다 적습니다
package com.test.dateformat; import java.text.parseexception; import java.text.simpledateformat; import org.junit.test; public class string2date {@test public void test () parseexception {string string = "2016-10-24 21:59:06"; simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd"); System.out.println (sdf.parse (String)); }} Mon 10 월 24 일 00:00:00 CST 2016
시간, 분 및 두 번째는 0이고 구문 분석되지 않음을 알 수 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.