Date before and after the month of the android computing week
:
A time-related tool class is used.
Package com. yqy. yqy_date; import android. util. log; import java. text. dateFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date;/*** time tool class * @ author YQY **/public class ToolDateTime {/** Date Format: yyyy-MM-dd HH: mm: ss **/public static final String DF_YYYY_MM_DD_HH_MM_SS = yyyy-MM-dd HH: mm: ss;/** Date Format: yyyy-MM-dd HH: mm **/public static final String DF_YYYY_MM_DD_HH_MM = Yyyy-MM-dd HH: mm;/** Date Format: yyyy-MM-dd **/public static final String DF_YYYY_MM_DD = yyyy-MM-dd; /** Date Format: HH: mm: ss **/public static final String DF_HH_MM_SS = HH: mm: ss;/** Date Format: HH: mm **/public static final String DF_HH_MM = HH: mm; private final static long minute = 60*1000; // 1 minute private final static long hour = 60 * minute; // 1 hour private final static long day = 24 * hour; // 1 day private final static Long month = 31 * day; // month private final static long year = 12 * month; // year/** Log output identifier **/private static final String TAG = ToolDateTime. class. getSimpleName ();/*** format the date into a friendly string: several minutes ago, several hours ago, a few days ago, a few months ago, a few years ago, just ** @ param date * @ return */public static String formatFriendly (Date date) {if (date = null) {return null;} long diff = new Date (). getTime ()-date. getTime (); long r = 0; if (diff> year) {r = (diff /Year); return r + years ago;} if (diff> month) {r = (diff/month); return r + months ago;} if (diff> day) {r = (diff/day); return r + days ago;} if (diff> hour) {r = (diff/hour); return r + hours ago ;} if (diff> minute) {r = (diff/minute); return r + minutes ago;} return just now;}/*** set the date to yyyy-MM-dd HH: mm: ss format ** @ param dateL * date * @ return */public static String formatDateTime (long dateL) {SimpleDateFormat sdf = new SimpleDateF Ormat (DF_YYYY_MM_DD_HH_MM_SS); Date date = new Date (dateL); return sdf. format (date);}/*** use yyyy-MM-dd HH: mm: ss formatting ** @ param dateL * date * @ return */public static String formatDateTime (long dateL, String formater) {SimpleDateFormat sdf = new SimpleDateFormat (formater); return sdf. format (new Date (dateL);}/*** use yyyy-MM-dd HH: mm: ss format ** @ param dateL * date * @ return */public static String format DateTime (Date date, String formater) {SimpleDateFormat sdf = new SimpleDateFormat (formater); return sdf. format (date);}/*** convert the date string to the date ** @ param strDate * string date * @ return java. util. date Date type */public static Date parseDate (String strDate) {DateFormat dateFormat = new SimpleDateFormat (DF_YYYY_MM_DD_HH_MM_SS); date returnDate = null; try {returnDate = dateFormat. parse (strDate);} catch (ParseException e) {Log. v (TAG, parseDate failed !);} Return returnDate;}/*** obtain the current system Date ** @ return */public static Date gainCurrentDate () {return new Date ();} /*** check whether the verification date is earlier than the current date ** @ param target1 * comparison time 1 * @ param target2 * comparison time 2 * @ return true indicates that target1 is later than target2 or equal target2, otherwise, it is earlier than target2 */public static boolean compareDate (Date target1, Date target2) {boolean flag = false; try {String target1DateTime = ToolDateTime. formatDateTime (target1, DF_YYYY_MM_DD_HH_MM_SS); String target2DateTime = ToolDateTime. formatDateTime (target2, DF_YYYY_MM_DD_HH_MM_SS); if (target1DateTime. compareTo (target2DateTime) <= 0) {flag = true ;}} catch (Exception e1) {System. out. println (comparison failed, cause: + e1.getMessage ();} return flag ;} /*** Add a Date ** @ param target * Date to be calculated * @ param hour * @ return */public static Date addDateTime (Date target, double hour) {if (null = target | hour <0) {return target;} return new Date (target. getTime () + (long) (hour * 60*60*1000 ));} /*** subtract a Date ** @ param target * Date to be calculated * @ param hour * @ return */public static Date subDateTime (Date target, double hour) {if (null = target | hour <0) {return target;} return new Date (target. getTime ()-(long) (hour * 60*60*1000 ));}}
MainActivity
Package com. yqy. yqy_date; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. widget. textView; public class MainActivity extends Activity {private TextView TV _week, TV _month; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContent View (R. layout. activity_main); TV _week = (TextView) findViewById (R. id. TV _week); TV _month = (TextView) findViewById (R. id. TV _month); Date mDate = new Date (); Date sDate = null; Date eDate = null; String sstr =; String estr =; sDate = ToolDateTime. subDateTime (mDate, (mDate. getDay ()-1) * 24); for (int I = 0; I <15; I ++) {eDate = ToolDateTime. addDateTime (sDate, 6*24); sstr = ToolDateTime. formatDateTime (sDa Te, ToolDateTime. DF_YYYY_MM_DD); estr = ToolDateTime. formatDateTime (eDate, ToolDateTime. DF_YYYY_MM_DD); TV _week.append (sstr ++ ~ + Estr +); sDate = ToolDateTime. addDateTime (eDate, 24);} SimpleDateFormat format = new SimpleDateFormat (ToolDateTime. DF_YYYY_MM_DD); // obtain the first day of the current month: String first =; String last =; for (int I = 0; I <10; I ++) {Calendar Ar c = Calendar ar. getInstance (); Calendar ca = Calendar. getInstance (); c. add (Calendar. MONTH, I); c. set (Calendar. DAY_OF_MONTH, 1); // set to 1. The current date is the first day of this month. format (c. getTime (); // obtain the last part of the current month Daily ca. add (Calendar. MONTH, I); ca. set (Calendar. DAY_OF_MONTH, www. bkjia. comca. getActualMaximum (Calendar. DAY_OF_MONTH); last = format. format (ca. getTime (); TV _month.append (first ++ ~ + Last +) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}