Skip to content

Commit d23c6c4

Browse files
author
linzhijun
committed
修改 MyDate.toString 方法
1 parent b5ef3f6 commit d23c6c4

File tree

1 file changed

+21
-26
lines changed
  • java/toolgood.algorithm/src/main/java/toolgood/algorithm

1 file changed

+21
-26
lines changed

java/toolgood.algorithm/src/main/java/toolgood/algorithm/MyDate.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public static MyDate now() {
177177
@Override
178178
public String toString() {
179179
StringBuffer stringBuffer = new StringBuffer();
180-
if (Year != null) {
180+
if (Year != null && Year > 0) {
181181
stringBuffer.append(Year);
182182
stringBuffer.append("-");
183183
if (Month < 10) {
@@ -190,26 +190,22 @@ public String toString() {
190190
}
191191
stringBuffer.append(Day);
192192

193-
if (Second > 0 || Hour > 0 || Minute > 0) {
194-
stringBuffer.append(" ");
195-
if (Hour < 10) {
196-
stringBuffer.append("0");
197-
}
198-
stringBuffer.append(Hour);
199-
stringBuffer.append(":");
200-
if (Minute < 10) {
201-
stringBuffer.append("0");
202-
}
203-
stringBuffer.append(Minute);
204-
if (Second > 0) {
205-
if (Second < 10) {
206-
stringBuffer.append("0");
207-
}
208-
stringBuffer.append(Second);
209-
}
193+
stringBuffer.append(" ");
194+
if (Hour < 10) {
195+
stringBuffer.append("0");
196+
}
197+
stringBuffer.append(Hour);
198+
stringBuffer.append(":");
199+
if (Minute < 10) {
200+
stringBuffer.append("0");
201+
}
202+
stringBuffer.append(Minute);
203+
if (Second < 10) {
204+
stringBuffer.append("0");
210205
}
206+
stringBuffer.append(Second);
211207
} else {
212-
if (Day != null) {
208+
if (Day != null && Day > 0) {
213209
stringBuffer.append(Day);
214210
stringBuffer.append(" ");
215211
}
@@ -222,12 +218,10 @@ public String toString() {
222218
stringBuffer.append("0");
223219
}
224220
stringBuffer.append(Minute);
225-
if (Second > 0) {
226-
if (Second < 10) {
227-
stringBuffer.append("0");
228-
}
229-
stringBuffer.append(Second);
221+
if (Second < 10) {
222+
stringBuffer.append("0");
230223
}
224+
stringBuffer.append(Second);
231225
}
232226

233227
return stringBuffer.toString();
@@ -237,9 +231,9 @@ public String toString(String f) {
237231
Date date;
238232
if (Year != null && Year > 1900) {
239233
date = new Date(Year, Month, Day, Hour, Minute, Second);
240-
}else if (Day != null){
234+
} else if (Day != null) {
241235
date = new Date(1900, 1, Day, Hour, Minute, Second);
242-
}else{
236+
} else {
243237
date = new Date(1900, 1, 0, Hour, Minute, Second);
244238
}
245239
SimpleDateFormat sd = new SimpleDateFormat(f);
@@ -249,6 +243,7 @@ public String toString(String f) {
249243
public DateTime ToDateTime() {
250244
return new DateTime(Year, Month, Day, Hour, Minute, Second, DateTimeZone.UTC);
251245
}
246+
252247
public DateTime ToDateTime(DateTimeZone zoo) {
253248
return new DateTime(Year, Month, Day, Hour, Minute, Second, zoo);
254249
}

0 commit comments

Comments
 (0)