Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package org.apache.commons.beanutils2.sql.converters;

import java.sql.Time;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.beanutils2.converters.AbstractDateConverterTest;
Expand All @@ -29,6 +31,15 @@
*/
class SqlTimeConverterTest extends AbstractDateConverterTest<Time> {

/**
* Gets the separator that precedes the AM/PM field in the US SHORT time format. Java 20 and up (CLDR) use a narrow no-break space (U+202F) here,
* earlier versions use a regular space.
*/
private static String amPmSeparator() {
final String formatted = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US).format(new Date());
return formatted.contains("\u202f") ? "\u202f" : " ";
}

/**
* Gets the expected type
*
Expand Down Expand Up @@ -89,14 +100,14 @@ public void testLocale() {
final Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);

final String pattern = "h:mm a"; // SHORT style time format for US Locale
final String pattern = "h:mm" + amPmSeparator() + "a"; // SHORT style time format for US Locale

// Create & Configure the Converter
final SqlTimeConverter converter = makeConverter();
converter.setUseLocaleFormat(true);

// Valid String --> Type Conversion
final String testString = "3:06 pm";
final String testString = "3:06" + amPmSeparator() + "pm";
final Object expected = toType(testString, pattern, null);
validConversion(converter, expected, testString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
*/
class SqlTimestampConverterTest extends AbstractDateConverterTest<Timestamp> {

/**
* Gets the separator that precedes the AM/PM field in the US SHORT time format. Java 20 and up (CLDR) use a narrow no-break space (U+202F) here,
* earlier versions use a regular space.
*/
private static String amPmSeparator() {
final String formatted = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US).format(new Date());
return formatted.contains("\u202f") ? "\u202f" : " ";
}

/**
* Gets the expected type
*
Expand Down Expand Up @@ -108,12 +117,12 @@ public void testLocale() {
String pattern; // SHORT style Date & Time format for US Locale
String testString;
if (isUSFormatWithComma()) {
pattern = "M/d/yy, h:mm a";
testString = "3/21/06, 3:06 PM";
pattern = "M/d/yy, h:mm" + amPmSeparator() + "a";
testString = "3/21/06, 3:06" + amPmSeparator() + "PM";
} else {
// More regular pattern for Java 8 and earlier
pattern = "M/d/yy h:mm a";
testString = "3/21/06 3:06 PM";
pattern = "M/d/yy h:mm" + amPmSeparator() + "a";
testString = "3/21/06 3:06" + amPmSeparator() + "PM";
}

// Valid String --> Type Conversion
Expand Down
Loading