比较时间大小
package com.fan.test;
import com.fan.dto.PersonDTO;
import com.fan.entity.Person;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TimeUtils {
public static void main(String[] args) throws ParseException {
String time1 = "2018-00-00 00:00:01";
String time2 = "2018-00-00 00:00:03";
String time3 = "2018-00-00 00:00:06";
String time4 = "2018-00-00 00:00:05";
List<Cat> cats = new ArrayList<>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
Date date3 = format.parse(time3);
Date date4 = format.parse(time4);
Cat cat1 = new Cat(1, "cat1", date1);
Cat cat2 = new Cat(2, "cat2", date2);
Cat cat3 = new Cat(3, "cat3", date3);
Cat cat4 = new Cat(4, "cat4", date4);
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
cats.add(cat4);
Cat catResult = cat1;
for (Cat cat : cats) {
if (catResult.getTime().compareTo(cat.getTime()) == -1) {
catResult = cat;
}
}
System.out.println(catResult);
}
static Integer bigOrSmall(String beginTime,String endTime) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = format.parse(beginTime);
Date date2 = format.parse(endTime);
int compareTo = date1.compareTo(date2);
System.out.println(compareTo);
return compareTo;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
|