下面列出了android.text.format.Time#set ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the timezone to display in the event info, if the local timezone is different
* from the event timezone. Otherwise returns null.
*/
public static String getDisplayedTimezone(long startMillis, String localTimezone,
String eventTimezone) {
String tzDisplay = null;
if (!TextUtils.equals(localTimezone, eventTimezone)) {
// Figure out if this is in DST
TimeZone tz = TimeZone.getTimeZone(localTimezone);
if (tz == null || tz.getID().equals("GMT")) {
tzDisplay = localTimezone;
} else {
Time startTime = new Time(localTimezone);
startTime.set(startMillis);
tzDisplay = tz.getDisplayName(startTime.isDst != 0, TimeZone.SHORT);
}
}
return tzDisplay;
}
public static String formatTime(Context context, long when) {
// TODO: DateUtils should make this easier
Time then = new Time();
then.set(when);
Time now = new Time();
now.setToNow();
int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT | DateUtils.FORMAT_ABBREV_ALL;
if (then.year != now.year) {
flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
} else if (then.yearDay != now.yearDay) {
flags |= DateUtils.FORMAT_SHOW_DATE;
} else {
flags |= DateUtils.FORMAT_SHOW_TIME;
}
return DateUtils.formatDateTime(context, when, flags);
}
@Override
public Time apply(ContentSet currentValues, Time oldValue, Time newValue)
{
Time reference = mReferenceAdapter.get(currentValues);
if (reference != null && newValue != null)
{
if (oldValue != null && !newValue.before(reference))
{
// try to shift the reference value
long diff = newValue.toMillis(false) - oldValue.toMillis(false);
if (diff > 0)
{
boolean isAllDay = reference.allDay;
reference.set(reference.toMillis(false) + diff);
// ensure the event is still allday if is was allday before.
if (isAllDay)
{
reference.set(reference.monthDay, reference.month, reference.year);
}
mReferenceAdapter.set(currentValues, reference);
}
}
if (!newValue.before(reference))
{
// constraint is still violated, so set reference to its default value
reference.set(mDefault.getCustomDefault(currentValues, newValue));
mReferenceAdapter.set(currentValues, reference);
}
}
return newValue;
}
static CharSequence fmtHceDeactivatedMessage(long stamp) {
Time time = new Time();
time.set(stamp);
return fmt(
ThisApplication.getStringResource(R.string.status_deactivated),
fmtTime(time), fmtDate(time));
}
private Time normalizeTime(int year, int month, int monthDay,
int hour, int minute, int second) {
Time result = new Time();
if (year == 0 && month == 0 && monthDay == 0 && hour == 0 &&
minute == 0 && second == 0) {
Calendar cal = Calendar.getInstance();
result.set(cal.get(Calendar.SECOND), cal.get(Calendar.MINUTE),
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.DATE),
cal.get(Calendar.MONTH), cal.get(Calendar.YEAR));
} else {
result.set(second, minute, hour, monthDay, month, year);
}
return result;
}
public static long parse(String timeString)
throws IllegalArgumentException {
int date = 1;
int month = Calendar.JANUARY;
int year = 1970;
TimeOfDay timeOfDay;
Matcher rfcMatcher = HTTP_DATE_RFC_PATTERN.matcher(timeString);
if (rfcMatcher.find()) {
date = getDate(rfcMatcher.group(1));
month = getMonth(rfcMatcher.group(2));
year = getYear(rfcMatcher.group(3));
timeOfDay = getTime(rfcMatcher.group(4));
} else {
Matcher ansicMatcher = HTTP_DATE_ANSIC_PATTERN.matcher(timeString);
if (ansicMatcher.find()) {
month = getMonth(ansicMatcher.group(1));
date = getDate(ansicMatcher.group(2));
timeOfDay = getTime(ansicMatcher.group(3));
year = getYear(ansicMatcher.group(4));
} else {
throw new IllegalArgumentException();
}
}
// FIXME: Y2038 BUG!
if (year >= 2038) {
year = 2038;
month = Calendar.JANUARY;
date = 1;
}
Time time = new Time(Time.TIMEZONE_UTC);
time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date,
month, year);
return time.toMillis(false /* use isDst */);
}
@Override
public Time apply(ContentSet currentValues, Time oldValue, Time newValue)
{
Time reference = mReferenceAdapter.get(currentValues);
if (reference != null && newValue != null && !newValue.after(reference))
{
newValue.set(mDefault.getCustomDefault(currentValues, reference));
}
return newValue;
}
private JSONObject getStringtoDate(JSONArray options)throws GlobalizationError{
JSONObject obj = new JSONObject();
Date date;
try{
//get format pattern from android device (Will only have device specific formatting for short form of date) or options supplied
DateFormat fmt = new SimpleDateFormat(getDatePattern(options).getString("pattern"));
//attempt parsing string based on user preferences
date = fmt.parse(options.getJSONObject(0).get(DATESTRING).toString());
//set Android Time object
Time time = new Time();
time.set(date.getTime());
//return properties;
obj.put("year", time.year);
obj.put("month", time.month);
obj.put("day", time.monthDay);
obj.put("hour", time.hour);
obj.put("minute", time.minute);
obj.put("second", time.second);
obj.put("millisecond", Long.valueOf(0));
return obj;
}catch(Exception ge){
throw new GlobalizationError(GlobalizationError.PARSING_ERROR);
}
}
private JSONObject getStringtoDate(JSONArray options)throws GlobalizationError{
JSONObject obj = new JSONObject();
Date date;
try{
//get format pattern from android device (Will only have device specific formatting for short form of date) or options supplied
DateFormat fmt = new SimpleDateFormat(getDatePattern(options).getString("pattern"));
//attempt parsing string based on user preferences
date = fmt.parse(options.getJSONObject(0).get(DATESTRING).toString());
//set Android Time object
Time time = new Time();
time.set(date.getTime());
//return properties;
obj.put("year", time.year);
obj.put("month", time.month);
obj.put("day", time.monthDay);
obj.put("hour", time.hour);
obj.put("minute", time.minute);
obj.put("second", time.second);
obj.put("millisecond", new Long(0));
return obj;
}catch(Exception ge){
throw new GlobalizationError(GlobalizationError.PARSING_ERROR);
}
}
public static boolean isToday(long l)
{
Time time = new Time();
time.set(l);
int j = time.year;
int k = time.month;
int i1 = time.monthDay;
time.set(System.currentTimeMillis());
return j == time.year && k == time.month && i1 == time.monthDay;
}
@Override
public Time get(ContentSet values)
{
Long timestamp = values.getAsLong(mTimestampField);
if (timestamp == null)
{
// if the time stamp is null we return null
return null;
}
// create a new Time for the given time zone, falling back to UTC if none is given
String timezone = mTzField == null ? Time.TIMEZONE_UTC : values.getAsString(mTzField);
Time value = new Time(timezone == null ? Time.TIMEZONE_UTC : timezone);
// set the time stamp
value.set(timestamp);
// cache mAlldayField locally
String allDayField = mAllDayField;
// set the allday flag appropriately
Integer allDayInt = allDayField == null ? null : values.getAsInteger(allDayField);
if ((allDayInt != null && allDayInt != 0) || (allDayField == null && mAllDayDefault))
{
value.set(value.monthDay, value.month, value.year);
value.timezone = Time.TIMEZONE_UTC;
}
return value;
}
public static long parse(String timeString)
throws IllegalArgumentException {
int date = 1;
int month = Calendar.JANUARY;
int year = 1970;
TimeOfDay timeOfDay;
Matcher rfcMatcher = HTTP_DATE_RFC_PATTERN.matcher(timeString);
if (rfcMatcher.find()) {
date = getDate(rfcMatcher.group(1));
month = getMonth(rfcMatcher.group(2));
year = getYear(rfcMatcher.group(3));
timeOfDay = getTime(rfcMatcher.group(4));
} else {
Matcher ansicMatcher = HTTP_DATE_ANSIC_PATTERN.matcher(timeString);
if (ansicMatcher.find()) {
month = getMonth(ansicMatcher.group(1));
date = getDate(ansicMatcher.group(2));
timeOfDay = getTime(ansicMatcher.group(3));
year = getYear(ansicMatcher.group(4));
} else {
throw new IllegalArgumentException();
}
}
// FIXME: Y2038 BUG!
if (year >= 2038) {
year = 2038;
month = Calendar.JANUARY;
date = 1;
}
Time time = new Time(Time.TIMEZONE_UTC);
time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date,
month, year);
return time.toMillis(false /* use isDst */);
}
private void addTestingTicket() {
Ticket ticket = new Ticket();
if (Locale.getDefault().toString().startsWith("en")) {
ticket.setCity("Prague");
} else {
ticket.setCity("Praha");
}
ticket.setCityId(1);
ticket.setHash("bhAJpWP9B / 861418");
ticket.setStatus(TicketProvider.Tickets.STATUS_DELIVERED);
ticket.setText("DP hl.m.Prahy, a.s., Jizdenka prestupni 32,- Kc, Platnost od: 29.8.11 8:09 do: 29.8.11 9:39. Pouze v pasmu P. WzL9n3JuQ /" +
" " +
"169605");
int second = 1000;
int minute = 60 * second;
int hour = 60 * minute;
int day = 24 * hour;
Time time = new Time();
time.setToNow();
ticket.setOrdered(time);
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.SECOND, 0);
long nowFullMinute = calendar.getTimeInMillis();
time.set(nowFullMinute);
ticket.setValidFrom(time);
Time time2 = new Time();
time2.set(nowFullMinute + 12 * minute);
ticket.setValidTo(time2);
SmsReceiverService.call(c, ticket);
}
public static long parse(String timeString)
throws IllegalArgumentException {
int date = 1;
int month = Calendar.JANUARY;
int year = 1970;
TimeOfDay timeOfDay;
Matcher rfcMatcher = HTTP_DATE_RFC_PATTERN.matcher(timeString);
if (rfcMatcher.find()) {
date = getDate(rfcMatcher.group(1));
month = getMonth(rfcMatcher.group(2));
year = getYear(rfcMatcher.group(3));
timeOfDay = getTime(rfcMatcher.group(4));
} else {
Matcher ansicMatcher = HTTP_DATE_ANSIC_PATTERN.matcher(timeString);
if (ansicMatcher.find()) {
month = getMonth(ansicMatcher.group(1));
date = getDate(ansicMatcher.group(2));
timeOfDay = getTime(ansicMatcher.group(3));
year = getYear(ansicMatcher.group(4));
} else {
throw new IllegalArgumentException();
}
}
// FIXME: Y2038 BUG!
if (year >= 2038) {
year = 2038;
month = Calendar.JANUARY;
date = 1;
}
Time time = new Time(Time.TIMEZONE_UTC);
time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date,
month, year);
return time.toMillis(false /* use isDst */);
}
public static long parse(String timeString)
throws IllegalArgumentException {
int date = 1;
int month = Calendar.JANUARY;
int year = 1970;
TimeOfDay timeOfDay;
Matcher rfcMatcher = HTTP_DATE_RFC_PATTERN.matcher(timeString);
if (rfcMatcher.find()) {
date = getDate(rfcMatcher.group(1));
month = getMonth(rfcMatcher.group(2));
year = getYear(rfcMatcher.group(3));
timeOfDay = getTime(rfcMatcher.group(4));
} else {
Matcher ansicMatcher = HTTP_DATE_ANSIC_PATTERN.matcher(timeString);
if (ansicMatcher.find()) {
month = getMonth(ansicMatcher.group(1));
date = getDate(ansicMatcher.group(2));
timeOfDay = getTime(ansicMatcher.group(3));
year = getYear(ansicMatcher.group(4));
} else {
throw new IllegalArgumentException();
}
}
// FIXME: Y2038 BUG!
if (year >= 2038) {
year = 2038;
month = Calendar.JANUARY;
date = 1;
}
Time time = new Time(Time.TIMEZONE_UTC);
time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date,
month, year);
return time.toMillis(false /* use isDst */);
}
private boolean isAlarmForToday() {
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + nextUpdate;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
Log.v(TAG, "water time: " + nextUpdateTime.hour);
//only between 8 am and 18 pm
return nextUpdateTime.hour > 7 && nextUpdateTime.hour < 18;
}
@Override
public Cursor getCursor()
{
mTime.setToNow();
MatrixCursor result = new MatrixCursor(mProjection);
Time time = new Time(mTimezone.getID());
time.set(mTime.monthDay + 1, mTime.month, mTime.year);
// today row (including overdue)
long t2 = time.toMillis(false);
result.addRow(makeRow(1, TYPE_END_OF_TODAY, MIN_TIME, t2));
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// tomorrow row
long t3 = time.toMillis(false);
result.addRow(makeRow(2, TYPE_END_OF_TOMORROW, t2, t3));
time.monthDay += 5;
time.yearDay += 5;
time.normalize(true);
// next week row
long t4 = time.toMillis(false);
result.addRow(makeRow(3, TYPE_END_IN_7_DAYS, t3, t4));
time.monthDay += 1;
time.normalize(true);
// open future for future tasks (including tasks without dates)
if (mProjectionList.contains(RANGE_OPEN_FUTURE))
{
result.addRow(makeRow(4, TYPE_NO_END, t4, null));
}
return result;
}
public Cursor getCursor()
{
mTime.setToNow();
MatrixCursor result = new MatrixCursor(mProjection);
// get time of today 00:00:00
Time time = new Time(mTimezone.getID());
time.set(mTime.monthDay, mTime.month, mTime.year);
// null row, for tasks without due date
if (mProjectionList.contains(RANGE_NULL_ROW))
{
result.addRow(makeRow(1, 0, null, null));
}
long t1 = time.toMillis(false);
// open past row for overdue tasks
if (mProjectionList.contains(RANGE_OPEN_PAST))
{
result.addRow(makeRow(2, TYPE_END_OF_YESTERDAY, MIN_TIME, t1));
}
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// today row
long t2 = time.toMillis(false);
result.addRow(makeRow(3, TYPE_END_OF_TODAY, t1, t2));
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// tomorrow row
long t3 = time.toMillis(false);
result.addRow(makeRow(4, TYPE_END_OF_TOMORROW, t2, t3));
time.monthDay += 5;
time.yearDay += 5;
time.normalize(true);
// next week row
long t4 = time.toMillis(false);
result.addRow(makeRow(5, TYPE_END_IN_7_DAYS, t3, t4));
time.set(1, time.month + 1, time.year);
time.normalize(true);
// month row
long t5 = time.toMillis(false);
result.addRow(makeRow(6, TYPE_END_OF_A_MONTH, t4, t5));
time.set(1, 0, time.year + 1);
// rest of year row
long t6 = time.toMillis(false);
result.addRow(makeRow(7, TYPE_END_OF_A_YEAR, t5, t6));
// open future for future tasks
if (mProjectionList.contains(RANGE_OPEN_FUTURE))
{
result.addRow(makeRow(8, TYPE_NO_END, t6, MAX_TIME));
}
return result;
}
/**
* Format the given due date. The result depends on the current date and on the all-day flag of the due date.
*
* @param date
* The due date to format.
* @return A string with the formatted due date.
*/
public String format(Time date, Time now, DateFormatContext dateContext)
{
// normalize time to ensure yearDay is set properly
date.normalize(false);
if (dateContext.useRelative(now, date))
{
long delta = Math.abs(now.toMillis(false) - date.toMillis(false));
if (date.allDay)
{
Time allDayNow = new Time("UTC");
allDayNow.set(now.monthDay, now.month, now.year);
return DateUtils.getRelativeTimeSpanString(date.toMillis(false), allDayNow.toMillis(false), DAY_IN_MILLIS).toString();
}
else if (delta < 60 * 1000)
{
// the time is within this minute, show "now"
return mContext.getString(R.string.now);
}
else if (delta < 60 * 60 * 1000)
{
// time is within this hour, show number of minutes left
return DateUtils.getRelativeTimeSpanString(date.toMillis(false), now.toMillis(false), DateUtils.MINUTE_IN_MILLIS).toString();
}
else if (delta < 24 * 60 * 60 * 1000)
{
// time is within 24 hours, show relative string with time
// FIXME: instead of using a fixed 24 hour interval this should be aligned to midnight tomorrow and yesterday
return routingGetRelativeDateTimeString(mContext, date.toMillis(false), DAY_IN_MILLIS, WEEK_IN_MILLIS,
dateContext.getDateUtilsFlags(now, date)).toString();
}
else
{
return DateUtils.getRelativeTimeSpanString(date.toMillis(false), now.toMillis(false), DAY_IN_MILLIS).toString();
}
}
return date.allDay ? formatAllDay(date, now, dateContext) : formatNonAllDay(date, now, dateContext);
}
@Override
public Cursor getCursor()
{
mTime.setToNow();
MatrixCursor result = new MatrixCursor(mProjection);
// get time of today 00:00:00
Time time = new Time(mTime.timezone);
time.set(mTime.monthDay, mTime.month, mTime.year);
// already started row
long t1 = time.toMillis(false);
result.addRow(makeRow(1, TYPE_OVERDUE, MIN_TIME, t1));
time.hour = 0;
time.minute = 0;
time.second = 0;
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// today row
long t2 = time.toMillis(false);
result.addRow(makeRow(2, TYPE_END_OF_TODAY, t1, t2));
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// tomorrow row
long t3 = time.toMillis(false);
result.addRow(makeRow(3, TYPE_END_OF_TOMORROW, t2, t3));
time.monthDay += 5;
time.yearDay += 5;
time.normalize(true);
// next week row
long t4 = time.toMillis(false);
result.addRow(makeRow(4, TYPE_END_IN_7_DAYS, t3, t4));
time.monthDay += 1;
time.normalize(true);
// open past future for future tasks (including tasks without dates)
if (mProjectionList.contains(RANGE_OPEN_FUTURE))
{
result.addRow(makeRow(5, TYPE_NO_END, t4, MAX_TIME));
}
return result;
}