All date/time in java is epoch based anyway, so I would just store it as a long until you need to display it then use a Date object to format the output, small & fast :)
On Jan 27, 11:20 pm, Zsolt Vasvari <[email protected]> wrote: > Sorry, I take that back.... > > I define my columns as DATETIME and then use the following static > class (feel free to use it as you wish) to convert 'long' values to > DateStamp format. SQLite can sort on that format. > > import java.util.Calendar; > import android.database.Cursor; > > // This is a replacement for java.sql.Timestamp which is way too slow > final class Timestamp > { > static Calendar calendar; > > static > { > calendar = Calendar.getInstance(); > } > > // Can't instantiate this class > private Timestamp() > { > // Nothing to do > } > > static CharSequence formatCurrentTimestamp() > { > return formatTimestamp(System.currentTimeMillis()); > } > > static CharSequence formatTimestamp(long time) > { > calendar.setTimeInMillis(time); > > int year = calendar.get(Calendar.YEAR); > int month = calendar.get(Calendar.MONTH) + 1; > int day = calendar.get(Calendar.DAY_OF_MONTH); > int hour = calendar.get(Calendar.HOUR_OF_DAY); > int minute = calendar.get(Calendar.MINUTE); > int second = calendar.get(Calendar.SECOND); > int millisecond = calendar.get(Calendar.MILLISECOND); > > // 01234567890123456789012 > // YYYY-MM-DD HH:MM:SS:MMM > StringBuilder s = new StringBuilder(23); > s.append(year); > s.append('-'); > if (month < 10) > s.append('0'); > s.append(month); > s.append('-'); > if (day < 10) > s.append('0'); > s.append(day); > s.append(' '); > if (hour < 10) > s.append('0'); > s.append(hour); > s.append(':'); > if (minute < 10) > s.append('0'); > s.append(minute); > s.append(':'); > if (second < 10) > s.append('0'); > s.append(second); > s.append('.'); > if (millisecond < 100) > s.append('0'); > if (millisecond < 10) > s.append('0'); > s.append(millisecond); > > return s; > } > > static long parseTimestampOrZeroFromCursor(Cursor cursor, int > columnId) > { > long time; > CharSequence s = cursor.getString(columnId); > > if ((s.length() == 1) && (s.charAt(0) == '0')) > time = 0; > else > time = parseTimestampFromCursorCommon(s); > > return time; > } > > static long parseTimestampFromCursor(Cursor cursor, int columnId) > { > return parseTimestampFromCursorCommon(cursor.getString > (columnId)); > } > > static private long parseTimestampFromCursorCommon(CharSequence s) > { > // 01234567890123456789012 > // YYYY-MM-DD HH:MM:SS:MMM > int year = ((s.charAt(0) - '0') * 1000) + ((s.charAt(1) - '0') > * 100) + ((s.charAt(2) - '0') * 10) + (s.charAt(3) - '0'); > int month = ((s.charAt(5) - '0') * 10) + (s.charAt(6) - '0'); > int day = ((s.charAt(8) - '0') * 10) + (s.charAt(9) - '0'); > int hour = ((s.charAt(11) - '0') * 10) + (s.charAt(12) - '0'); > int minute = ((s.charAt(14) - '0') * 10) + (s.charAt(15) - > '0'); > int second = ((s.charAt(17) - '0') * 10) + (s.charAt(18) - > '0'); > int millisecond = ((s.charAt(20) - '0') * 100) + ((s.charAt > (21) - '0') * 10) + (s.charAt(22) - '0'); > > calendar.set(Calendar.YEAR, year); > calendar.set(Calendar.MONTH, month - 1); > calendar.set(Calendar.DAY_OF_MONTH, day); > calendar.set(Calendar.HOUR_OF_DAY, hour); > calendar.set(Calendar.MINUTE, minute); > calendar.set(Calendar.SECOND, second); > calendar.set(Calendar.MILLISECOND, millisecond); > > return calendar.getTimeInMillis(); > } > > } > > On Jan 28, 3:16 pm, Zsolt Vasvari <[email protected]> wrote: > > > > > I just store a "long", as returned from getTime() > > > On Jan 28, 1:10 pm, Frank Weiss <[email protected]> wrote: > > > > Another option for not-too-large databases is ISO-8601, like > > > 20100127T134900Z, or just 20100127, etc. It's designed to sort and compare > > > as simple strings. Downside is it takes more space in the database.- Hide > > > quoted text - > > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

