In the Android development process, often encounter the date of various format conversion, mainly use SimpleDateFormat this class to implement, mastered the class, you can convert any of the various formats you want.
Common date formats:
1, date format: string datestring = "2017-06-20 10:30:30" corresponds to the format: string pattern = "Yyyy-mm-dd HH:mm:ss";
2, date format: string datestring = "2017-06-20" corresponds to the format: string pattern = "YYYY-MM-DD";
3, date format: string datestring = "June 20, 2017 10:30 30 seconds corresponds to the format: string pattern =" yyyy mm month DD Day hh when mm min SS second ";
4, date format: string datestring = "June 20, 2017" corresponds to the format: string pattern = "yyyy mm month dd Day";
Convert a long time format time to a string yyyy. Mm.dd hh:mm
Public Static String datetostrtohours (Date datedate) { new SimpleDateFormat ("yyyy. Mm.dd hh:mm "); = Formatter.format (datedate); return datestring; }
Note: yyyy-mm-dd HH:mm:ss need to write to where to write, such as above this is to take to minutes (not repeat below)
Second, the long time format time string yyyy year mm month DD Day HH converted to date
public static Date Strtodatelong (string strdate) {string format = null ; Date Date = null ; try {SimpleDateFormat Formatter_old
= new simpledateformat ("YYYY year mm month DD day hh" = Formatter_old.parse (strdate); } catch (Exception e) {e.printstacktrace (); return date; }
three, a long type is converted to a string type
// currenttime to convert a long type of time // formattype The time format of the type of string to convert publicstatic string longtostring (long currenttime, String formattype) Throws parseexception { // long type to date type // The date type turns into a string return strtime; }
The formattype here is the four types defined above
Four, long converts to date type
//currenttime The long type of time to convert//formattype the time format to convert YYYY-MM-DD HH:mm:ss//yyyy mm month dd Day hh mm min ss sec Public StaticDate Longtodate (Longcurrenttime, String formattype)throwsparseexception {Date dateold=NewDate (currenttime); String sdatetime = datetostring (Dateold, formattype);//convert time of date type to stringDate date = Stringtodate (Sdatetime, formattype);//To Convert a string type to a date type returndate;}
V. Convert a string type to a long type
//Strtime the time of the string type to convert//formattype Time Format//strtime time format and formattype time format must be the same Public Static LongStringtolong (String strtime, String formattype)throwsparseexception {Date Date= Stringtodate (Strtime, formattype);//The string type is converted to a date type if(Date = =NULL) { return0; } Else { LongCurrentTime = Datetolong (date);//date type turns into long type returncurrenttime; } }
Six, compare two times the size of the play
Compare the size of two dates with a date format of YYYY-MM-DD
/**
* Compare the size of two dates with the date format YYYY-MM-DD
*
* @param str1 the first date
* @param str2 the second date
* @return T
Public Static BooleanisDateOneBigger1 (String str1, String str2) {BooleanIsbigger =false; SimpleDateFormat SDF=NewSimpleDateFormat ("yyyy. Mm.dd hh:mm "); Date DT1=NULL; Date DT2=NULL; Try{dt1=Sdf.parse (STR1); DT2=Sdf.parse (STR2); } Catch(ParseException e) {e.printstacktrace (); } if(Dt1.gettime () >Dt2.gettime ()) {Isbigger=false; } Else if(Dt1.gettime () <Dt2.gettime ()) {Isbigger=true; } returnIsbigger; }
You need to view the first to 3rd to string and then compare.
The above method is used directly.
--Lei tua
Conversion and size of Android time type