나는 Date 객체에 문자열을 변환하는 다음과 같은 방법을자바가 내 시간을 올바르게 변환하지 않는 이유는 무엇입니까?
public Date convertTime(String time) {
SimpleDateFormat parser = new SimpleDateFormat("d/M/y HH:mm:ss.S");
try {
return parser.parse(time);
}
catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
내가 다시
public String dateToTimeMillis(Date date) {
//StringBuffer formatted = new StringBuffer();
SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss.S");
try {
String formatted = parser.format(date);
return formatted;
}
catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
다음 테스트 코드
TraderLib lib = new TraderLib();
Date d1 = lib.convertTime("01/11/2011 10:41:09.045");
System.out.println(lib.dateToTimeMillis(d1));
반환 (10)을 변환하려면 다음 방법을 : 41 : 09.45
어떻게 0을 보존합니까?
new SimpleDateFormat("d/M/y HH:mm:ss.SSS");
가 3 개 자리를 유지하기 위해 :
@deltanovember은 어떤 계획이 질문에 대한 대답을 수락? 이 답변이 도움이 되었습니까? – retrodrone