一、日期加减
public class DateFormatUtil {
public final static String DATE_TIME_PATTERN_FORMAT = "yyyy-MM-dd HH:mm:ss";
public final static String DATE_PATTERN_FORMAT = "yyyy-MM-dd";
public static String format(Date date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_FORMAT);
return sdf.format(date);
}catch (Exception e1){
e1.printStackTrace();
return null;
}
}
public static Date addDateMonths(Date date, int months) {
DateTime dateTime = new DateTime(date);
return dateTime.plusMonths(months).toDate();
}
public static Date addDateHours(Date date, int hours) {
DateTime dateTime = new DateTime(date);
return dateTime.plusHours(hours).toDate();
}
public static Date addDateDays(Date date, int days) {
DateTime dateTime = new DateTime(date);
return dateTime.plusDays(days).toDate();
}
public static List<String> getBetweenDates(Date start_date, Date end_date) {
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
if(tempStart.get(Calendar.DATE)<10){
result.add("0"+tempStart.get(Calendar.DATE));
}else{
result.add(""+tempStart.get(Calendar.DATE));
}
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
return result;
}
public static List<String> getBetweenDatesMD(Date start_date, Date end_date) {
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
String month = "";
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
if((tempStart.get(Calendar.MONTH)+1)<10){
month = "0"+(tempStart.get(Calendar.MONTH)+1);
}else {
month = ""+(tempStart.get(Calendar.MONTH)+1);
}
if(tempStart.get(Calendar.DATE)<10){
result.add(month + "-" + "0" + tempStart.get(Calendar.DATE));
}else{
result.add(month + "-" + tempStart.get(Calendar.DATE));
}
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
return result;
}
public static List<String> getBetweenYearMonthDates(Date start_date, Date end_date) {
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
String month = "";
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
if((tempStart.get(Calendar.MONTH)+1)<10){
month = "0"+(tempStart.get(Calendar.MONTH)+1);
}else {
month = ""+(tempStart.get(Calendar.MONTH)+1);
}
if(tempStart.get(Calendar.DATE)<10){
result.add(month+"-"+"0"+tempStart.get(Calendar.DATE));
}else{
result.add(month+"-"+tempStart.get(Calendar.DATE));
}
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
return result;
}
public static List<String> getBetweenYearMonthDatesYMD(Date start_date, Date end_date) {
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
String month = "";
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
if((tempStart.get(Calendar.MONTH)+1)<10){
month = "0"+(tempStart.get(Calendar.MONTH)+1);
}else {
month = ""+(tempStart.get(Calendar.MONTH)+1);
}
if(tempStart.get(Calendar.DATE)<10){
result.add(tempStart.get(Calendar.YEAR)+"-"+month+"-"+"0"+tempStart.get(Calendar.DATE));
}else{
result.add(tempStart.get(Calendar.YEAR)+"-"+month+"-"+tempStart.get(Calendar.DATE));
}
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
return result;
}
public static List<String> getBetweenHours(Date start_date, Date end_date) {
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
if(tempStart.get(Calendar.HOUR_OF_DAY)<10)
result.add("0"+tempStart.get(Calendar.HOUR_OF_DAY));
else
result.add(""+tempStart.get(Calendar.HOUR_OF_DAY));
tempStart.add(Calendar.HOUR_OF_DAY, 1);
}
return result;
}
public static List<String> getBetweenHoursM(Date start_date, Date end_date) {
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
if(tempStart.get(Calendar.HOUR_OF_DAY)<10)
result.add("0"+tempStart.get(Calendar.HOUR_OF_DAY)+":00");
else
result.add(""+tempStart.get(Calendar.HOUR_OF_DAY)+":00");
tempStart.add(Calendar.HOUR_OF_DAY, 1);
}
return result;
}
public static List<String> getBetweenMonths(Date start_date, Date end_date) {
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
int i = tempStart.get(Calendar.MONTH) +1;
int year = tempStart.get(Calendar.YEAR);
if(i<10)
result.add(year + "-0" + i);
else
result.add(year + "-" + i);
tempStart.add(Calendar.MONTH, 1);
}
return result;
}
public static List<String> getStrBetweenMonths(String start, String end) {
List<String> result = new ArrayList<String>();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start_date = sdf.parse(start);
Date end_date = sdf.parse(end);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
int i = tempStart.get(Calendar.MONTH) +1;
if(i<10)
result.add("0"+i);
else
result.add(""+i);
tempStart.add(Calendar.MONTH, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
public static List<String> getDateWeeks(int weeks) {
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
String beginDate,endDate;
List<String> result = new ArrayList<>();
for(int i = weeks - 1; i >=0 ; i-- ){
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
int offset1 = 1 - dayOfWeek;
int offset2 = 7 - dayOfWeek;
calendar1.add(Calendar.DATE, offset1 - 7 * i);
calendar2.add(Calendar.DATE, offset2 - 7 * i);
beginDate = sdf.format(calendar1.getTime());
endDate = sdf.format(calendar2.getTime());
if(i == 0){
endDate = sdf.format(new Date());
}
result.add(beginDate + "~" + endDate);
}
return result;
}
public static void main(String[] args) {
System.out.println(getBetweenHours(addDateHours(new Date(),-11),new Date()));
}
public static Date parse(String pattern,String dateString)throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date d = format.parse(dateString );
return d;
}
public static String getStartOfDay(String dateStr){
if(StringUtils.isEmpty(dateStr)){
return "";
}
try {
Date date = parse(DATE_PATTERN_FORMAT, dateStr);
return getStartOfDay(date);
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}
public static String getStartOfDay(Date date){
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN_FORMAT);
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
Date date1 = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
return sdf.format(date1);
}
public static String getEndOfDay(String dateStr){
if(StringUtils.isEmpty(dateStr)){
return "";
}
try {
Date date = parse(DATE_PATTERN_FORMAT, dateStr);
return getEndOfDay(date);
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}
public static String getEndOfDay(Date date){
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN_FORMAT);
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
Date date1 = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
return sdf.format(date1);
}
}