本文整理汇总了Java中com.ibm.icu.text.DateFormat类的典型用法代码示例。如果您正苦于以下问题:Java DateFormat类的具体用法?Java DateFormat怎么用?Java DateFormat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DateFormat类属于com.ibm.icu.text包,在下文中一共展示了DateFormat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: guessDateFormat
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* This function can be overridden by subclasses to use different heuristics.
* <b>It MUST return a 'safe' value,
* one whose modification will not affect this object.</b>
*
* @param dateStyle
* @param timeStyle
* @draft ICU 3.6
* @provisional This API might change or be removed in a future release.
*/
protected DateFormat guessDateFormat(int dateStyle, int timeStyle) {
DateFormat result;
ULocale dfLocale = getAvailableLocale(TYPE_DATEFORMAT);
if (dfLocale == null) {
dfLocale = ULocale.ROOT;
}
if (timeStyle == DF_NONE) {
result = DateFormat.getDateInstance(getCalendar(), dateStyle, dfLocale);
} else if (dateStyle == DF_NONE) {
result = DateFormat.getTimeInstance(getCalendar(), timeStyle, dfLocale);
} else {
result = DateFormat.getDateTimeInstance(getCalendar(), dateStyle, timeStyle, dfLocale);
}
return result;
}
示例2: makeDateTimeCheck
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* Checks the given formatted date by means of the {@link DateFormat} instances from
* {@code getFormatters} method. If one of the {@link DateFormat} instances parsed the given
* datetime correctly, then check considered as successful.
*
* @param target instance of the {@link Placeholder}
* @param locale to use during the sanity check
* @param expected is a numeric representation of expected date
* @throws AssertionError
*/
private void makeDateTimeCheck(Placeholder target, ULocale locale, @Nullable Number expected)
throws AssertionError {
// This variable is needed to distinguish two error cases: none of formats matches, or there are
// matching formats, but expected value does not match.
boolean matching = false;
for (DateFormat formatter : dateFormatsProducer.get(target, locale)) {
Date result = checkFormat(target, formatter);
if (result != null) {
matching = true;
if (checkExpectedValue(result, expected)) {
return;
}
}
}
String errorMessage;
// Compose error message for assertion based on failure case.
if (matching) {
errorMessage = String.format("The expected value '%d' does not match parsed data.", expected);
} else {
errorMessage = String.format("'%s' does not satisfy any format of locale %s.",
target.getActualContent(), locale.toString());
}
throw new AssertionError(errorMessage);
}
示例3: makePatternBasedCheck
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* Checks the date time format based on the user provided pattern.
*
* @param target instance of the {@link Placeholder}
* @param pattern is a string representation of the date time pattern
* @param locale to use during the sanity check
* @param expected is a numeric representation of expected date
* @throws AssertionError
*/
private void makePatternBasedCheck(Placeholder target, String pattern,
ULocale locale, @Nullable Number expected) throws AssertionError {
DateFormat formatter = new SimpleDateFormat(pattern, locale);
formatter.setLenient(target.isLenient());
Date result = checkFormat(target, formatter);
if (result != null) {
if (!checkExpectedValue(result, expected)) {
throw new AssertionError(String.format(
"Expected value '%s' does not match parsed value '%s' for pattern '%s'.",
expected, result.getTime()));
}
} else {
throw new AssertionError(String.format("'%s' does not satisfy specified pattern '%s'.",
target.getActualContent(), pattern));
}
}
示例4: testCheck_ValueBestMatch
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
@Test
public void testCheck_ValueBestMatch() throws ParseException {
// Use special checker with multiple data formats to verity best match strategy
final TimeDateChecker multiChecker =
new TimeDateChecker(new TimeDateChecker.DateFormatProducer() {
@Override
public ImmutableList<DateFormat> get(Placeholder target, ULocale locale) {
return ImmutableList.of(mockFormat, mockFormat, mockFormat);
}
});
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d MMMM yyyy, HH:mm:ss");
final Date date = TestUtils.generateRandomDate();
final String formattedDate = simpleDateFormat.format(date);
when(mockFormat.parse(formattedDate))
.thenReturn(mockDate, null, date);
final Placeholder testToken = Placeholder
.builder("datetime", formattedDate)
.putExpectedValueParam(String.format("%d", date.getTime())).build();
multiChecker.check(testToken, ULocale.GERMAN, null);
}
示例5: formatURLValue
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* Formats value in URL parameters so that it can be read in server
*
* @param value
* @return
*/
private String formatURLValue( Object value )
{
if ( value instanceof Calendar )
{
// Bugzilla#215442 fix a parse issue to date
// Bugzilla#245920 Just using default locale to format date string
// to avoid passing locale-specific value for drill-through.
return DateFormat.getDateInstance( DateFormat.LONG ).format( value );
}
if ( value instanceof Number )
{
// Do not output decimal for integer value, and also avoid double
// precision error for double value
Number num = (Number) value;
if ( ChartUtil.mathEqual( num.doubleValue( ), num.intValue( ) ) )
{
return String.valueOf( num.intValue( ) );
}
return String.valueOf( ValueFormatter.normalizeDouble( num.doubleValue( ) ) );
}
return ChartUtil.stringValue( value );
}
示例6: getJavaType
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
*
* @return
* @throws UndefinedValueException
*/
private final int getJavaType( ) throws ChartException
{
switch ( getType( ).getValue( ) )
{
case DateFormatType.SHORT :
return DateFormat.SHORT;
case DateFormatType.MEDIUM :
return DateFormat.MEDIUM;
case DateFormatType.LONG :
return DateFormat.LONG;
case DateFormatType.FULL :
return DateFormat.FULL;
}
return 0;
}
示例7: toDateFromString
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
public static java.sql.Date toDateFromString( String s ) throws OdaException
{
if ( s == null )
{
return null;
}
try
{
Date date = DateFormat.getInstance( ).parse( s );
return new java.sql.Date( date.getTime( ) );
}
catch ( ParseException e )
{
throw new OdaException( e );
}
}
示例8: toTimeFromString
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
public static java.sql.Time toTimeFromString( String s ) throws OdaException
{
if ( s == null )
{
return null;
}
try
{
Date date = DateFormat.getInstance( ).parse( s );
return new Time( date.getTime( ) );
}
catch ( ParseException e )
{
throw new OdaException( e );
}
}
示例9: toTimestampFromString
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
public static java.sql.Timestamp toTimestampFromString( String s ) throws OdaException
{
if ( s == null )
{
return null;
}
try
{
Date date = DateFormat.getInstance( ).parse( s );
return new Timestamp( date.getTime( ) );
}
catch ( ParseException e )
{
throw new OdaException( e );
}
}
示例10: toDateWithCheck
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* Convert string to date with check.
* JDK may do incorrect converse, for example:
* 2005/1/1 Local.US, format pattern is MM/dd/YY.
* Above conversion can be done without error, but obviously
* the result is not right. This method will do such a simple check,
* in DateFormat.SHORT case instead of all cases.
* Year is not lower than 0.
* Month is from 1 to 12.
* Day is from 1 to 31.
* @param source
* @param locale
* @return Date
* @throws BirtException
*/
public static Date toDateWithCheck( String source, ULocale locale )
throws BirtException
{
DateFormat dateFormat = DateFormatFactory.getDateInstance( DateFormat.SHORT,
locale );
Date resultDate = null;
try
{
resultDate = dateFormat.parse( source );
}
catch ( ParseException e )
{
return toDate( source, locale );
}
// check whether conversion is correct
if ( DateUtil.checkValid( dateFormat, source ) == false )
{
throw new CoreException(
ResourceConstants.CONVERT_FAILS,
new Object[]{
source.toString( ), "Date"
});
}
return resultDate;
}
示例11: checkValid
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* Check whether dateStr can be correctly converted to Date in
* format of DateFormat.SHORT. Here one point must be noticed that
* dateStr should firstly be able to be converted to Date.
*
* @param df
* @param dateStr
* @return checkinfo
*/
public static boolean checkValid( DateFormat df, String dateStr )
{
assert df != null;
assert dateStr != null;
boolean isValid = true;
if ( df instanceof SimpleDateFormat )
{
String[] dateResult = splitDateStr( dateStr );
SimpleDateFormat sdf = (SimpleDateFormat) df;
String pattern = sdf.toPattern( );
String[] patternResult = splitDateStr( pattern );
if ( dateResult != null && patternResult != null )
{
isValid = isMatch( dateResult, patternResult );
}
}
return isValid;
}
示例12: CreateDateTimeParts
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* CreateDateTimeParts(dateTimeFormat, x)
*
* @param dateTimeFormat
* the date format object
* @param date
* the date object
* @return the formatted date-time object
*/
private static List<Map.Entry<String, String>> CreateDateTimeParts(DateTimeFormatObject dateTimeFormat, Date date) {
ArrayList<Map.Entry<String, String>> parts = new ArrayList<>();
DateFormat dateFormat = dateTimeFormat.getDateFormat();
AttributedCharacterIterator iterator = dateFormat.formatToCharacterIterator(date);
StringBuilder sb = new StringBuilder();
for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
sb.append(ch);
if (iterator.getIndex() + 1 == iterator.getRunLimit()) {
Iterator<Attribute> keyIterator = iterator.getAttributes().keySet().iterator();
String key;
if (keyIterator.hasNext()) {
key = fieldToString((DateFormat.Field) keyIterator.next());
} else {
key = "literal";
}
String value = sb.toString();
sb.setLength(0);
parts.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
}
}
return parts;
}
示例13: createDateFormat
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
private DateFormat createDateFormat() {
ULocale locale = ULocale.forLanguageTag(this.locale);
// calendar and numberingSystem are already handled in language-tag
// assert locale.getKeywordValue("calendar").equals(calendar);
// assert locale.getKeywordValue("numbers").equals(numberingSystem);
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern.get(), locale);
if (timeZone != null) {
dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
}
Calendar calendar = dateFormat.getCalendar();
if (calendar instanceof GregorianCalendar) {
// format uses a proleptic Gregorian calendar with no year 0
GregorianCalendar gregorian = (GregorianCalendar) calendar;
gregorian.setGregorianChange(new Date(Long.MIN_VALUE));
}
return dateFormat;
}
示例14: processHeader
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* Process the header of the file.
* @param config
*
* @param contents Contents of file
* @param content
*/
private void processHeader(Configuration config, List<String> contents, final Map<String, Object> content) {
for (String line : contents) {
if (line.equals(HEADER_SEPARATOR)) {
break;
}
if (line.isEmpty()) {
continue;
}
String[] parts = line.split("=",2);
if (parts.length != 2) {
continue;
}
String key = parts[0].trim();
String value = parts[1].trim();
if (key.equalsIgnoreCase(Crawler.Attributes.DATE)) {
DateFormat df = new SimpleDateFormat(config.getString(Keys.DATE_FORMAT));
Date date = null;
try {
date = df.parse(value);
content.put(key, date);
} catch (ParseException e) {
e.printStackTrace();
}
} else if (key.equalsIgnoreCase(Crawler.Attributes.TAGS)) {
content.put(key, getTags(value));
} else if (isJson(value)) {
content.put(key, JSONValue.parse(value));
} else {
content.put(key, value);
}
}
}
示例15: handleGetDateFormat
import com.ibm.icu.text.DateFormat; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* @stable ICU 4.2
*/
protected DateFormat handleGetDateFormat(String pattern, String override, ULocale locale) {
// Note: ICU 50 or later versions no longer use ChineseDateFormat.
// The super class's handleGetDateFormat will create an instance of
// SimpleDateFormat which supports Chinese calendar date formatting
// since ICU 49.
//return new ChineseDateFormat(pattern, override, locale);
return super.handleGetDateFormat(pattern, override, locale);
}