|
Android example source code file (Time.java)
The Time.java Android example source code/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.text.format; import android.content.res.Resources; import java.util.Locale; import java.util.TimeZone; /** * The Time class is a faster replacement for the java.util.Calendar and * java.util.GregorianCalendar classes. An instance of the Time class represents * a moment in time, specified with second precision. It is modelled after * struct tm, and in fact, uses struct tm to implement most of the * functionality. */ public class Time { private static final String Y_M_D_T_H_M_S_000 = "%Y-%m-%dT%H:%M:%S.000"; private static final String Y_M_D_T_H_M_S_000_Z = "%Y-%m-%dT%H:%M:%S.000Z"; private static final String Y_M_D = "%Y-%m-%d"; public static final String TIMEZONE_UTC = "UTC"; /** * The Julian day of the epoch, that is, January 1, 1970 on the Gregorian * calendar. */ public static final int EPOCH_JULIAN_DAY = 2440588; /** * True if this is an allDay event. The hour, minute, second fields are * all zero, and the date is displayed the same in all time zones. */ public boolean allDay; /** * Seconds [0-61] (2 leap seconds allowed) */ public int second; /** * Minute [0-59] */ public int minute; /** * Hour of day [0-23] */ public int hour; /** * Day of month [1-31] */ public int monthDay; /** * Month [0-11] */ public int month; /** * Year. TBD. Is this years since 1900 like in struct tm? */ public int year; /** * Day of week [0-6] */ public int weekDay; /** * Day of year [0-365] */ public int yearDay; /** * This time is in daylight savings time. One of: * <ul> * <li>positive - in dst * <li>0 - not in dst * <li>negative - unknown * </ul> */ public int isDst; /** * Offset from UTC (in seconds). */ public long gmtoff; /** * The timezone for this Time. Should not be null. */ public String timezone; /* * Define symbolic constants for accessing the fields in this class. Used in * getActualMaximum(). */ public static final int SECOND = 1; public static final int MINUTE = 2; public static final int HOUR = 3; public static final int MONTH_DAY = 4; public static final int MONTH = 5; public static final int YEAR = 6; public static final int WEEK_DAY = 7; public static final int YEAR_DAY = 8; public static final int WEEK_NUM = 9; public static final int SUNDAY = 0; public static final int MONDAY = 1; public static final int TUESDAY = 2; public static final int WEDNESDAY = 3; public static final int THURSDAY = 4; public static final int FRIDAY = 5; public static final int SATURDAY = 6; /* * The Locale for which date formatting strings have been loaded. */ private static Locale sLocale; private static String[] sShortMonths; private static String[] sLongMonths; private static String[] sLongStandaloneMonths; private static String[] sShortWeekdays; private static String[] sLongWeekdays; private static String sTimeOnlyFormat; private static String sDateOnlyFormat; private static String sDateTimeFormat; private static String sAm; private static String sPm; private static String sDateCommand = "%a %b %e %H:%M:%S %Z %Y"; /** * Construct a Time object in the timezone named by the string * argument "timezone". The time is initialized to Jan 1, 1970. * @param timezone string containing the timezone to use. * @see TimeZone */ public Time(String timezone) { if (timezone == null) { throw new NullPointerException("timezone is null!"); } this.timezone = timezone; this.year = 1970; this.monthDay = 1; // Set the daylight-saving indicator to the unknown value -1 so that // it will be recomputed. this.isDst = -1; } /** * Construct a Time object in the default timezone. The time is initialized to * Jan 1, 1970. */ public Time() { this(TimeZone.getDefault().getID()); } /** * A copy constructor. Construct a Time object by copying the given * Time object. No normalization occurs. * * @param other */ public Time(Time other) { set(other); } /** * Ensures the values in each field are in range. For example if the * current value of this calendar is March 32, normalize() will convert it * to April 1. It also fills in weekDay, yearDay, isDst and gmtoff. * * <p> * If "ignoreDst" is true, then this method sets the "isDst" field to -1 * (the "unknown" value) before normalizing. It then computes the * correct value for "isDst". * * <p> * See {@link #toMillis(boolean)} for more information about when to * use <tt>true or false for "ignoreDst". * * @return the UTC milliseconds since the epoch */ native public long normalize(boolean ignoreDst); /** * Convert this time object so the time represented remains the same, but is * instead located in a different timezone. This method automatically calls * normalize() in some cases */ native public void switchTimezone(String timezone); private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /** * Return the maximum possible value for the given field given the value of * the other fields. Requires that it be normalized for MONTH_DAY and * YEAR_DAY. * @param field one of the constants for HOUR, MINUTE, SECOND, etc. * @return the maximum value for the field. */ public int getActualMaximum(int field) { switch (field) { case SECOND: return 59; // leap seconds, bah humbug case MINUTE: return 59; case HOUR: return 23; case MONTH_DAY: { int n = DAYS_PER_MONTH[this.month]; if (n != 28) { return n; } else { int y = this.year; return ((y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0)) ? 29 : 28; } } case MONTH: return 11; case YEAR: return 2037; case WEEK_DAY: return 6; case YEAR_DAY: { int y = this.year; // Year days are numbered from 0, so the last one is usually 364. return ((y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0)) ? 365 : 364; } case WEEK_NUM: throw new RuntimeException("WEEK_NUM not implemented"); default: throw new RuntimeException("bad field=" + field); } } /** * Clears all values, setting the timezone to the given timezone. Sets isDst * to a negative value to mean "unknown". * @param timezone the timezone to use. */ public void clear(String timezone) { if (timezone == null) { throw new NullPointerException("timezone is null!"); } this.timezone = timezone; this.allDay = false; this.second = 0; this.minute = 0; this.hour = 0; this.monthDay = 0; this.month = 0; this.year = 0; this.weekDay = 0; this.yearDay = 0; this.gmtoff = 0; this.isDst = -1; } /** * return a negative number if a is less than b, a positive number if a is * greater than b, and 0 if they are equal. */ native public static int compare(Time a, Time b); /** * Print the current value given the format string provided. See man * strftime for what means what. The final string must be less than 256 * characters. * @param format a string containing the desired format. * @return a String containing the current time expressed in the current locale. */ public String format(String format) { synchronized (Time.class) { Locale locale = Locale.getDefault(); if (sLocale == null || locale == null || !(locale.equals(sLocale))) { Resources r = Resources.getSystem(); sShortMonths = new String[] { r.getString(com.android.internal.R.string.month_medium_january), r.getString(com.android.internal.R.string.month_medium_february), r.getString(com.android.internal.R.string.month_medium_march), r.getString(com.android.internal.R.string.month_medium_april), r.getString(com.android.internal.R.string.month_medium_may), r.getString(com.android.internal.R.string.month_medium_june), r.getString(com.android.internal.R.string.month_medium_july), r.getString(com.android.internal.R.string.month_medium_august), r.getString(com.android.internal.R.string.month_medium_september), r.getString(com.android.internal.R.string.month_medium_october), r.getString(com.android.internal.R.string.month_medium_november), r.getString(com.android.internal.R.string.month_medium_december), }; sLongMonths = new String[] { r.getString(com.android.internal.R.string.month_long_january), r.getString(com.android.internal.R.string.month_long_february), r.getString(com.android.internal.R.string.month_long_march), r.getString(com.android.internal.R.string.month_long_april), r.getString(com.android.internal.R.string.month_long_may), r.getString(com.android.internal.R.string.month_long_june), r.getString(com.android.internal.R.string.month_long_july), r.getString(com.android.internal.R.string.month_long_august), r.getString(com.android.internal.R.string.month_long_september), r.getString(com.android.internal.R.string.month_long_october), r.getString(com.android.internal.R.string.month_long_november), r.getString(com.android.internal.R.string.month_long_december), }; sLongStandaloneMonths = new String[] { r.getString(com.android.internal.R.string.month_long_standalone_january), r.getString(com.android.internal.R.string.month_long_standalone_february), r.getString(com.android.internal.R.string.month_long_standalone_march), r.getString(com.android.internal.R.string.month_long_standalone_april), r.getString(com.android.internal.R.string.month_long_standalone_may), r.getString(com.android.internal.R.string.month_long_standalone_june), r.getString(com.android.internal.R.string.month_long_standalone_july), r.getString(com.android.internal.R.string.month_long_standalone_august), r.getString(com.android.internal.R.string.month_long_standalone_september), r.getString(com.android.internal.R.string.month_long_standalone_october), r.getString(com.android.internal.R.string.month_long_standalone_november), r.getString(com.android.internal.R.string.month_long_standalone_december), }; sShortWeekdays = new String[] { r.getString(com.android.internal.R.string.day_of_week_medium_sunday), r.getString(com.android.internal.R.string.day_of_week_medium_monday), r.getString(com.android.internal.R.string.day_of_week_medium_tuesday), r.getString(com.android.internal.R.string.day_of_week_medium_wednesday), r.getString(com.android.internal.R.string.day_of_week_medium_thursday), r.getString(com.android.internal.R.string.day_of_week_medium_friday), r.getString(com.android.internal.R.string.day_of_week_medium_saturday), }; sLongWeekdays = new String[] { r.getString(com.android.internal.R.string.day_of_week_long_sunday), r.getString(com.android.internal.R.string.day_of_week_long_monday), r.getString(com.android.internal.R.string.day_of_week_long_tuesday), r.getString(com.android.internal.R.string.day_of_week_long_wednesday), r.getString(com.android.internal.R.string.day_of_week_long_thursday), r.getString(com.android.internal.R.string.day_of_week_long_friday), r.getString(com.android.internal.R.string.day_of_week_long_saturday), }; sTimeOnlyFormat = r.getString(com.android.internal.R.string.time_of_day); sDateOnlyFormat = r.getString(com.android.internal.R.string.month_day_year); sDateTimeFormat = r.getString(com.android.internal.R.string.date_and_time); sAm = r.getString(com.android.internal.R.string.am); sPm = r.getString(com.android.internal.R.string.pm); sLocale = locale; } return format1(format); } } native private String format1(String format); /** * Return the current time in YYYYMMDDTHHMMSS<tz> format */ @Override native public String toString(); /** * Parses a date-time string in either the RFC 2445 format or an abbreviated * format that does not include the "time" field. For example, all of the * following strings are valid: * * <ul> * <li>"20081013T160000Z" * <li>"20081013T160000" * <li>"20081013" * </ul> * * Returns whether or not the time is in UTC (ends with Z). If the string * ends with "Z" then the timezone is set to UTC. If the date-time string * included only a date and no time field, then the <code>allDay * field of this Time class is set to true and the <code>hour, * <code>minute, and Other Android examples (source code examples)Here is a short list of links related to this Android Time.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.