1.日期转String
Date转String
Date d=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ds=df.format(d);
2.String转Date
String ds=new String("2019-06-09 10:12:32");
Date sd=df.parse(ds);
3.使用compare方法比较
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String beginTime=new String("2017-06-09 10:22:22");
String endTime = new String("2020-06-09 10:22:22");
Date nowTime = new Date();
String now = format.format(nowTime);
System.out.println(now);
System.out.println("返回正数,代表now > beginTime,左侧参数 > 右侧参数");
System.out.println(now.compareTo(beginTime));
System.out.println("返回0,代表now > beginTime,左侧参数 = 右侧参数");
System.out.println(now.compareTo(now));
System.out.println("返回负数,代表now < beginTime,左侧参数 < 右侧参数");
System.out.println(now.compareTo(endTime));