Android calculates the date before and after the month of the week in which

Source: Internet
Author: User
Tags diff


A time-dependent tool class was 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:m  M **/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 **/p Ublic static final String df_hh_mm = "hh:mm";p rivate final static long minute = 60 * 1000;//1 min Private final static long hour = minute;//1 hours private final static long day = $ * hour;//1 days private final static long month = * day;//month pri Vate final static long year = month;//years/** log output identity **/private static final String TAG = ToolDateTime.class.getSimple Name ();/** * Format the date as a friendly string: a few minutes ago, a few hours ago, a fewDays ago, months ago, 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";} /** * Format the date in YYYY-MM-DD HH:mm:ss * * @param datel * date * @return */public static String formatdatetime (Long da TeL) {SimpleDateFormat SDF = new SimpleDateFormat (DF_YYYY_MM_DD_HH_MM_SS);D ate Date = new Date (Datel); return Sdf.format ( date);} /** * Format the date in YYYY-MM-DD HH:mm:ss * * @param datel * date * @return */public static String formatdatetime (Long da TeL, String formater) {SimpleDateFormat SDF = new SimpleDateFormat (formater); return Sdf.format (New Date (Datel));} /** * Date in YYYY-MM-DD HH:mm:ss format* * @param datel * date * @return */public static string FormatDateTime (date date, String formater) {Simpledat Eformat SDF = new SimpleDateFormat (formater); return Sdf.format (date);}  /** * Convert date string to 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);D ate returndate = null;try {returnDate = d Ateformat.parse (strdate);} catch (ParseException e) {log.v (TAG, "Parsedate failed!");} return returndate;} /** * Get System Current Date * * @return */public static date gaincurrentdate () {return new date (); /** * Verify that the date is earlier than the current date * * @param target1 * Comparison time 1 * @param Target2 * Comparison time 2 * @return true means target1 than TA Rget2 late or equal to Target2, otherwise than Target2 early */public static Boolean comparedate (date target1, date Target2) {Boolean flag = false;try {St Ring 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, Reason:" + e1.getmessage ());} return flag;} /** * Increase Operation date * * @param target * Date required for operation * @param hour * hours * @return */public static date add DateTime (Date target, double hour) {if (null = = Target | | Hour < 0) {return target;} return new Date (Target.gettime () + (long) (HOUR * 60 * 60 * 1000)); /** * Subtract dates * * @param target * Date of operation required * @param hour * hours * @return */public Static Date Sub DateTime (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); Setcontentview (r.layout.activity_main); Tv_week = ( TextView) Findviewbyid (r.id.tv_week) Tv_month = (TextView) Findviewbyid (r.id.tv_month);D ate mdate = new Date ();D ate Sdate = Null;date eDate = null; String sstr = ""; String estr = ""; sdate = Tooldatetime.subdatetime (Mdate, (Mdate.getday ()-1) *); for (int i = 0; I < 15; i++) {eDate = Tooldatetime.adddatetime (sdate, 6 *); sstr = Tooldatetime.formatdatetime (sdate,tooldatetime.df_yyyy_mm _DD); estr = Tooldatetime.formatdatetime (EDATE,TOOLDATETIME.DF_YYYY_MM_DD); Tv_week.append (sstr + "~" + estr + "\ n"); Sdate = Tooldatetime.adddatetime (eDate, 24);} SimpleDateFormat format = new SimpleDateFormat (TOOLDATETIME.DF_YYYY_MM_DD);//Gets the first day of the current month: String ""; String last = "", for (int i = 0; I < 10; i++) {Calendar c = calendar.getinstance (); Calendar CA = calendar.getinstance (); C.add (Calendar.month, i); C.set (Calendar.day_of_month, 1);//set to number 1th, The current date is the first day of the month, firstly = Format.format (C.gettime ());//Gets the last day of the current month Ca.add (Calendar.month, i); Ca.set (Calendar.day_of_month, Ca.getactualmaximum (Calendar.day_of_month)); last = Format.format (Ca.gettime ()); Tv_month.append (first+ "~" +last+ "\ n ");}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <textview        android:id=" @+id/tv_week "        android:layout_width=" Wrap_content "        android:layout_height= "wrap_content"/>    <textview        android:id= "@+id/tv_month"        Android : layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:layout_alignparentleft= " True "        android:layout_below=" @+id/tv_week "        android:layout_margintop=" 28DP "/></relativelayout >



Android calculates the date before and after the month of the week in which

Related Article

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.