我在我的數據庫中有一個意外時間變量,這是錯誤的,例如時間可能顯示爲55,這意味着00:55或0意味着00:00。另一個可能是200意味着02:00。它具有很好的價值,就像9:15我需要做的09:15。這是一個24小時制系統。現在我正在編寫一個小型私有方法,我需要比較時間(這是一個字符串)並將其更改爲適當的時間。我使用.equals來檢查字符串是否等於0,並將它賦值爲00:00。我需要幫助來檢查大於或小於Java中的字符串(例如,如果時間小於10,則在前面加上0)以及如何將200更改爲02:00。任何幫助讚賞。Java中字符串的比較
回答
我不會重視equals
方法。相反,我會使用Integer.parseInt
方法來檢查DB值是否是錯誤的(整數)格式,然後將該int值轉換爲新的「規範」表示。
這裏是我的意思是:
String fixupDbValue(final String dbVal)
{
final int intVal;
try
{
intVal = Integer.parseInt(dbVal);
}
catch (NumberFormatException ex)
{
// Not an int string, canonicalize string value
// Check that it's in dd:dd format
return fixupStringValue(dbVal);
}
return fixupIntValue(intVal);
}
我會用下面的代碼轉換的時間軍用時間。對於模式傳遞參數「yyyy-MM-dd HH:mm:ss」。
公共靜態日期formatDate(日期字符串,字符串dateFormatPattern)拋出ParseException的 {
if(date!=null && dateFormatPattern!=null && dateFormatPattern.trim().length()>0){ SimpleDateFormat df = new SimpleDateFormat(dateFormatPattern); return df.parse(df.format(date)); } return null; }
然後使用Date.compareTo()日期使用格式化返回的比較。
首先,我強烈建議改變你的數據庫列使用TIME
代替VARCHAR
和編寫SQL腳本相應修正值。這樣你就不再需要在Java端擺弄它了。這很難看。在時間上使用正確的數據類型提供了很多優點,您可以使用SQL中常用的基於整數的運算符/函數更容易地選擇/計算/操作它。
你可以使用java.sql.Time
(或java.util.Date
)對象,保存的時間信息(PreparedStatement
和ResultSet
提供方法來設置和獲取它從DB /),最後只使用java.text.SimpleDateFormat
人類可讀的時間串之間的轉換和時間/日期對象。
至於你實際問題,這樣的事情應該有所幫助:
String time = getItSomehow();
String paddedTime = String.format("%04d", Integer.valueOf(time)); // Pad with zeros to length of 4.
String formattedTime = String.format("%s:%s", paddedTime.substring(0, 2), paddedTime.substring(2, 4)); // Format in desired format.
我不確定'你的意思是寫'一個SQL腳本來修正相應的值'。我試圖把我的時間從STRING改爲TIME,但是它沒有成功......你能提出什麼建議嗎? – mona 2010-08-30 20:57:47
但首先,你需要變換String
到Date
。 java.text.SimpleDateFormat
可以提供幫助。
分析你的算法,我看到: - 如果字符串包括2位,則需要追加「00:」在開始 - 如果字符串組成的3個數字,你需要追加「0」開始並在第二位後加上「:」分開; - 如果字符串包含「:」,我們什麼也不做。
我看到這樣的事情:
public class Main {
public static void main(String[] args) throws ParseException {
System.out.println(getTime("02"));
System.out.println(getTime("200"));
System.out.println(getTime("9:15"));
System.out.println(getTime("9:"));
System.out.println(getTime("55"));
}
public static Date getTime(String stringTime) throws ParseException {
String correctTimeString;
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm");
//first of all, converting string to
int len = stringTime.length();
if (stringTime.contains(":")) {
int index = stringTime.indexOf(":");
if (index == stringTime.length() - 1) {
correctTimeString = stringTime + "0";
} else {
correctTimeString = stringTime;
}
} else if (len == 1) {
correctTimeString = "00:0" + stringTime;
} else if (len == 2) {
correctTimeString = "00:" + stringTime;
} else if (len == 3) {
correctTimeString = "0" + stringTime.charAt(0) + ":" + stringTime.substring(1, stringTime.length());
} else {
throw new RuntimeException("Unsupported date format");
}
//after convertation, parsing date
return dateFormat.parse(correctTimeString);
}
}
您可以更改時間格式或使用StringBuffer
或StringBuilder
優化串建築物或者您可以使用正則表達式解析日期。我想你有這個想法。如果我遺漏算法中的某些內容,您可以更正代碼。
爲了比較日期使用java.util.Date.compareTo(Date anotherDate)
返回: 值0如果參數日期等於這個日期;如果此日期在Date參數之前,則值小於0;如果此Date在Date參數之後,則值大於0。
這是在行動
import java.util.Arrays;
import java.util.List;
public class DBTime {
final private static List<String> timeStrings = Arrays.asList(
"55",
"0",
"200",
"915",
"00:55",
"00:00",
"02:00",
"09:15");
public static void main(String[] args) {
for (String timeString : timeStrings) {
System.out.println(convertDbString(timeString));
}
}
private static String convertDbString(final String dbString) {
if (dbString.contains(":")) { //Check if value is corrupted
return dbString;
} else {
String paddedTime = String.format("%04d", Integer.valueOf(dbString));
return String.format("%s:%s", paddedTime.substring(0, 2), paddedTime.substring(2, 4));
}
}
}
BalusCs代碼,這是這段代碼的輸出
run:
00:55
00:00
02:00
09:15
00:55
00:00
02:00
09:15
BUILD SUCCESSFUL (total time: 1 second)
爲什麼'timeStrings'包級別訪問(而不是私有)? – 2010-08-31 00:11:03
它是私人的。我沒有看到將它打包爲私有的原因(默認)。之前包裝是否私密? – 2010-08-31 14:25:47
如果您還沒有計劃在這個你應該做一次修復正常化所有的時間,然後添加驗證,以防止進入數據庫的時間。大多數人採取的方法是實際在數據庫中使用DateTime字段而不是字符串。 – Justin 2010-08-30 19:56:20