Android Development record 19-Friends circle Dynamic release Time calculation

Source: Internet
Author: User
Tags time in milliseconds

Android Development record 19-Friends circle Dynamic release Time calculation
reprint Please specify: It_xiao, http://blog.csdn.net/wwj_748If you feel good, please go to the 2014 blog star vote address: Http://vote.blog.csdn.net/blogstar2014/details?username=wwj_748#contentthe previous article gives you the sharing of time knowledge points http://blog.csdn.net/wwj_748/article/details/ 11536421, this blog I would like to share with you how the calendar to calculate our usual need to use the year, month, day, time, minutes, seconds, etc., and how to calculate a similar dynamic release of the Circle of friends time. Here are a few important classes to understand: Calendar class: Calendars Date class: Date date format class: SimpleDateFormat Basically, some of our conversions for dates are in these classes, and there are several common conversions: get relevant date information through the calendar, such as the current month and year, the date of the month, the next year, and so on, here you can see the author about the custom calendar of a blog post, there are related dates work class: Http://blog.csdn.net /wwj_748/article/details/42244865conversion between date and calendar conversion of dates to date strings

let's start by looking at how to use the calendar to get the date information we want:
get current calendar:
Calendar calendar = Calendar.getinstance ();

get information based on fields get current year:
Calendar.getinstance (). Get (Calendar.year)
get current Month:
Calendar.getinstance (). Get (Calendar.month) + 1
Note that this is +1 because the month is starting from 0. gets the day ordinal of the current month:
Calendar.get (Calendar.day_of_month)
gets the current time in milliseconds displayed:
Calendar.gettimeinmillis ()

Calendar can also do a lot of calculations, the author in the development of the date control, I did a lot of dates on the operation:use the Add method of the calendar to perform operations on the specified field, for example:
Get last year public static int getlastyear () {Calendar c = calendar.getinstance (); C.add (Calendar.year,-1); return C.get ( Calendar.year);} Gets the next yearly copy of public static int getnextyear () {Calendar c = calendar.getinstance (); C.add (calendar.year, 1); return C.get ( Calendar.year);}

Gets the month of the last one months public static int Getlastmonth () {Calendar c = calendar.getinstance (); C.add (Calendar.month,-1); return C.get (calendar.month) + 1;} Gets the month of the next one months public static int Getnextmonth () {Calendar c = calendar.getinstance (); C.add (Calendar.month, 1); return C.get ( Calendar.month) + 1;}


There are a lot of fields for the calendar and I want readers to look at the API and try it out.



conversion between date and calendarwe can set the Calendar object for the time by calling the SetTime method of the calendar
Calendar.settime (New Date ())

Conversion of dates to date strings
in practical development, we often need to format the date data returned by the server, and here we need to use the Simledateformat, we can specify the date format:
private static SimpleDateFormat Date_format_till_second = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

We get a date string that you can convert to a Date object:
/** * Date string converted to date * @param datestr * @param format * @return */public static date strtodate (string datestr, string format {Date date = Null;if (! Textutils.isempty (DATESTR)) {DateFormat df = new SimpleDateFormat (format); try {date = Df.parse (DATESTR);} catch ( ParseException e) {e.printstacktrace ();}} return date;}


You can also convert the Date object we get to a string of the specified format:
/** * Date converted to String * @param timestr * @param format * @return */public static string datetostring (string timestr, string forma T) {//Determine if this is the date of the current date = Datehelper.strtodate (timestr, format); Calendar calendar = Calendar.getinstance (); Calendar.settime (date);//If it is this year, go to "XX month XX Day" date format if (Calendar.get ( calendar.year) = = GetYear ()) {return Date_format_till_day_current_year.format (DATE);} Return Date_format_till_day_ch.format (DATE);}


the last thing to say is that in development we need to calculate a dynamic release time, generally we get a date string, we need to calculate the current time and dynamic release time, calculate their difference to determine when the release, the code:
/** * Date Logic * @param datestr Date String * @return */public static string Timelogic (string datestr) {Calendar calendar = Calendar . getinstance (); Calendar.get (calendar.day_of_month); Long now = Calendar.gettimeinmillis ();D ate Date = Strtodate ( Datestr, date_format); Calendar.settime (DATE); Long past = Calendar.gettimeinmillis ();//difference in seconds long time = (now-past)/100 0; StringBuffer sb = new StringBuffer (), if (Time > 0 && Time < 60) {//1 hours return Sb.append (time + seconds ago). tostr ing ();} else if (Time > && time < 3600) {return sb.append (time/60+ "Minutes Ago"). ToString ();} else if (Time >= 3600 && Time < 3600 *) {return sb.append (time/3600 + "Hour Ago"). ToString ();} else if (Time >= 3600 * && Time < 3600 *) {return sb.append ("Yesterday"). ToString (); else if (Time >= 3600 * && Time < 3600 *) {return sb.append ("The day Before"). ToString (); else if (Time >= 3600 *) {return datetostring (datestr, date_format);} Return datetostring (DATESTR, Date_format);} 

The effect is similar to this:The record here, here, a little bit, development we often encounter some of the knowledge points can be collated and summarized, mobile product development is required to express the iteration, the development of knowledge points of the record to their own great help, improve your own development efficiency, corresponding to your ability to ascend, Salary will follow up, Little Witch's personal point of view, thank you for your patience to read.



Android Development record 19-Friends circle Dynamic release Time calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.