In actual development, some of the small partners will encounter some date processing problems, such as the date of comparison. Sometimes a date is not a date class, but rather a string representation, like this:1994-09-11, this date is not directly comparable, which requires us to format it first, where the DateFormat class.
First code:
| The code is as follows |
Copy Code |
public Boolean Comparedate (String startdate, String enddate) {
simpledateformat sdf = new SimpleDateFormat (" Yyyy-mm-dd ");
try {
Date sdate = Sdf.parse (startdate);
& nbsp; Date eDate = Sdf.parse (EndDate);
if (Edate.before (sdate)) {
return false;
} else {
return true;
}
} catch (ParseException e) {
e.printstacktrace ( );
}
return false;
}
|
We do not use the DateFormat class, the SimpleDateFormat class, it is a subclass of the DateFormat class, this is not to say.
Look at the code, first construct a format object SDF, and then use SDF to format the input of the two string into a date, its format by the string you passed the decision, here is YYYY-MM-DD, because I want to pass the string is 1994-09-11 this format. Then use the SDF before method to compare the size of two dates, is not very convenient?
The most used in practice is the SimpleDateFormat class, and as a subclass of DateFormat, the SimpleDateFormat class obviously has more features than DateFormat, and he can format the standard date into the date format you want, Here is the sample code:
| The code is as follows |
Copy Code |
Public String formatdate (date date) {
SimpleDateFormat formater = new SimpleDateFormat ("YYYY year mm month DD Day"); To set up a template by constructing a method
String datestring = Formater.format (date);
Formater.applypattern ("yyyy.") Mm.dd ");//Reset Formatting template
datestring = Formater.format (date);
return datestring;
}
|
Example
| code is as follows |
copy code |
|
Public class Timetest {
public static void Main (string[] args) {
    &N bsp; //TODO auto-generated method stub
String str1= " 2015-02-08 20:20:20 ";
String str2= "2015-01-08 10:10:10";
int Res=str1.compareto (STR2);
if (res>0)
System.out.println ("str1>str2");
Else if (res==0)
System.out.println ("str1=str2");
Else
System.out.println ("str1<str2");
}
}
|
Of course we can first try to check whether a string is not a valid date format
Example
| The code is as follows |
Copy Code |
|
public static Boolean isvaliddate (String str) {
Boolean convertsuccess=true;
Specify a date format of four-bit year/two-bit month/two-bit date, note that YYYY/MM/DD is case-sensitive;
SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd hh:mm");
try {
Set lenient to False. Otherwise SimpleDateFormat will be more relaxed to verify the date, such as 2007/02/29 will be accepted and converted into 2007/03/01
Format.setlenient (FALSE);
Format.parse (str);
catch (ParseException e) {
E.printstacktrace ();
If throw java.text.ParseException or NullPointerException, the format is incorrect
Convertsuccess=false;
}
return convertsuccess;
} |