The company suddenly has this demand:
1. Calculate the time 2 weeks before returning in the normal date format
2. If the month and the date before 0 need to get rid of the return results, what does that mean, for example: 2017-08-15 will need to show 2017-8-15, you say fuck no, what ghost demand, okay, go straight to the code.
Calendar calendar = Calendar.getinstance ();
Calendar.add (Calendar.date,-14);
Date date = Calendar.gettime ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String time = Sdf.format (date);//The date output here is 2 weeks ago, the output is 2017-08-16
The first method:
String result = "";
for (int i = 0; i< time.length (); i++) {
if ((i = = 5 && time.charat (i) = = ' 0 ') | (i = = 8 && Time.charat (i) = = ' 0 ')) {
Index + = "";
} else {
Index + = Time.charat (i);
}
}
SYSTEM.OUT,PRINTN (result);
The second method (the simplest way to use StringBuffer is OK):
StringBuffer buffer = new StringBuffer (time);
if (Buffer.charat (5) = = ' 0 ') {
Buffer.deletecharat (5);
}
if (Buffer.charat (7) = = ' 0 ') {
Buffer.deletecharat (7);
}
System.out.println (Buffer.tostring ());
Java string to delete the specified characters