Java Code Examples for java.text.DateFormat.getTimeInstance()

The following are Jave code examples for showing how to use getTimeInstance() of the java.text.DateFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: jdk8u-jdk   File: TCKLocalizedPrinterParser.java   Source Code and License Vote up 5 votes
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_print(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
    DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
    Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
    String text = old.format(oldDate);

    DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
    String formatted = f.format(time);
    assertEquals(formatted, text);
}
 
Example 2
Project: openjdk-jdk10   File: TCKLocalizedPrinterParser.java   Source Code and License Vote up 5 votes
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_parse(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
    DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
    Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
    String text = old.format(oldDate);

    DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
    TemporalAccessor parsed = f.parse(text, pos);
    assertEquals(pos.getIndex(), text.length());
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(LocalTime.from(parsed), time);
}
 
Example 3
Project: jdk8u-jdk   File: TCKLocalizedPrinterParser.java   Source Code and License Vote up 5 votes
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_parse(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
    DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
    Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
    String text = old.format(oldDate);

    DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
    TemporalAccessor parsed = f.parse(text, pos);
    assertEquals(pos.getIndex(), text.length());
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(LocalTime.from(parsed), time);
}
 
Example 4
Project: ShoppingCartActivity   File: StringUtil.java   Source Code and License Vote up 5 votes
/**
 * 获取日期字符串中的时分秒
 * 
 * @param str
 * @param format
 * @return
 */
public static String getTime(String str) {
	try {
		Date date = getDate(str, "yyyy-MM-dd HH:mm:ss");
		DateFormat d3 = DateFormat.getTimeInstance();
		return d3.format(date);
	} catch (Exception ex) {
	}
	return null;
}
 
Example 5
Project: lams   File: FastDateFormat.java   Source Code and License Vote up 5 votes
/**
 * <p>Gets a time formatter instance using the specified style, time
 * zone and locale.</p>
 * 
 * @param style  time style: FULL, LONG, MEDIUM, or SHORT
 * @param timeZone  optional time zone, overrides time zone of
 *  formatted time
 * @param locale  optional locale, overrides system locale
 * @return a localized standard time formatter
 * @throws IllegalArgumentException if the Locale has no time
 *  pattern defined
 */
public static synchronized FastDateFormat getTimeInstance(int style, TimeZone timeZone, Locale locale) {
    Object key = new Integer(style);
    if (timeZone != null) {
        key = new Pair(key, timeZone);
    }
    if (locale != null) {
        key = new Pair(key, locale);
    }

    FastDateFormat format = (FastDateFormat) cTimeInstanceCache.get(key);
    if (format == null) {
        if (locale == null) {
            locale = Locale.getDefault();
        }

        try {
            SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getTimeInstance(style, locale);
            String pattern = formatter.toPattern();
            format = getInstance(pattern, timeZone, locale);
            cTimeInstanceCache.put(key, format);
        
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("No date pattern for locale: " + locale);
        }
    }
    return format;
}
 
Example 6
Project: lams   File: DateUtil.java   Source Code and License Vote up 5 votes
/**
    * Equivalent of <LAMS:Date value="value" type="date|time|both"/>. Use for processing a date to send to the client
    * via JSON. Locale comes from request.getLocale();
    *
    * @param value
    * @param type
    *            TYPE_BOTH (both data and time), TYPE_DATE or TYPE_TIME
    * @param locale
    * @return
    */
   public static String convertToStringForJSON(Date value, Integer style, Integer type, Locale locale) {

HttpSession ss = SessionManager.getSession();
UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER);
TimeZone tz = user.getTimeZone();

int dateStyle, timeStyle;
switch (style) {
    case DateFormat.SHORT:
	dateStyle = DateFormat.SHORT;
	timeStyle = DateFormat.SHORT;
	break;
    case DateFormat.FULL:
	dateStyle = DateFormat.LONG;
	timeStyle = DateFormat.FULL;
	break;
    default:
	dateStyle = DateFormat.LONG;
	timeStyle = DateFormat.MEDIUM;
}

DateFormat df = null;
switch (type) {
    case TYPE_DATE:
	df = DateFormat.getDateInstance(dateStyle, locale);
	break;
    case TYPE_TIME:
	df = DateFormat.getTimeInstance(timeStyle, locale);
	break;
    default:
	df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
}

if (tz != null) {
    df.setTimeZone(tz);
}

return df.format(value);
   }
 
Example 7
Project: whackpad   File: NativeDate.java   Source Code and License Vote up 5 votes
private static String toLocale_helper(double t, int methodId)
{
    DateFormat formatter;
    switch (methodId) {
      case Id_toLocaleString:
        if (localeDateTimeFormatter == null) {
            localeDateTimeFormatter
                = DateFormat.getDateTimeInstance(DateFormat.LONG,
                                                 DateFormat.LONG);
        }
        formatter = localeDateTimeFormatter;
        break;
      case Id_toLocaleTimeString:
        if (localeTimeFormatter == null) {
            localeTimeFormatter
                = DateFormat.getTimeInstance(DateFormat.LONG);
        }
        formatter = localeTimeFormatter;
        break;
      case Id_toLocaleDateString:
        if (localeDateFormatter == null) {
            localeDateFormatter
                = DateFormat.getDateInstance(DateFormat.LONG);
        }
        formatter = localeDateFormatter;
        break;
      default: throw new AssertionError(); // unreachable
    }

    synchronized (formatter) {
        return formatter.format(new Date((long) t));
    }
}
 
Example 8
Project: iosched-reader   File: UIUtils.java   Source Code and License Vote up 5 votes
/**
 * Format and return the given session time and {@link Rooms} values using
 * {@link Config#CONFERENCE_TIMEZONE}.
 */
public static String formatSessionSubtitle(long intervalStart, long intervalEnd, String roomName, StringBuilder recycle,
        Context context, boolean shortFormat) {

    // Determine if the session is in the past
    long currentTimeMillis = UIUtils.getCurrentTime(context);
    boolean conferenceEnded = currentTimeMillis > Config.CONFERENCE_END_MILLIS;
    boolean sessionEnded = currentTimeMillis > intervalEnd;
    if (sessionEnded && !conferenceEnded) {
        return context.getString(R.string.session_finished);
    }

    if (roomName == null) {
        roomName = context.getString(R.string.unknown_room);
    }

    if (shortFormat) {
        TimeZone timeZone = SettingsUtils.getDisplayTimeZone(context);
        Date intervalStartDate = new Date(intervalStart);
        SimpleDateFormat shortDateFormat = new SimpleDateFormat("MMM dd");
        DateFormat shortTimeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
        shortDateFormat.setTimeZone(timeZone);
        shortTimeFormat.setTimeZone(timeZone);
        return shortDateFormat.format(intervalStartDate) + " "
                + shortTimeFormat.format(intervalStartDate);
    } else {
        String timeInterval = formatIntervalTimeString(intervalStart, intervalEnd, recycle,
                context);
        return context.getString(R.string.session_subtitle, timeInterval, roomName);
    }
}
 
Example 9
Project: openjdk-jdk10   File: TCKLocalizedPrinterParser.java   Source Code and License Vote up 5 votes
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_print(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
    DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
    Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
    String text = old.format(oldDate);

    DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
    String formatted = f.format(time);
    assertEquals(formatted, text);
}
 
Example 10
Project: Neukoelln_SER316   File: Local.java   Source Code and License Vote up 4 votes
public static String getTimeString(Date d) {
	DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
	return dateFormat.format(d);
}
 
Example 11
Project: Wilmersdorf_SER316   File: Local.java   Source Code and License Vote up 4 votes
public static String getTimeString(Date d) {
    DateFormat dateFormat =
        DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
    return dateFormat.format(d);
}
 
Example 12
Project: Dahlem_SER316   File: Local.java   Source Code and License Vote up 4 votes
public static String getTimeString(Date d) {
    DateFormat dateFormat =
        DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
    return dateFormat.format(d);
}
 
Example 13
Project: holon-core   File: DefaultLocalizationContext.java   Source Code and License Vote up 4 votes
@Override
public DateFormat getDateFormat(TemporalType type, TemporalFormat dateFormat, TemporalFormat timeFormat) {
	TemporalType temporalType = (type != null) ? type : TemporalType.DATE;

	Localization lzn = getLocalization();
	if (lzn == null) {
		throw new LocalizationException("Context is not localized");
	}

	TemporalFormat df = (dateFormat != null) ? dateFormat : TemporalFormat.DEFAULT;
	TemporalFormat tf = (timeFormat != null) ? timeFormat : TemporalFormat.DEFAULT;

	if (df == TemporalFormat.DEFAULT) {
		df = lzn.getDefaultDateTemporalFormat().orElse(getDefaultDateFormatStyle().orElse(TemporalFormat.SHORT));
	}
	if (tf == TemporalFormat.DEFAULT) {
		tf = lzn.getDefaultTimeTemporalFormat().orElse(getDefaultTimeFormatStyle().orElse(TemporalFormat.SHORT));
	}

	DateFormat formatter;

	switch (temporalType) {
	case TIME: {
		if (isUseDateTimeFormatsCache() && timeFormatCache.containsKey(tf)) {
			formatter = timeFormatCache.get(tf);
		} else {
			formatter = DateFormat.getTimeInstance(tf.getTimeStyle(), checkLocalized());
			if (isUseDateTimeFormatsCache()) {
				timeFormatCache.put(tf, formatter);
			}
		}
	}
		break;
	case DATE_TIME: {
		DateTimeFormat dtf = new DateTimeFormat(df, tf);
		if (isUseDateTimeFormatsCache() && dateTimeFormatCache.containsKey(dtf)) {
			formatter = dateTimeFormatCache.get(dtf);
		} else {
			formatter = DateFormat.getDateTimeInstance(df.getDateStyle(), tf.getTimeStyle(), checkLocalized());
			if (isUseDateTimeFormatsCache()) {
				dateTimeFormatCache.put(dtf, formatter);
			}
		}
	}
		break;
	case DATE:
	default: {
		if (isUseDateTimeFormatsCache() && dateFormatCache.containsKey(df)) {
			formatter = dateFormatCache.get(df);
		} else {
			formatter = DateFormat.getDateInstance(df.getDateStyle(), checkLocalized());
			if (isUseDateTimeFormatsCache()) {
				dateFormatCache.put(df, formatter);
			}
		}
	}
		break;
	}

	return formatter;
}
 
Example 14
Project: TYT   File: SublimePickerFragment.java   Source Code and License Vote up 4 votes
public SublimePickerFragment() {
    // Initialize formatters
    mDateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
    mTimeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
    mTimeFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
}
 
Example 15
Project: SER316-Dresden   File: Local.java   Source Code and License Vote up 4 votes
public static String getTimeString(Date d) {
    DateFormat dateFormat =
        DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
    return dateFormat.format(d);
}
 
Example 16
Project: diary   File: Diary.java   Source Code and License Vote up 4 votes
private String eventCheck(String text)
{
    Pattern pattern = Pattern.compile(EVENT_PATTERN, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(text);

    DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);

    // Find matches
    while (matcher.find())
    {
        // Parse time
        Date date = null;
        try
        {
            date = dateFormat.parse(matcher.group(1));
        }

        // Ignore errors
        catch (Exception e)
        {
            continue;
        }

        Calendar time = Calendar.getInstance();
        time.setTime(date);

        Calendar startTime =
            new GregorianCalendar(currEntry.get(Calendar.YEAR),
                                  currEntry.get(Calendar.MONTH),
                                  currEntry.get(Calendar.DATE),
                                  time.get(Calendar.HOUR_OF_DAY),
                                  time.get(Calendar.MINUTE));
        Calendar endTime =
            new GregorianCalendar(currEntry.get(Calendar.YEAR),
                                  currEntry.get(Calendar.MONTH),
                                  currEntry.get(Calendar.DATE),
                                  time.get(Calendar.HOUR_OF_DAY),
                                  time.get(Calendar.MINUTE));
        // Add an hour
        endTime.add(Calendar.HOUR, 1);

        String title = matcher.group(2);

        QueryHandler.insertEvent(this, startTime.getTimeInMillis(),
                                 endTime.getTimeInMillis(), title);
    }

    return matcher.replaceAll(EVENT_TEMPLATE);
}
 
Example 17
Project: SER316-Aachen   File: Local.java   Source Code and License Vote up 4 votes
public static String getTimeString(Date d) {
    DateFormat dateFormat =
        DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
    return dateFormat.format(d);
}
 
Example 18
Project: SER316-Munich   File: Local.java   Source Code and License Vote up 4 votes
public static String getTimeString(Date d) {
    DateFormat dateFormat =
        DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
    return dateFormat.format(d);
}
 
Example 19
Project: SER316-Ingolstadt   File: Local.java   Source Code and License Vote up 4 votes
public static String getTimeString(Date d) {
    DateFormat dateFormat =
        DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
    return dateFormat.format(d);
}
 
Example 20
Project: SOS-The-Healthcare-Companion   File: FormatDateTime.java   Source Code and License Vote up 3 votes
public String getTime(Calendar cal) {
    DateFormat finalTimeFormat;

    finalTimeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);


    String finalTime = finalTimeFormat.format(cal.getTime());
    return finalTime + "";
}