package tool;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CompareDate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String day1 = "2010-01-01 01:00:00";
String day2 = "2012-01-01 02:00:00";
try {
Date date1 = sdf.parse(day1);
Date date2 = sdf.parse(day2);
compareTime(date1, date2);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void compareTime(Date date1, Date date2){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
if(date1.compareTo(date2) > 0){
System.out.println(sdf.format(date1) + "年份大");
}else{
System.out.println(sdf.format(date2) + "年份大");
}
boolean flag = date1.after(date2);
if(flag){
System.out.println(sdf.format(date1) + "年份大");
}else{
System.out.println(sdf.format(date2) + "年份大");
}
}
}
java.util.Date d = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String temp = sdf.format(d);
try {
if(StringUtils.isBlank(bean.getWorktime().toString())){
temp = temp.substring(0,11)+"09:00:00";
bean.setWorktime(sdf.parse(temp));
}
if(StringUtils.isBlank(bean.getTimeoff().toString())){
temp = temp.substring(0,11)+"17:30:00";
bean.setTimeoff(sdf.parse(temp));
}
} catch (ParseException e) {
e.printStackTrace();
}
J2SE字符串时间比较与转换
最新推荐文章于 2021-02-12 20:06:50 发布