下面列出了java.text.SimpleDateFormat#getTimeInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public SimpleDateFormat getTimeFormat() {
SimpleDateFormat timeFormat = (SimpleDateFormat) SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT, getLocale());
timeFormat.setTimeZone(TimeZone.getTimeZone(getAdminTimezone()));
timeFormat.setLenient(false);
return timeFormat;
}
static DateFormat getTimeInstance(Context context, int style) {
if (context != null &&
(style == SimpleDateFormat.SHORT || style == SimpleDateFormat.MEDIUM)) {
Locale locale = Locale.getDefault();
boolean is24Hour = android.text.format.DateFormat.is24HourFormat(context);
String skeleton = (is24Hour ? "Hm" : "hm");
if (style == SimpleDateFormat.MEDIUM)
skeleton += "s";
String pattern = android.text.format.DateFormat.getBestDateTimePattern(locale, skeleton);
return new SimpleDateFormat(pattern, locale);
} else
return SimpleDateFormat.getTimeInstance(style);
}
public static String msTimeToShortTimeString(long msTime) {
Date date = new Date(msTime);
DateFormat df = SimpleDateFormat.getTimeInstance(DateFormat.SHORT);
return df.format(date);
}
private void initDateTimeFormat() {
simpleTimeFormat = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
simpleDateFormat = SimpleDateFormat.getDateInstance();
}
@Override
protected DateFormat initialValue() {
return SimpleDateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
}
@SuppressWarnings("all")
public Observable<String> getCalendarEvents() {
String details, title;
Cursor cursor;
ContentResolver contentResolver = application.getContentResolver();
final String[] colsToQuery = new String[]{
CalendarContract.EventsEntity.TITLE,
CalendarContract.EventsEntity.DTSTART,
CalendarContract.EventsEntity.DTEND,
CalendarContract.EventsEntity.EVENT_LOCATION};
Calendar now = Calendar.getInstance();
SimpleDateFormat startFormat = new SimpleDateFormat(Constants.SIMPLEDATEFORMAT_DDMMYY, Locale.getDefault());
String dateString = startFormat.format(now.getTime());
long start = now.getTimeInMillis();
SimpleDateFormat endFormat = new SimpleDateFormat(Constants.SIMPLEDATEFORMAT_HHMMSSDDMMYY, Locale.getDefault());
Calendar endOfDay = Calendar.getInstance();
Date endOfDayDate;
try {
endOfDayDate = endFormat.parse(Constants.END_OF_DAY_TIME + dateString);
endOfDay.setTime(endOfDayDate);
} catch (ParseException e) {
Log.e(GoogleCalendarService.class.getSimpleName(), e.toString());
throw new RuntimeException(String.format("ParseException occured: %s", e.getLocalizedMessage()));
}
cursor = contentResolver.query(CalendarContract.Events.CONTENT_URI, colsToQuery,
Constants.CALENDAR_QUERY_FIRST + start + Constants.CALENDAR_QUERY_SECOND + endOfDay.getTimeInMillis() + Constants.CALENDAR_QUERY_THIRD,
null, Constants.CALENDAR_QUERY_FOURTH);
if (cursor != null) {
if (cursor.getCount() > 0) {
StringBuilder stringBuilder = new StringBuilder();
while (cursor.moveToNext()) {
title = cursor.getString(0);
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(cursor.getLong(1));
Calendar endTime = Calendar.getInstance();
endTime.setTimeInMillis(cursor.getLong(2));
DateFormat formatter = SimpleDateFormat.getTimeInstance(DateFormat.SHORT);
details = formatter.format(startTime.getTime()) + " - " + formatter.format(endTime.getTime());
if (!TextUtils.isEmpty(cursor.getString(3))) {
details += " " + application.getString(R.string.at) + " " + cursor.getString(3);
}
stringBuilder.append(title + ", " + details);
if (!cursor.isLast()) {
stringBuilder.append(" | ");
}
}
cursor.close();
return Observable.just(stringBuilder.toString());
} else {
cursor.close();
return Observable.just(application.getString(R.string.no_events_today));
}
} else {
throw new RuntimeException(application.getString(R.string.no_events_error));
}
}
private static String getFormattedTime(Date date)
{
SimpleDateFormat timeFormat = (SimpleDateFormat) SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
return timeFormat.format(date);
}
public ChannelChatAdapter(Context context, IJumbleService service, List<IChatMessage> messages) {
super(context, 0, new ArrayList<>(messages));
mService = service;
mImageGetter = new MumbleImageGetter(context);
mDateFormat = SimpleDateFormat.getTimeInstance();
}
/**
* Gets formatted time.
*
* @return the formatted time
*/
public final String getFormattedTime() {
DateFormat timeInstance = SimpleDateFormat.getTimeInstance();
return timeInstance.format(Calendar.getInstance().getTime());
}
/**
* Gets formatted up time.
*
* @return the formatted up time
*/
public final String getFormattedUpTime() {
DateFormat timeInstance = SimpleDateFormat.getTimeInstance();
return timeInstance.format(SystemClock.uptimeMillis());
}