字符串的比较除了eques,
还有a.contains(b):既a是否包含了b的所有字符
如:a:“qwerttyuiop” b:“qw” 则返回true
b:“qc” 返回false
1、时间转化:data—》INstant—》LocalDateTime
Instant instant = qudrq.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalDate localDate = localDateTime.toLocalDate();
//jdk1.8新特性,专门用来计算时间区间
Period period = Period.between(localDate, LocalDate.now(zone));
2、时间工具类
package com.patrol.beans.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期工具类
*
* @author 阿仔
*/
public class DateUtil {
/**
* 格式化日期和时间
*/
public static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 格式化日期
*/
public static SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
/**
* 获取当前日期yyyy-MM-dd
*
* @return
*/
public static String getDate() {
return sdfDate.format(new Date());
}
/**
* 获取当前日期
*
* @return
*/
public static String getCurrentDate() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DATE);
return "" + year + "-" + month + "-" + day;
}
/**
* 获取当前日期和时间
*
* @return
*/
public static String getCurrentDateTime() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
return "" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
/**
* 获取当前日期前若干天或者后若干天的日期
*
* @param dayCount ,天数,正数为之后,负数为之前
* @return
*/
public static String getIntervalDay(int dayCount) {
//SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate = new Date();
Calendar date = Calendar.getInstance();
date.setTime(beginDate);
date.set(Calendar.DATE, date.get(Calendar.DATE) + dayCount);
return sdfDate.format(date.getTime());
}
/**
* 获取昨天的日期
*
* @return
*/
public static String getYesterdayDate() {
return getIntervalDay(-1);
}
/**
* 获取今天的日期
*
* @return
*/
public static String getTodayDate() {
return getIntervalDay(0);
}
/**
* 获取明天的日期
*
* @return
*/
public static String getTomorrowDate() {
return getIntervalDay(1);
}
/**
* 获取明天的时间LONG
*
* @return
*/
public static Long getTomorrowTime() {
String tomorrowDate = DateUtil.getTomorrowDate();
return DateUtil.getSpecifiedDayTime(tomorrowDate);
}
/**
* 获取日期时间
*
* @param dateTime
* @return
*/
public static Long getLongTime(String dateTime) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = sdfTime.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}
/**
* 获得指定日期的前一天
*
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getSpecifiedDayBefore(String specifiedDay) {
// SimpleDateFormat simpleDateFormat = new
// SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = sdfDate.parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - 1);
String dayBefore = sdfDate.format(c.getTime());
return dayBefore;
}
/**
* 获得指定日期的后一天
*
* @param specifiedDay
* @return
*/
public static String getSpecifiedDayAfter(String specifiedDay) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = sdfDate.parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + 1);
String dayAfter = sdfDate.format(c.getTime());
return dayAfter;
}
/**
* 获取指定日期的时间LONG类型
*
* @param specifiedDay
* @return
*/
public static Long getSpecifiedDayTime(String specifiedDay) {
Date date = null;
try {
date = sdfDate.parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}
/**
* 获取时差日期和时间
*
* @param time 负数向前时间,正数向后时间
* @return
*/
public static String getDateTime(Date date, long time) {
date.setTime(time);
return sdfTime.format(date);
}
/**
* 增加或减少当前日期的毫秒数
*
* @param pastOrFutureTime 负数向前时间,正数向后时间
* @return
*/
public static String getAddDateTime(long pastOrFutureTime) {
Date date = new Date();
pastOrFutureTime = date.getTime() + pastOrFutureTime;
date.setTime(pastOrFutureTime);
return sdfTime.format(date);
}
/**
* 时间毫秒设置日期格式化
*
* @param time
* @return
*/
public static String setDateTime(long time) {
Date date = new Date();
date.setTime(time);
return sdfTime.format(date);
}
}