Android-Use of date classes

Source: Internet
Author: User

Android Date classes, date and calendar, date display, calendar calculation.
Processing order: Calendar, Date, String.
The example implements several more common methods:
(1) The date is separated by the number of days;
(2) string conversion date class;
(3) The date of the previous acquisition;
(4) Whether the judgment is more than today.

Code:

 PackageMe.chunyu.Pedometer.manager;ImportAndroid.util.Pair;ImportJava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.ArrayList;ImportJava.util.Calendar;ImportJava.util.Date;ImportJava.util.Locale;ImportMe.chunyu.Pedometer.database.counter.DailyData;/** * Date Manager, enter the date of the format "Yyyy-mm-dd" of the string array, may not be contiguous. * To extract the number of steps, you need to have the number of steps to extract, the number of non-steps to 0. * When used, the date is counted. * <p/> * Crea Ted by Wangchenlong on 15/8/17. */ Public  class datemanager {    Private StaticDatemanager sinstance;//Single case    Private FinalRecordmanager Mrecordmanager;//Data Manager    Private FinalSimpleDateFormat Sformatter;//Formatting    Private Datemanager() {Mrecordmanager = Recordmanager.getinstance (); Sformatter =NewSimpleDateFormat ("Yyyy-mm-dd", locale.english); } Public StaticDatemanagergetinstance() {if(Sinstance = =NULL) {sinstance =NewDatemanager (); }returnSinstance; }/** * Gets the data from the Recordmanager, converts it to a number of steps array, from the current date to the earliest date. * * @return number of steps array [current date, to, earliest date] */     PublicArraylist<pair<string, integer>>getdatadates() {string[] days = Mrecordmanager.getsortedfilenames ();if(Days.length <=0) {return NULL; } String FirstDay = days[0]; String Lastday = days[days.length-1];if(Isaftertoday (Lastday)) {Lastday = Sformatter.format (NewDate ()); }Longn = daysbetweentwodates (FirstDay, Lastday) +1; Arraylist<integer> allsteps =NewArraylist<> (); arraylist<string> alldays =NewArraylist<> (); for(inti =0; I < n;            ++i) {String perday = Getbeforeday (Lastday, i); Dailydata dailydata = mrecordmanager.getdailysteps (Perday);if(Dailydata! =NULL) {int[] steps = dailydata.steps;intMaxstep = Steps[steps.length-1];            Allsteps.add (Maxstep); }Else{Allsteps.add (0);        } alldays.add (Perday); } arraylist<pair<string, integer>> totaldata =NewArraylist<> (); for(inti =0; I < allsteps.size ();            ++i) {pair<string, integer> datewithstep = pair.create (Alldays.get (i), allsteps.get (i));        Totaldata.add (Datewithstep); }returnTotaldata; }/** * Determine the number of days between two dates * * @param earlyday earlier date * @param lateday later date * @retur n days separated by * /     Public Long daysbetweentwodates(String earlyday, String lateday)        {Date earlydate = getdatefromstr (Earlyday);        Date latedate = Getdatefromstr (Lateday);        Calendar Startcalendar = Calendar.getinstance ();        Calendar Latecalendar = Calendar.getinstance ();        Startcalendar.settime (earlydate); Latecalendar.settime (latedate);Longdiff = Latecalendar.gettimeinmillis ()-Startcalendar.gettimeinmillis ();returnDiff/( -* -* -* +); }/** * Convert date strings to date class * * @param Day Date String * @return Date Data class * *     PublicDateGetdatefromstr(String Day) {Date date;Try{date = Sformatter.parse (day); }Catch(ParseException e) {E.printstacktrace ();return NULL; }returnDate }/** * Gets the date of the previous day * * @param days Time Benchmark * @param i difference days * @return days before date */     PublicStringGetbeforeday(String Day,inti) {Date date = Getdatefromstr (day);        Calendar calendar = Calendar.getinstance ();        Calendar.settime (date); Calendar.add (Calendar.day_of_year, I * (-1));returnSformatter.format (Calendar.gettime ()); }/** * Determine if the time is greater than today * * @param Day time * @return * *     Public Boolean Isaftertoday(String Day)        {Date latedate = Getdatefromstr (day); Date todaydate =NewDate ();returnLatedate.gettime () > Todaydate.gettime (); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android-Use of date classes

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.