Android development record-Calculation of the dynamic release time of the circle of friends
Android development record 19-Calculation of the dynamic release time of the circle of friends
For more information, see IT_xiao, http://blog.csdn.net/wwj_748? Username = wwj_748 # A previous article on content will share time knowledge points with you. Here are several important classes to understand:Calendar type: CalendarDate: DateDate Format class: SimpleDateFormatBasically, some conversions to dates are carried out in these classes. There are several common conversions: Get related date information through Calendar, such as the current year, month, and day, last year, month, day, and next year, you can view my blog on a custom calendar, which provides related date work types: conversion between http://blog.csdn.net/wwj_748/article/details/42244865 Date and Calendar Date and Date String Conversion
First, let's look at how to use Calendar to obtain the date information we want:
Get current calendar:
Calendar calendar = Calendar.getInstance();
Obtain information based on fieldsGet current year:
Calendar.getInstance().get(Calendar.YEAR)
Get current month:
Calendar.getInstance().get(Calendar.MONTH) + 1
Note that the value is + 1, because the month starts from 0. Get the day of the current month:
calendar.get(Calendar.DAY_OF_MONTH)
Display the current time in milliseconds:
calendar.getTimeInMillis()
Calendar can also perform a lot of operations. When developing the Calendar control, I made a lot of operations on the date: using the add method of Calendar to calculate the specified field, for example:
// Obtain the public static int getLastYear () {Calendar c = Calendar. getInstance (); c. add (Calendar. YEAR,-1); return c. get (Calendar. YEAR);} // obtain the public static int getNextYear () {Calendar Ar c = Calendar ar. getInstance (); c. add (Calendar. YEAR, 1); return c. get (Calendar. YEAR );}
// Obtain the public static int getLastMonth () {Calendar c = Calendar. getInstance (); c. add (Calendar. MONTH,-1); return c. get (Calendar. MONTH) + 1;} // obtain the MONTH of the next MONTH public static int getNextMonth () {Calendar Ar c = Calendar ar. getInstance (); c. add (Calendar. MONTH, 1); return c. get (Calendar. MONTH) + 1 ;}
There are many other fields in the Calendar, so you can check the API and try it yourself.
You can call the setTime method of Calendar to set the Calendar Object corresponding to the time.
calendar.setTime(new Date())
Mutual conversion between Date and Date string
In actual development, we often need to format the date data returned by the server. here we need to use 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, which can be converted to a Date object:
/*** Convert a Date String 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 obtained Date object to a string in the specified format:
/*** Convert a date to a String * @ param timeStr * @ param format * @ return */public static String dateToString (String timeStr, String format) {// determine whether this year's Date = DateHelper. strToDate (timeStr, format); Calendar calendar = Calendar. getInstance (); calendar. setTime (date); // if this year is used, the date format of if (calendar. get (Calendar. YEAR) = getYear () {return DATE_FORMAT_TILL_DAY_CURRENT_YEAR.format (date);} return DATE_FORMAT_TILL_DAY_CH.format (date );}
Finally, we need to calculate a dynamic release time during development. Generally, we get a date string. We need to calculate the current time and the dynamic release time, calculate their differences to determine when the code is published:
/*** Date logic ** @ param dateStr date String * @ return */public static String timeLogic (String dateStr) {Calendar ar calendar = Calendar. getInstance (); calendar. get (Calendar. DAY_OF_MONTH); long now = calendar. getTimeInMillis (); Date date = strToDate (dateStr, DATE_FORMAT); calendar. setTime (date); long past = calendar. getTimeInMillis (); // time difference long time = (now-past)/1000; StringBuffer sb = new StringBuffer (); if (time> 0 & time <60) {// return sb within 1 hour. append (time + "seconds ago "). toString ();} else if (time> 60 & amp; time <3600) {return sb. append (time/60 + "Minutes Ago "). toString ();} else if (time >=3600 & time <3600*24) {return sb. append (time/3600 + "Hours Ago "). toString ();} else if (time> = 3600*24 & time <3600*48) {return sb. append ("yesterday "). toString ();} else if (time> = 3600*48 & time <3600*72) {return sb. append ("the day before yesterday "). toString ();} else if (time> = 3600*72) {return dateToString (dateStr, DATE_FORMAT);} return dateToString (dateStr, DATE_FORMAT );}
The effect is similar: the record in this article is here. Some of the knowledge points we often encounter during development can be summarized and summarized. Mobile product development requires express delivery iteration, the records of these development knowledge points are of great help to you, improving your development efficiency, improving your ability, and increasing your salary, thank you for your patience.