📄 conversions.java
字号:
*/
public static String booleanToString(boolean bool) {
if (LOG.isDebugEnabled()) {
LOG.debug("booleanToString: Converting '" + bool + "' to a String");
}
String returnVal = bool
? ConstantValues.STRING_TRUE : ConstantValues.STRING_FALSE;
if (LOG.isDebugEnabled()) {
LOG.debug("booleanToString: Result of the conversion is '"
+ returnVal + "'");
}
return returnVal;
}
//////// Convert a java.lang.String into a java.lang.Boolean
/**
* Convert a <code>java.lang.String</code> into a
* <code>java.lang.Booleam</code>. If the string passed in is
* <code>null</code>, then <code>null</code> will be returned. Otherwise
* this method will attempt to convert the string to a boolean object,
* and will throw a <code>ConversionException</code> if it is unable to
* do so.
*
* @param sBool The string to be converted to a boolean (object)
* @return The boolean (object) that the string was converted into
* @throws ConversionException If the string cannot be converted
* @see ConversionException
*/
public static Boolean stringToBoolean(String sBool)
throws ConversionException {
if (LOG.isDebugEnabled()) {
LOG.debug("stringToBoolean: Converting '" + sBool
+ "' to a Boolean");
}
if (sBool == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("stringToBoolean: Result of the conversion is null");
}
return null;
}
try {
Boolean bool = null;
if (sBool.equalsIgnoreCase(ConstantValues.STRING_TRUE)
|| sBool.equals("1")
|| sBool.equalsIgnoreCase("t")
|| sBool.equalsIgnoreCase(ConstantValues.STRING_YES)) {
bool = new Boolean(true);
} else if (sBool.equalsIgnoreCase(ConstantValues.STRING_FALSE)
|| sBool.equals("0")
|| sBool.equalsIgnoreCase("f")
|| sBool.equalsIgnoreCase(ConstantValues.STRING_NO)) {
bool = new Boolean(false);
} else {
// Will be discarded when a conversion exception is thrown later
throw new Exception();
}
if (LOG.isDebugEnabled()) {
LOG.debug("stringToBoolean: Result of the conversion is '"
+ bool + "'");
}
return bool;
} catch (Exception ex) {
if (LOG.isDebugEnabled()) {
LOG.debug("stringToBoolean: Unable to convert '" + sBool
+ "' to a Boolean", ex);
}
throw new ConversionException(sBool, String.class, Boolean.class);
}
}
//////// Convert a time (java.lang.Long) to a java.lang.String
/**
* Convert a time (long) value to a string. This will convert the long
* value (representing milliseconds) into a properly formatted strings
* showing the time in a variation of the HH:mm:ss.SSS format. If no
* hours or milliseconds are present, those portions of the format are
* ommitted.
*
* @param time The time (in milliseconds)
* @return A string representation of the time
* @throws ConversionException If the time is negative, or too large to
* convert
*/
public static String timeToString(Long time) throws ConversionException {
if (LOG.isDebugEnabled()) {
LOG.debug("timeToString: Converting the following time " + time);
}
if (time == null) {
return null;
}
// Start with a calendar for 00:00:00.000
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
// Increment the calendar by the appropriate milliseconds
// Must be less than 86400000
long timeValue = time.longValue();
if (timeValue < 0) {
LOG.error("Time value is negative");
throw new ConversionException(time, Long.class, String.class);
} else if (timeValue > MAX_TIME) {
LOG.error("Time value is too large");
throw new ConversionException(time, Long.class, String.class);
} else {
cal.add(Calendar.MILLISECOND, (int) time.longValue());
}
// Now pick off the time components
int hours = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
int millis = cal.get(Calendar.MILLISECOND);
if (LOG.isDebugEnabled()) {
LOG.debug("timeToString: Hours for time " + hours);
LOG.debug("timeToString: Minutes for time " + minutes);
LOG.debug("timeToString: Seconds for time " + seconds);
LOG.debug("timeToString: Milliseconds for time " + millis);
}
//Build up a string representing the time, padding with 0s when needed
StringBuffer buff = new StringBuffer();
if (hours > 0) {
if (hours < TEN) {
buff.append(0);
}
buff.append(hours);
buff.append(COLON);
}
if (minutes < TEN) {
buff.append(0);
}
buff.append(minutes);
buff.append(COLON);
if (seconds < TEN) {
buff.append(0);
}
buff.append(seconds);
if (millis > 0) {
buff.append(PERIOD);
if (millis < ONE_HUNDRED) {
buff.append(0);
}
if (millis < TEN) {
buff.append(0);
}
buff.append(millis);
}
if (LOG.isDebugEnabled()) {
LOG.debug("timeToString: Converted the time to " + buff);
}
return buff.toString();
}
/**
* Convert a string value into a time value (i.e., a long representing the
* number of milliseconds). This method will accept times that are
* formatted as a variation of the HH:mm:ss.SSS pattern (possibly without
* hours, seconds, or milliseconds), and return the number of milliseconds
* that the string represents.
*
* @param string The string representation of the time
* @return The number of milliseconds in the time
* @throws ConversionException If the string cannot be converted into a time
*/
public static Long stringToTime(String string) throws ConversionException {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("stringToTime: Converting this string " + string);
}
return convertStringToLong(string);
} catch (Exception ex) {
if (LOG.isDebugEnabled()) {
LOG.debug("Unable to convert '" + string + "' to a time", ex);
}
throw new ConversionException(string, String.class, Long.class);
}
}
//////// Internal Utility Methods
/**
* Get a new <code>java.text.DateFormat</code> instance for use in
* formatting/parsing dates. The format object will have the specified
* style and locale.
*
* @param style The style that the instance should use. Should be one of
* the style constants defined in
* <code>java.text.DateFormat</code>
* @param locale The locale for the instance
* @return The <code>java.text.DateFormat</code> instance
*/
private static DateFormat getDateInstance(int style, Locale locale) {
return SimpleDateFormat.getDateInstance(style, locale);
}
/**
* Perform the actual work of converting a string into a long (time). This
* method will parse the string parameter and resturn the long
* representation. If, for any reason, the string cannot be converted, an
* exception will be thrown.
*
* @param string The string to convert
* @return The long representation of the string
* @throws Exception If the string cannot be converted
*/
private static Long convertStringToLong(String string) throws Exception {
if (string == null) {
return null;
}
if (string.startsWith(COLON) || string.endsWith(COLON)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Unrecognized time format");
}
throw new Exception("Unrecognized time format");
}
String sHours = null;
String sMinutes = null;
String sSeconds = null;
String sMillis = null;
StringTokenizer tokenizer = new StringTokenizer(string, COLON, false);
int numTokens = tokenizer.countTokens();
int format = tokenizer.countTokens();
boolean hasMillis = string.indexOf(PERIOD) != -1;
if (LOG.isDebugEnabled()) {
LOG.debug("convertStringToLong: Format is " + format);
LOG.debug("convertStringToLong: Has milliseconds is " + hasMillis);
}
if (format == FORMAT_H_M_S) {
sHours = tokenizer.nextToken();
sMinutes = tokenizer.nextToken();
sSeconds = tokenizer.nextToken();
} else if (format == FORMAT_M_S) {
sMinutes = tokenizer.nextToken();
sSeconds = tokenizer.nextToken();
} else if (format == FORMAT_M) {
sMinutes = tokenizer.nextToken();
} else {
throw new Exception("Unrecognized time format");
}
if (LOG.isDebugEnabled()) {
LOG.debug("convertStringToLong: Hours (string) is " + sHours);
LOG.debug("convertStringToLong: Minutes (string) is " + sMinutes);
LOG.debug("convertStringToLong: Seconds (string) is " + sSeconds);
LOG.debug("convertStringToLong: Millis (string) is " + sMillis);
}
if (hasMillis) {
String temp = sSeconds;
sSeconds = temp.substring(0, temp.indexOf(PERIOD));
sMillis = temp.substring(temp.indexOf(PERIOD) + 1, temp.length());
if (sMillis.length() == 1) {
sMillis += String.valueOf(0);
}
if (sMillis.length() == 2) {
sMillis += String.valueOf(0);
}
}
int hours = sHours == null ? 0 : Integer.parseInt(sHours);
int minutes = sMinutes == null ? 0 : Integer.parseInt(sMinutes);
int seconds = sSeconds == null ? 0 : Integer.parseInt(sSeconds);
int millis = sMillis == null ? 0 : Integer.parseInt(sMillis);
if (LOG.isDebugEnabled()) {
LOG.debug("convertStringToLong: Hours (int) is " + hours);
LOG.debug("convertStringToLong: Minutes (int) is " + minutes);
LOG.debug("convertStringToLong: Seconds (int) is " + seconds);
LOG.debug("convertStringToLong: Millis (int) is " + millis);
}
if (hours < 0 || minutes < 0 || seconds < 0 || millis < 0) {
throw new Exception("One or more time components is negative");
}
long timeInMillis = Utilities.getMilliseconds(hours, minutes,
seconds, millis);
if (timeInMillis > MAX_TIME) {
throw new Exception("Time value is too large");
}
if (LOG.isDebugEnabled()) {
LOG.debug("convertStringToLong: Converted into this time "
+ timeInMillis);
}
return new Long(timeInMillis);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -