[android-beginners] Re: How to format time in millis to human readable?
Just out of curiosity, has anybody tried using Joda Time (http://joda- time.sourceforge.net/) in an Android application? The Joda API is much better than Java's built-in Date and Calendar classes. ++Steve On Mar 8, 12:54 pm, sm1 sergemas...@gmail.com wrote: There may be built-in methods for such things as 5 minutes ago but I don't know any yet. for the current time in human-readable form, using some default date display format (which may or may not be dependent on user-preferred locale and date format selection, this remains TBD for Android), the fastest way is: +new Date() and with a given time in millis, it would be as in: Build.TIME +Build.TIME+ = +new Date(Build.TIME) Calendar is recommended for conversions (e.g., time zone changes) and for other calculations with dates, such add, after, before, compareTo. When you create a Calendar, it is already set to the current time: Calendar cal = Calendar.getInstance(); is usually equivalent and preferable to: long millis = System.currentTimeMillis(); //don't do these 4 statements Date date = new Date(millis); Calendar c = Calendar.getInstance(); c.setTime(date); And if you need to use SimpleDateFormat, because for example you don't want to use the default format you get from Date, then take special care because some implementations are not multithreaded, therefore unless you know for sure that Android's implementation is multithreaded, then don't cache the instance and use a local variable that only lives in the method, don't use a field. This makes SimpleDateFormat somewhat expansive to use but usually it is only used for some UI work (i.e., not background work), therefore using it may be OK for many apps. happy coding serge On Mar 8, 4:33 am, TAKEphONE shimo...@gmail.com wrote: Hi, Is there any internal method/object I can use to return a format like the built-in call log does ? (i.e. 5 minutes ago, 2 days ago...) On Mar 6, 2:55 pm, Tseng tseng.priv...@googlemail.com wrote: I wouldn't really use this method to be honest. Even the Android Documentation sugest to use native methods, instead of writing your own stuff for methods which are already available (i.e. simple date formating). [Use Native Methods]http://developer.android.com/guide/practices/design/performance.html#... On Mar 6, 1:20 pm, droozen droozenr...@gmail.com wrote: Sometimes I construct my human readable strings in a separate function by myself, mostly because I imagine it's faster. Something like. int iMonth = cal.get(Calendar.MONTH) + 1; // Months from the calendar are offset by one. Add one if you want human readable. int iDay = cal.get(Calendar.DAY_OF_MONTH); String month = Integer.toString(iMonth); if(iMonth 10){ month = 0 + month; // Otherwise, you might get something like 1/1/1900, instead of 01/01/1900 } String day = Integer.toString(iDay); if(iDay 10){ day = 0 + day; } String humanReadable = month + / + day + / cal.get(Calendar.YEAR); But really, you should be using SimpleDateFormat, I suppose... On Mar 6, 5:55 am, Łukasz Warchoł warchol...@gmail.com wrote: - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: How to format time in millis to human readable?
Hi, Is there any internal method/object I can use to return a format like the built-in call log does ? (i.e. 5 minutes ago, 2 days ago...) On Mar 6, 2:55 pm, Tseng tseng.priv...@googlemail.com wrote: I wouldn't really use this method to be honest. Even the Android Documentation sugest to use native methods, instead of writing your own stuff for methods which are already available (i.e. simple date formating). [Use Native Methods]http://developer.android.com/guide/practices/design/performance.html#... On Mar 6, 1:20 pm, droozen droozenr...@gmail.com wrote: Sometimes I construct my human readable strings in a separate function by myself, mostly because I imagine it's faster. Something like. int iMonth = cal.get(Calendar.MONTH) + 1; // Months from the calendar are offset by one. Add one if you want human readable. int iDay = cal.get(Calendar.DAY_OF_MONTH); String month = Integer.toString(iMonth); if(iMonth 10){ month = 0 + month; // Otherwise, you might get something like 1/1/1900, instead of 01/01/1900 } String day = Integer.toString(iDay); if(iDay 10){ day = 0 + day; } String humanReadable = month + / + day + / cal.get(Calendar.YEAR); But really, you should be using SimpleDateFormat, I suppose... On Mar 6, 5:55 am, Łukasz Warchoł warchol...@gmail.com wrote: - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: How to format time in millis to human readable?
There may be built-in methods for such things as 5 minutes ago but I don't know any yet. for the current time in human-readable form, using some default date display format (which may or may not be dependent on user-preferred locale and date format selection, this remains TBD for Android), the fastest way is: +new Date() and with a given time in millis, it would be as in: Build.TIME +Build.TIME+ = +new Date(Build.TIME) Calendar is recommended for conversions (e.g., time zone changes) and for other calculations with dates, such add, after, before, compareTo. When you create a Calendar, it is already set to the current time: Calendar cal = Calendar.getInstance(); is usually equivalent and preferable to: long millis = System.currentTimeMillis(); //don't do these 4 statements Date date = new Date(millis); Calendar c = Calendar.getInstance(); c.setTime(date); And if you need to use SimpleDateFormat, because for example you don't want to use the default format you get from Date, then take special care because some implementations are not multithreaded, therefore unless you know for sure that Android's implementation is multithreaded, then don't cache the instance and use a local variable that only lives in the method, don't use a field. This makes SimpleDateFormat somewhat expansive to use but usually it is only used for some UI work (i.e., not background work), therefore using it may be OK for many apps. happy coding serge On Mar 8, 4:33 am, TAKEphONE shimo...@gmail.com wrote: Hi, Is there any internal method/object I can use to return a format like the built-in call log does ? (i.e. 5 minutes ago, 2 days ago...) On Mar 6, 2:55 pm, Tseng tseng.priv...@googlemail.com wrote: I wouldn't really use this method to be honest. Even the Android Documentation sugest to use native methods, instead of writing your own stuff for methods which are already available (i.e. simple date formating). [Use Native Methods]http://developer.android.com/guide/practices/design/performance.html#... On Mar 6, 1:20 pm, droozen droozenr...@gmail.com wrote: Sometimes I construct my human readable strings in a separate function by myself, mostly because I imagine it's faster. Something like. int iMonth = cal.get(Calendar.MONTH) + 1; // Months from the calendar are offset by one. Add one if you want human readable. int iDay = cal.get(Calendar.DAY_OF_MONTH); String month = Integer.toString(iMonth); if(iMonth 10){ month = 0 + month; // Otherwise, you might get something like 1/1/1900, instead of 01/01/1900 } String day = Integer.toString(iDay); if(iDay 10){ day = 0 + day; } String humanReadable = month + / + day + / cal.get(Calendar.YEAR); But really, you should be using SimpleDateFormat, I suppose... On Mar 6, 5:55 am, Łukasz Warchoł warchol...@gmail.com wrote: - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: How to format time in millis to human readable?
That page also said not to use native methods for trivial computation. In my experience the Java Calendar and Date objects are expensive and memory hungry. Avoid them, including their DateFormatters, whenever possible. That being said, it probably shouldn't matter much unless you are performing many calculations in a loop. So the only real time to consider avoiding them, I guess, is when you are investigating performance problems. On Mar 6, 6:55 am, Tseng tseng.priv...@googlemail.com wrote: I wouldn't really use this method to be honest. Even the Android Documentation sugest to use native methods, instead of writing your own stuff for methods which are already available (i.e. simple date formating). [Use Native Methods]http://developer.android.com/guide/practices/design/performance.html#... On Mar 6, 1:20 pm, droozen droozenr...@gmail.com wrote: Sometimes I construct my human readable strings in a separate function by myself, mostly because I imagine it's faster. Something like. int iMonth = cal.get(Calendar.MONTH) + 1; // Months from the calendar are offset by one. Add one if you want human readable. int iDay = cal.get(Calendar.DAY_OF_MONTH); String month = Integer.toString(iMonth); if(iMonth 10){ month = 0 + month; // Otherwise, you might get something like 1/1/1900, instead of 01/01/1900 } String day = Integer.toString(iDay); if(iDay 10){ day = 0 + day; } String humanReadable = month + / + day + / cal.get(Calendar.YEAR); But really, you should be using SimpleDateFormat, I suppose... On Mar 6, 5:55 am, Łukasz Warchoł warchol...@gmail.com wrote: - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: How to format time in millis to human readable?
I would use: Date date = new Date(); date.setTime(milliseconds); and now from date you can read all: year, month, day, hour, min. and sec. Marcus pisze: Hi, as I understand, the internal date-format is the time in millis, since it is the format to store in sqlite and the format I get from System.currentTimeMillis(). But how can I convert this back to a human readable format? Do I have to use new Date(int) and then use a SimpleDateFormat? Or is there any convinience method? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: How to format time in millis to human readable?
The Date object works, but much better to use Calendar. Calendar cal = Calendar.getInstance(); cal.setTime(milliseconds); SimpleDateFormat sdf = new SimpleDateFormat(MM/dd/); // see javadocs for how to construct date strings. sdf.format(cal.getTime()); Sometimes I construct my human readable strings in a separate function by myself, mostly because I imagine it's faster. Something like. int iMonth = cal.get(Calendar.MONTH) + 1; // Months from the calendar are offset by one. Add one if you want human readable. int iDay = cal.get(Calendar.DAY_OF_MONTH); String month = Integer.toString(iMonth); if(iMonth 10){ month = 0 + month; // Otherwise, you might get something like 1/1/1900, instead of 01/01/1900 } String day = Integer.toString(iDay); if(iDay 10){ day = 0 + day; } String humanReadable = month + / + day + / cal.get(Calendar.YEAR); But really, you should be using SimpleDateFormat, I suppose... On Mar 6, 5:55 am, Łukasz Warchoł warchol...@gmail.com wrote: I would use: Date date = new Date(); date.setTime(milliseconds); and now from date you can read all: year, month, day, hour, min. and sec. Marcus pisze: Hi, as I understand, the internal date-format is the time in millis, since it is the format to store in sqlite and the format I get from System.currentTimeMillis(). But how can I convert this back to a human readable format? Do I have to use new Date(int) and then use a SimpleDateFormat? Or is there any convinience method? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: How to format time in millis to human readable?
I wouldn't really use this method to be honest. Even the Android Documentation sugest to use native methods, instead of writing your own stuff for methods which are already available (i.e. simple date formating). [Use Native Methods] http://developer.android.com/guide/practices/design/performance.html#native_methods On Mar 6, 1:20 pm, droozen droozenr...@gmail.com wrote: Sometimes I construct my human readable strings in a separate function by myself, mostly because I imagine it's faster. Something like. int iMonth = cal.get(Calendar.MONTH) + 1; // Months from the calendar are offset by one. Add one if you want human readable. int iDay = cal.get(Calendar.DAY_OF_MONTH); String month = Integer.toString(iMonth); if(iMonth 10){ month = 0 + month; // Otherwise, you might get something like 1/1/1900, instead of 01/01/1900 } String day = Integer.toString(iDay); if(iDay 10){ day = 0 + day; } String humanReadable = month + / + day + / cal.get(Calendar.YEAR); But really, you should be using SimpleDateFormat, I suppose... On Mar 6, 5:55 am, Łukasz Warchoł warchol...@gmail.com wrote: - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: How to format time in millis to human readable?
Hi Marcus, if I get you correctly, you want to display a date in a readable format that comes from miiliseconds. System.currentTimeMillis() returns a primitive long, so Date date = new Date(System.currentTimeMillis()) will not work. Try this: long millis = System.currentTimeMillis(); Date date = new Date(millis); Calendar c = Calendar.getInstance(); c.setTime(date); System.out.println(millis + ms correspond to mm-dd- + c.get (Calendar.MONTH) + - + c.get(Calendar.DATE) + - + c.get (Calendar.YEAR)); Ciao, Tommaso On 6 Mrz., 09:33, Marcus marcus.ter...@gmail.com wrote: Hi, as I understand, the internal date-format is the time in millis, since it is the format to store in sqlite and the format I get from System.currentTimeMillis(). But how can I convert this back to a human readable format? Do I have to use new Date(int) and then use a SimpleDateFormat? Or is there any convinience method? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---